编辑代码

#include <iostream>
using namespace std;

// 声明抽象基类 Shape
class Shape {
public:
    virtual float area() const { return 0.0; }   // 虚函数
    virtual float volume() const { return 0.0; } // 虚函数
    virtual void shapeName() const = 0;          // 纯虚函数
};

// 声明 Point 类
class Point : public Shape { // Point 是 Shape 的公用派生类
public:
    Point(float = 0, float = 0);                 // 声明构造函数
    void setPoint(float, float);
    float getX() const { return x; }             // 获取点的 x 坐标
    float getY() const { return y; }             // 获取点的 y 坐标
    virtual void shapeName() const { cout << "Point: "; } // 重定义虚函数
    friend ostream& operator<<(ostream&, const Point&);   // 运算符重载
protected:
    float x, y;
};

// 定义 Point 类成员函数
Point::Point(float a, float b) : x(a), y(b) {}

void Point::setPoint(float a, float b) { x = a; y = b; }

ostream& operator<<(ostream& output, const Point& p) {
    output << "[" << p.x << "," << p.y << "]";
    return output;
}

// 声明 Circle 类
class Circle : public Point {
public:
    Circle(float x = 0, float y = 0, float r = 0);       // 声明构造函数
    void setRadius(float);                               // 设置半径
    float getRadius() const;                             // 取半径的值
    virtual float area() const;                          // 重定义虚函数
    virtual void shapeName() const { cout << "Circle: "; } // 重定义虚函数
    friend ostream& operator<<(ostream&, const Circle&);   // 运算符重载
protected:
    float radius;
};

// 定义 Circle 类成员函数
Circle::Circle(float a, float b, float r) : Point(a, b), radius(r) {}

void Circle::setRadius(float r) { radius = r; }

float Circle::getRadius() const { return radius; }

float Circle::area() const { return 3.14159 * radius * radius; }

ostream& operator<<(ostream& output, const Circle& c) {
    output << "[" << c.x << "," << c.y << "], r=" << c.radius;
    return output;
}

// 声明 Cylinder 类
class Cylinder : public Circle {
public:
    Cylinder(float x = 0, float y = 0, float r = 0, float h = 0); // 声明构造函数
    void setHeight(float);                                         // 设置圆柱高
    virtual float area() const;                                    // 重载虚函数  
    virtual float volume() const;                                  // 重载虚函数
    virtual void shapeName() const { cout << "Cylinder: "; }       // 重载虚函数
    friend ostream& operator<<(ostream&, const Cylinder&);         // 运算符重载
protected:
    float height;
};

// 定义 Cylinder 类成员函数
Cylinder::Cylinder(float a, float b, float r, float h)
    : Circle(a, b, r), height(h) {}

void Cylinder::setHeight(float h) { height = h; }

float Cylinder::area() const { return 2 * Circle::area() + 2 * 3.14159 * radius * height; }

float Cylinder::volume() const { return Circle::area() * height; }

ostream& operator<<(ostream& output, const Cylinder& cy) {
    output << "[" << cy.x << "," << cy.y << "], r=" << cy.radius << ", h=" << cy.height;
    return output;
}

// 主函数
int main() {
    Point point(3.2, 4.5);
    Circle circle(2.4, 1.2, 5.6);
    Cylinder cylinder(3.5, 6.4, 5.2, 10.5);
    
    point.shapeName();
    cout << point << endl;
    
    circle.shapeName();
    cout << circle << endl;
    
    cylinder.shapeName();
    cout << cylinder << endl << endl;
    
    Shape* pt;
    
    pt = &point;
    pt->shapeName();
    cout << "x=" << point.getX() << ", y=" << point.getY() << "\narea=" << pt->area()
         << "\nvolume=" << pt->volume() << "\n\n";
    
    pt = &circle;
    pt->shapeName();
    cout << "x=" << circle.getX() << ", y=" << circle.getY() << "\narea=" << pt->area()
         << "\nvolume=" << pt->volume() << "\n\n";
    
    pt = &cylinder;
    pt->shapeName();
    cout << "x=" << cylinder.getX() << ", y=" << cylinder.getY() << "\narea=" << pt->area()
         << "\nvolume=" << pt->volume() << "\n\n";
         
    return 0;
}