定义一个Shape抽象类,由它派生出Rectanglr和Circle类,二者都有GetArea( )#include

2024-12-02 20:07:36
推荐回答(1个)
回答1:

AreaCacul 类就是你的Shape抽象类,主控函数最下方 类中现在计算的是矩形和三角形两个继承类的面积。你参考改下!

class Areaclass{
public:
Areaclass(double x = 0, double y = 0): height(x), width(y) {}
protected:
double height, width;
};

class AreaCacul : public Areaclass
{
public:
AreaCacul(double x, double y):Areaclass(x,y){}
virtual double Area()=0;
};

class Box : public AreaCacul
{
public:
Box(double x,double y):AreaCacul(x,y){};
virtual double Area()
{
return height*width;
}
};

class Isosceles : public AreaCacul
{
public:
Isosceles(double x,double y):AreaCacul(x,y){};
virtual double Area()
{
return (height*width)/2;
}
};

#include
#include "test_class.h"
using namespace std;

int main(int argc, char *argv[])
{
AreaCacul *temp[] = {new Box(10,10),new Isosceles(10,10)};
cout<Area()< cout<Area()< system("pause");
return 0;
}