矩形类继承虚基类图形类 ,getTotalArea () 得到总的图形面积,编译器报错
#include<iostream>using namespace std;
class CShape {
public:
virtual double getArea()=0;
};
class CRectangle:public virtual CShape{
private:
double longth_,width_;
public:
CRectangle (double longth=0,double width=0):longth_(longth),width_(width){}
double getArea ();
};
double CRectangle::getArea ()
{
return longth_*width_ ;
}
double getTotalArea(CShape * array,int size)
{
double sum=0;
for(int i=0;i<size;i++)
{
sum+=array[i].getArea();//运行此处错误,编译器不怎么会用 ,求解
}
return sum;
}
void main()
{
CRectangle array[3]={CRectangle(1,2),CRectangle(1,3),CRectangle (1,4)};
cout<<getTotalArea(array,3)<<endl;
}