声明抽象基类Shape,由它派生出5个派生类;Circle(圆形)、Square(正方形)、Rectangle(矩形)、Trapezoid(梯形)、Triangle(三角形),分别计算几种图形的面积,并求它们的和并显示出来。请按照要求选择如下一种方式编程实现。 注意圆周率取3.14. 实现方式: 1.使用继承和派生完成上述功能。 2.使用动态多态完成上述功能,提示:基类指针+派生类对象。 3. 使用派生类构造函数知识完成上述功能。
时间: 2024-03-02 09:50:59 浏览: 60
c++ 形状类Shape(派生出圆类Circle和矩形类Rectangle)
好的,我明白你的问题了。我将会给出三种不同的实现方式,你可以选择其中一种进行编程实现。
1. 使用继承和派生完成上述功能:
```c++
#include <iostream>
using namespace std;
class Shape {
public:
virtual float area() = 0; // 抽象基类,计算面积
};
class Circle : public Shape {
private:
float radius;
public:
Circle(float r) { radius = r; }
float area() { return 3.14 * radius * radius; }
};
class Square : public Shape {
private:
float side;
public:
Square(float s) { side = s; }
float area() { return side * side; }
};
class Rectangle : public Shape {
private:
float length, width;
public:
Rectangle(float l, float w) { length = l; width = w; }
float area() { return length * width; }
};
class Trapezoid : public Shape {
private:
float top, bottom, height;
public:
Trapezoid(float t, float b, float h) { top = t; bottom = b; height = h; }
float area() { return (top + bottom) * height / 2; }
};
class Triangle : public Shape {
private:
float base, height;
public:
Triangle(float b, float h) { base = b; height = h; }
float area() { return base * height / 2; }
};
int main() {
Shape *shapes[5];
shapes[0] = new Circle(5);
shapes[1] = new Square(4);
shapes[2] = new Rectangle(3, 5);
shapes[3] = new Trapezoid(2, 4, 3);
shapes[4] = new Triangle(3, 4);
float total_area = 0;
for (int i = 0; i < 5; i++) {
total_area += shapes[i]->area();
}
cout << "Total area: " << total_area << endl;
for (int i = 0; i < 5; i++) {
delete shapes[i];
}
return 0;
}
```
2. 使用动态多态完成上述功能:
```c++
#include <iostream>
using namespace std;
class Shape {
public:
virtual float area() = 0; // 抽象基类,计算面积
};
class Circle : public Shape {
private:
float radius;
public:
Circle(float r) { radius = r; }
float area() { return 3.14 * radius * radius; }
};
class Square : public Shape {
private:
float side;
public:
Square(float s) { side = s; }
float area() { return side * side; }
};
class Rectangle : public Shape {
private:
float length, width;
public:
Rectangle(float l, float w) { length = l; width = w; }
float area() { return length * width; }
};
class Trapezoid : public Shape {
private:
float top, bottom, height;
public:
Trapezoid(float t, float b, float h) { top = t; bottom = b; height = h; }
float area() { return (top + bottom) * height / 2; }
};
class Triangle : public Shape {
private:
float base, height;
public:
Triangle(float b, float h) { base = b; height = h; }
float area() { return base * height / 2; }
};
int main() {
Shape *shapes[5];
shapes[0] = new Circle(5);
shapes[1] = new Square(4);
shapes[2] = new Rectangle(3, 5);
shapes[3] = new Trapezoid(2, 4, 3);
shapes[4] = new Triangle(3, 4);
float total_area = 0;
for (int i = 0; i < 5; i++) {
total_area += shapes[i]->area();
}
cout << "Total area: " << total_area << endl;
for (int i = 0; i < 5; i++) {
delete shapes[i];
}
return 0;
}
```
3. 使用派生类构造函数知识完成上述功能:
```c++
#include <iostream>
using namespace std;
class Shape {
public:
virtual float area() = 0; // 抽象基类,计算面积
};
class Circle : public Shape {
private:
float radius;
public:
Circle(float r) { radius = r; }
float area() { return 3.14 * radius * radius; }
};
class Square : public Shape {
private:
float side;
public:
Square(float s) { side = s; }
float area() { return side * side; }
};
class Rectangle : public Square {
private:
float width;
public:
Rectangle(float l, float w) : Square(l) { width = w; }
float area() { return length * width; }
};
class Trapezoid : public Shape {
private:
float top, bottom, height;
public:
Trapezoid(float t, float b, float h) { top = t; bottom = b; height = h; }
float area() { return (top + bottom) * height / 2; }
};
class Triangle : public Shape {
private:
float base, height;
public:
Triangle(float b, float h) { base = b; height = h; }
float area() { return base * height / 2; }
};
int main() {
Shape *shapes[5];
shapes[0] = new Circle(5);
shapes[1] = new Square(4);
shapes[2] = new Rectangle(3, 5);
shapes[3] = new Trapezoid(2, 4, 3);
shapes[4] = new Triangle(3, 4);
float total_area = 0;
for (int i = 0; i < 5; i++) {
total_area += shapes[i]->area();
}
cout << "Total area: " << total_area << endl;
for (int i = 0; i < 5; i++) {
delete shapes[i];
}
return 0;
}
```
阅读全文