1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| #include <cmath>
class Shape { protected: double x, y; string color; public: Shape(double x, double y, const string& c) : x(x), y(y), color(c) { cout << "Shape构造: (" << x << "," << y << ") " << color << endl; } virtual ~Shape() { cout << "Shape析构: " << color << endl; } virtual double getArea() const = 0; virtual double getPerimeter() const = 0; virtual void draw() const = 0; virtual void move(double dx, double dy) { x += dx; y += dy; cout << color << "形状移动到 (" << x << "," << y << ")" << endl; } void setColor(const string& c) { color = c; } string getColor() const { return color; } double getX() const { return x; } double getY() const { return y; } virtual void displayInfo() const { cout << color << "形状位于 (" << x << "," << y << ")" << endl; } };
class Circle : public Shape { private: double radius; public: Circle(double x, double y, double r, const string& c = "红色") : Shape(x, y, c), radius(r) { cout << "Circle构造: 半径=" << radius << endl; } ~Circle() { cout << "Circle析构: 半径=" << radius << endl; } double getArea() const override { return M_PI * radius * radius; } double getPerimeter() const override { return 2 * M_PI * radius; } void draw() const override { cout << "绘制" << color << "圆形: 中心(" << x << "," << y << "), 半径=" << radius << endl; } double getRadius() const { return radius; } void setRadius(double r) { if (r > 0) radius = r; } void displayInfo() const override { Shape::displayInfo(); cout << "半径: " << radius << ", 面积: " << getArea() << ", 周长: " << getPerimeter() << endl; } };
class Rectangle : public Shape { private: double width, height; public: Rectangle(double x, double y, double w, double h, const string& c = "蓝色") : Shape(x, y, c), width(w), height(h) { cout << "Rectangle构造: " << width << "x" << height << endl; } ~Rectangle() { cout << "Rectangle析构: " << width << "x" << height << endl; } double getArea() const override { return width * height; } double getPerimeter() const override { return 2 * (width + height); } void draw() const override { cout << "绘制" << color << "矩形: 左上角(" << x << "," << y << "), 大小=" << width << "x" << height << endl; } double getWidth() const { return width; } double getHeight() const { return height; } void setWidth(double w) { if (w > 0) width = w; } void setHeight(double h) { if (h > 0) height = h; } void displayInfo() const override { Shape::displayInfo(); cout << "尺寸: " << width << "x" << height << ", 面积: " << getArea() << ", 周长: " << getPerimeter() << endl; } };
class Square : public Rectangle { public: Square(double x, double y, double side, const string& c = "绿色") : Rectangle(x, y, side, side, c) { cout << "Square构造: 边长=" << side << endl; } ~Square() { cout << "Square析构" << endl; } void draw() const override { cout << "绘制" << color << "正方形: 左上角(" << x << "," << y << "), 边长=" << getWidth() << endl; } double getSide() const { return getWidth(); } void setSide(double side) { setWidth(side); setHeight(side); } void displayInfo() const override { Shape::displayInfo(); cout << "边长: " << getSide() << ", 面积: " << getArea() << ", 周长: " << getPerimeter() << endl; } };
void exercise1() { cout << "\n=== 练习1:图形继承体系 ===" << endl; Circle circle(0, 0, 5); Rectangle rect(10, 10, 8, 6); Square square(20, 20, 4); cout << "\n=== 图形信息 ===" << endl; circle.displayInfo(); rect.displayInfo(); square.displayInfo(); cout << "\n=== 绘制图形 ===" << endl; circle.draw(); rect.draw(); square.draw(); cout << "\n=== 移动图形 ===" << endl; circle.move(5, 5); rect.move(-2, 3); square.move(1, -1); cout << "\n=== 多态演示 ===" << endl; Shape* shapes[] = {&circle, &rect, &square}; for (int i = 0; i < 3; i++) { cout << "形状 " << (i+1) << ": "; shapes[i]->draw(); cout << "面积: " << shapes[i]->getArea() << endl; } cout << "\n函数结束,开始析构..." << endl; }
|