已有抽象类shape定义如下,该类提供三个纯虚函数;显示数据成员、返回面积和返回体积。 Class Shape { virtual void showData()=0; virtual double reArea()=0; virtual double reVolume()=0; }; 以Shape为顶层基类,派生TwoDimShape(二维图形)和ThreeShape(三维图形)两个类,它们增加了有关的数据成员(自己设定,各自不少于2个),但没有成员函数的实现。 第三层派生具体的图形类。TwoDimShape类派生Circle(圆)、Elipse(椭圆)、Rectangle(矩形)和Triangle(三角形)等类。ThreeShape类派生Ball(球体)、Cylinder(圆柱体)、RectangularParallelepiped(长方体)等类。 在主函数测试中使用多态方式调用不同对象的求面积和体积等函数。请写出这段代码
时间: 2024-01-18 17:01:57 浏览: 67
C抽象基类和纯虚成员函数-C教程共3页.pdf.zip
好的,下面是这段代码的实现:
```c++
#include <iostream>
using namespace std;
class Shape {
public:
virtual void showData() = 0;
virtual double reArea() = 0;
virtual double reVolume() = 0;
};
class TwoDimShape : public Shape {
public:
double width;
double height;
};
class ThreeShape : public Shape {
public:
double length;
double width;
double height;
};
class Circle : public TwoDimShape {
public:
void showData() {
cout << "Circle: " << endl;
cout << "Width: " << width << ", Height: " << height << endl;
}
double reArea() {
return 3.14159 * width * width / 4;
}
double reVolume() {
return 0;
}
};
class Elipse : public TwoDimShape {
public:
void showData() {
cout << "Elipse: " << endl;
cout << "Width: " << width << ", Height: " << height << endl;
}
double reArea() {
return 3.14159 * width * height / 4;
}
double reVolume() {
return 0;
}
};
class Rectangle : public TwoDimShape {
public:
void showData() {
cout << "Rectangle: " << endl;
cout << "Width: " << width << ", Height: " << height << endl;
}
double reArea() {
return width * height;
}
double reVolume() {
return 0;
}
};
class Triangle : public TwoDimShape {
public:
void showData() {
cout << "Triangle: " << endl;
cout << "Width: " << width << ", Height: " << height << endl;
}
double reArea() {
return width * height / 2;
}
double reVolume() {
return 0;
}
};
class Ball : public ThreeShape {
public:
void showData() {
cout << "Ball: " << endl;
cout << "Length: " << length << ", Width: " << width << ", Height: " << height << endl;
}
double reArea() {
return 4 * 3.14159 * length * length;
}
double reVolume() {
return 4 * 3.14159 * length * length * length / 3;
}
};
class Cylinder : public ThreeShape {
public:
void showData() {
cout << "Cylinder: " << endl;
cout << "Length: " << length << ", Width: " << width << ", Height: " << height << endl;
}
double reArea() {
return 2 * 3.14159 * length * width + 2 * 3.14159 * length * height;
}
double reVolume() {
return 3.14159 * length * width * height;
}
};
class RectangularParallelepiped : public ThreeShape {
public:
void showData() {
cout << "RectangularParallelepiped: " << endl;
cout << "Length: " << length << ", Width: " << width << ", Height: " << height << endl;
}
double reArea() {
return 2 * (length * width + width * height + length * height);
}
double reVolume() {
return length * width * height;
}
};
int main() {
Shape* shapes[7];
shapes[0] = new Circle();
shapes[0]->width = 2;
shapes[0]->height = 2;
shapes[1] = new Elipse();
shapes[1]->width = 2;
shapes[1]->height = 3;
shapes[2] = new Rectangle();
shapes[2]->width = 2;
shapes[2]->height = 3;
shapes[3] = new Triangle();
shapes[3]->width = 2;
shapes[3]->height = 3;
shapes[4] = new Ball();
shapes[4]->length = 2;
shapes[4]->width = 2;
shapes[4]->height = 2;
shapes[5] = new Cylinder();
shapes[5]->length = 2;
shapes[5]->width = 2;
shapes[5]->height = 3;
shapes[6] = new RectangularParallelepiped();
shapes[6]->length = 2;
shapes[6]->width = 3;
shapes[6]->height = 4;
for (int i = 0; i < 7; i++) {
shapes[i]->showData();
cout << "Area: " << shapes[i]->reArea() << endl;
cout << "Volume: " << shapes[i]->reVolume() << endl;
cout << endl;
}
for (int i = 0; i < 7; i++) {
delete shapes[i];
}
return 0;
}
```
在这段代码中,首先定义了Shape类及其两个派生类TwoDimShape和ThreeShape。然后,定义了每个具体图形类,它们分别继承自TwoDimShape和ThreeShape,并实现了Shape类中的三个纯虚函数。
接下来,在main函数中,定义了一个Shape类型的指针数组,将各种不同类型的图形对象的指针存储到该数组中。然后,使用循环遍历该数组,通过多态调用各个对象的showData、reArea和reVolume函数,输出各个图形的数据、面积和体积。最后,释放动态分配的内存。
这就是这段代码的实现。
阅读全文