C++类的继承声明一个Shape(形状)基类,它有两个派生类:Circle(圆)和Square(正方形),要求:(1)根

78678sdf2022-10-04 11:39:541条回答

C++类的继承
声明一个Shape(形状)基类,它有两个派生类:Circle(圆)和Square(正方形),要求:(1)根据给出的圆心坐标和半径计算圆的面积;(2)根据给出的正方形中心坐标和一个顶点坐标计算正方形的面积。
求代码?

已提交,审核后显示!提交回复

共1条回复
知我者谓我何求 共回答了20个问题 | 采纳率85%
可执行程序,希望对你有帮助
#include
#include
using namespace std;
class Shape
{
public:
Shape(){}
~Shape(){}
//纯虚函数
virtual float GetArea() const=0;
};
class Circle : public Shape
{
public:
Circle(float sz):r(sz)
{}
~Circle(){}
float GetArea()const;
private:
//半径
float r;
};
//圆只用半径就可以计算面积了
float Circle::GetArea()const
{
const float pi=3.1415926f;
return pi*r*r;
}
class Square : public Shape
{
public:
Square(float x1,float y1,float x2,float y2)
{
cx=x1;
cy=y1;
px=x2;
py=y2;
}
~Square(){}
float GetArea()const;
private:
//中心点
float cx;
float cy;
//顶点
float px;
float py;
};
//正方形面积=对角线乘机的一半
float Square::GetArea()const
{
return powf(2*sqrt((cx-px)*(cx-px)+(cy-py)*(cy-py)),2)/2;
}
int main()
{
Circle c(10.0);
Square s(10.0,10.0,4.0,4.0);
cout
1年前

相关推荐

这是一道有关C++类与对象的题目
这是一道有关C++类与对象的题目
题目是这样的
声明一个名为Rectangle的矩形类,其属性为矩形的左下角和右上角两点的坐标,并有成员函数计算矩形的周长及面积.编程实现求左下角与右上角坐标分别为(2.1,3.2),(5.2,6.3)的矩形周长及面积.
求大神帮我改改程序
#include
#include
using namespace std;
class Rectangle
{
public:
x05Rectangle(double a,double b,double c,double d)
x05{x1=a;y1=b;x2=c;y2=d;}
x05void lowerleft()
x05{cout
背海听歌1年前1
mxzty123 共回答了15个问题 | 采纳率93.3%
g=2*(x2-x1+y2-y1); 2后面少了个*号
C++类定义 下边这样定义对不对 题目要求输入图形坐标求面积 能不能这样把图形和坐标类定义?
C++类定义 下边这样定义对不对 题目要求输入图形坐标求面积 能不能这样把图形和坐标类定义?
class CGraphicArea//类定义
{
private:
char Round;
char Rect;
char Tri;
char Trap;
public:
double x1,y1;
double x2,y2;
double x3,y3;
double x4,y4;
double Area(double);
};
像猪一样飞翔5211年前1
淼涵水啊 共回答了24个问题 | 采纳率87.5%
语法上没有问题
逻辑上应该把
char Round;
char Rect;
char Tri;
char Trap;
改成
char name[10];
name里存放图形的名称
4.计算表达式的值求用C++类做的代码
4.计算表达式的值求用C++类做的代码
对于给定的一个表达式,表达式中可以包括常数、算术运行符(“+”、“-”、“*”、“/”)和括号,编写程序计算表达式的值.
基本要求:从键盘输入一个正确的中缀表达式,将中缀表达式转换为对应的后缀表达式,计算后缀表达式的值.
测试数据:任意选取一个符合题目要求的表达式.
提高要求:(1)对于表达式中的简单错误,能够给出提示;
(2)不仅提示错误,也能给出错误信息
(3)表达式中可以包括单个字母表示的变量
(4)能够处理多种操作符
(5)实现包含简单运算的计算器
(6)实现一个包含简单运算和函数运算的计算器
430nn1年前1
杳然830301 共回答了22个问题 | 采纳率95.5%
#include
#include
using namespace std;

bool isoperator(char op);x09x09x09x09x09x09// 判断是否为运算符
int priority(char op);x09x09x09x09x09x09x09// 求运算符优先级
void postfix(char pre[] , char post[],int n);// 把中缀表达式转换为后缀表达式
double read_number(char str[],int *i);x09x09x09// 将数字字符串转变成相应的数字
double postfix_value(char post[]);x09x09x09x09// 由后缀表达式字符串计算相应的中值表达式的值x09

template class MyStack
{
public:
x09const staticint MAXSIZE =100;
x09ElemType data[MAXSIZE];
x09int top;

public:
x09void init();x09x09x09// 初始化栈
x09bool empty();x09x09x09// 判断栈是否为空
x09ElemType gettop();x09// 读取栈顶元素(不出栈)
x09void push(ElemType x);x09// 进栈
x09ElemType pop();x09x09x09// 出栈
};


template void MyStack::init()
{
x09this->top = 0;
}

template bool MyStack::empty()
{
x09return this->top == 0? true : false;
}

template T MyStack::gettop()
{
x09if(empty())
x09{
x09x09cout << "栈为空!n";
x09x09exit(1);
x09}
x09return this->data[this->top-1];
}

template void MyStack::push(T x)
{
x09if(this->top == MAXSIZE)
x09{
x09x09cout << "栈已满!n";
x09x09exit(1);
x09}
x09this->data[this->top] =x;
x09this->top ++;
}

template T MyStack::pop()
{
x09if(this->empty())
x09{
x09x09cout << "栈为空! n";
x09x09exit(1);
x09}

x09T e =this->data[this->top-1];
x09this->top --;
x09return e;
}


int main()
{
x09MyStack stack ;
x09stack.init();

x09//char pre[] ="22/(5*2+1)#";
x09char exp[100];
x09cout << "输入表达式(中缀,以#结束):";
x09cin >> exp;

x09char post[100] ;
x09//cout <<"中缀表达式为:"<< pre << endl;

x09int n =0;x09x09x09// 返回后缀表达式的长度
x09postfix(exp,post,n);
x09cout <<"后缀表达式为:";
x09for( int i =0 ;i < n ;i++)
x09x09cout << post[i] ;

x09cout << "n由后缀表达式计算出的数值结果:";
x09cout << postfix_value(post) << endl;

x09system("pause");
x09return 0;
}

bool isoperator(char op)
{
x09switch(op)
x09{
x09case '+':
x09case '-':
x09case '*':
x09case '/':
x09x09return 1;
x09default :
x09x09return 0;
x09}
}


int priority(char op)
{
x09switch(op)
x09{
x09case '#':
x09x09return -1;
x09case '(':
x09x09return 0;
x09case '+':
x09case '-':
x09x09return 1;
x09case '*':
x09case '/':
x09x09return 2;
x09default :
x09x09return -1;
x09}
}

//x09 把中缀表达式转换为后缀表达式,返回后缀表达式的长度(包括空格)
void postfix(char pre[] ,char post[],int n)
{
x09int i = 0 ,j=0;
x09MyStack stack;
x09stack.init();x09x09// 初始化存储操作符的栈

x09stack.push('#');x09// 首先把结束标志‘#’放入栈底

x09while(pre[i]!='#')
x09{
x09x09if((pre[i]>='0' pre[i] <='9')||pre[i] =='.') // 遇到数字和小数点直接写入后缀表达式
x09x09{
x09x09x09post[j++] = pre[i];
x09x09x09n++;
x09x09}
x09x09else if (pre[i]=='(')x09// 遇到“(”不用比较直接入栈
x09x09x09stack.push(pre[i]);
x09x09else if(pre[i] ==')')// 遇到右括号将其对应左括号后的操作符(操作符栈中的)全部写入后缀表达式
x09x09{
x09x09x09while(stack.gettop()!='(')
x09x09x09{
x09x09x09x09post[j++] = stack.pop();
x09x09x09x09n++;
x09x09x09}
x09x09x09stack.pop(); // 将“(”出栈,后缀表达式中不含小括号
x09x09}
x09x09else if (isoperator(pre[i]))
x09x09{
x09x09x09post[j++] = ' '; // 用空格分开操作数(
x09x09x09n++;
x09x09x09while(priority(pre[i]) <= priority(stack.gettop()))
x09x09x09{
x09x09x09x09// 当前的操作符小于等于栈顶操作符的优先级时,将栈顶操作符写入到后缀表达式,重复此过程
x09x09x09x09post[j++] = stack.pop();
x09x09x09x09n++;
x09x09x09}

x09x09x09stack.push(pre[i]);x09// 当前操作符优先级大于栈顶操作符的优先级,将该操作符入栈
x09x09}

x09x09i++;
x09}
x09while(stack.top) // 将所有的操作符加入后缀表达式
x09{
x09x09post[j++] = stack.pop();
x09x09n++;
x09}
}

double read_number(char str[],int *i)
{
x09double x=0.0;
x09int k = 0;
x09while(str[*i] >='0' str[*i]<='9')// 处理整数部分
x09{
x09x09x = x*10+(str[*i]-'0');
x09x09(*i)++;
x09}

x09if(str[*i]=='.') // 处理小数部分
x09{
x09x09(*i)++;
x09x09while(str[*i] >= '0'&&str[*i] <='9')
x09x09{
x09x09x09x = x * 10 + (str[*i]-'0');
x09x09x09(*i)++;
x09x09x09k++;
x09x09}
x09}
x09while(k!=0)
x09{
x09x09x /= 10.0;
x09x09k--;
x09}

x09return x;
}

double postfix_value(char post[])
{
x09MyStack stack;x09// 操作数栈
x09stack.init();

x09int i=0 ;
x09double x1,x2;

x09while(post[i] !='#')
x09{
x09x09if(post[i] >='0' post[i] <='9')
x09x09x09stack.push(read_number(post,&i));
x09x09else if(post[i] == ' ')
x09x09x09i++;
x09x09else if (post[i] =='+')
x09x09{
x09x09x09x2 = stack.pop();
x09x09x09x1 = stack.pop();
x09x09x09stack.push(x1+x2);
x09x09x09i++;
x09x09}
x09x09else if (post[i] =='-')
x09x09{
x09x09x09x2 = stack.pop();
x09x09x09x1 = stack.pop();
x09x09x09stack.push(x1-x2);
x09x09x09i++;
x09x09}
x09x09else if (post[i] =='*')
x09x09{
x09x09x09x2 = stack.pop();
x09x09x09x1 = stack.pop();
x09x09x09stack.push(x1*x2);
x09x09x09i++;
x09x09}
x09x09else if (post[i] =='/')
x09x09{
x09x09x09x2 = stack.pop();
x09x09x09x1 = stack.pop();
x09x09x09stack.push(x1/x2);
x09x09x09i++;
x09x09}
x09}
x09return stack.gettop();
}