请C++的高手帮我做道题 必须用C语言的知识??

来源:百度知道 编辑:UC知道 时间:2024/06/07 13:08:13
声明一个基类Shape,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square

class Shape {
public:
virtual double GetArea()=0;
virtual ~Shape(){}
};
class Rectangle : public Shape {
public:
Rectangle(double w, double h): width(w), height(h){}
double GetArea() { return height*width; }
protected:
double width, height;
};
class Circle : public Shape {
public:
Circle(double r): radius(r){}
double GetArea() { return 3.14159265359*radius*radius; }
protected:
double radius;
};
class Square : public Rectangle {
public:
Square(double d): Rectangle(d, d){}
};