(1)设计一个圆类Circle和一个桌子类Table,其中,类Circle包含数据成员radius和求圆面积的成员函数,类Table包含数据成员height和返回高度的成员函数; (2)再设计一个圆桌类Roundtable,它是从前两个类派生出来的(Roundtable多继承Circle和Table); (3)类Roundtable再添加数据成员color和相应的成员函数; (4)要求输出圆桌的高度、面积、颜色等数据; 编写完整的程序实现。
时间: 2023-05-11 21:02:04 浏览: 133
以下是实现上述要求的完整代码:
```cpp
#include <iostream>
using namespace std;
class Circle {
private:
double radius;
public:
Circle(double r = 0) : radius(r) {}
double getArea() { return 3.14 * radius * radius; }
};
class Table {
private:
double height;
public:
Table(double h = 0) : height(h) {}
double getHeight() { return height; }
};
class Roundtable : public Circle, public Table {
private:
string color;
public:
Roundtable(double r, double h, string c) : Circle(r), Table(h), color(c) {}
string getColor() { return color; }
};
int main() {
Roundtable rt(2.5, 1.2, "red");
cout << "Height: " << rt.getHeight() << endl;
cout << "Area: " << rt.getArea() << endl;
cout << "Color: " << rt.getColor() << endl;
return 0;
}
```
输出结果为:
```
Height: 1.2
Area: 19.625
Color: red
```