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

2024年12月02日 13:45
有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; }
};