C++实现: 5.定义一个具有多态性的基类Shape,派生出以下一些类,圆Circle类(坐标点和半径),长方形Recta

2024-12-02 13:45:36
推荐回答(1个)
回答1:

class point;

class shape {
public:
virtual double circumference() = 0;
virtual double area() = 0;
};

#define PI 3.14159
class circle : public shape {
private:
point center;
int radius;
public:
circle(const point &p, int r) : center(p), radius(r) {}
virtual double circumference() { return 2 * PI * radius; }
virtual double area() { return PI * radius * radius; }
};

class rectangle : public shape {
private:
point upleft;
int width, height;
public:
rectangle(const point &p, int w, int h) : upleft(p), width(w), height(h) {}
virtual double circunference() { return 2 * (width + height); }
virtual double area() { return width * height; }
};