C++程序题:定义抽象基类Shape且由它派生出3个派生类

2024-12-19 23:54:32
推荐回答(1个)
回答1:

class Shape
{
public:
virtual double GetArea()=0;
};

class Rectangle : public Shape
{
public:
Rectangle(double h, double w) : height(h), width(w){}
double GetArea(){ return height * width; }
private:
double height;
double width;
};
class Circle : public Shape
{
public:
Circle(double r):radius(r){}
double GetArea(){ return 3.14159 * radius * radius; }
private:
double radius;
};

class Square : public Rectangle
{
public:
Square(double l) : Rectangle(l, l){}
};

http://zhidao.baidu.com/question/15305701.html