问题遇到的现象和发生背景
求各种图形的面积,但不知道如何解决最后的所有图形面积相加的问题
问题相关代码,请勿粘贴截图
#include<iostream>
#define Pi 3.14159
using namespace std;
class CShape{
protected:
double x,y;
public:
void set(double i,double j)
{
x=i;
y=j;
}
virtual double GetArea()=0;
};
class CRectangle:public CShape{
public:
double GetArea(){
return x*y;
}
};
class CCircle: public CShape
{
private:
double radius;
public:
CCircle(double r):radius(r){ }
double GetArea() { return Pi*radius*radius;}
};
class CSquare: public CShape{
private:
double side;
public:
CSquare(double s):side(s){ }
double GetArea(){ return side*side;}
};
class CTrapezoid:public CShape{
private:
double high,bottom,waist;
public:
CTrapezoid(double h,double b,double w):high(h),bottom(b),waist(w){ };
double GetArea(){ return 0.5*(high+bottom)*waist;}
};
class CTriangle:public CShape{
private:
double length,width;
public:
CTriangle(double le,double wi):length(le),width(wi){ };
double GetArea(){ return 0.5*length*width;}
};
int main()
{
double sum=0;
CShape *c;
CCircle i(6.6);
CRectangle r;
CSquare s(8);
CTrapezoid z(1,2,3);
CTriangle g(3,4);
c=&r;
c->set(6,6);
cout<<"CRectangle area:"<<c->GetArea()<<endl;
c=&i;
cout<<"CCircle area:"<<c->GetArea()<<endl;
c=&s;
cout<<"CSquare area:"<<c->GetArea()<<endl;
c=&z;
cout<<"CTrapezoid area:"<<c->GetArea()<<endl;
c=&g;
cout<<"CTriangle area:"<<c->GetArea()<<endl;
return 0;
}
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
最后输出一个图形面积之和的结果