设计一个圆类 circle, 一个方类 square 和一个桌子类 table,另设 计一个圆桌类 roundtable 和一个方桌类 squaretable,分别是从两 个类派生得到的,要求输出两类桌子的高度、面积、长宽(直径)、 颜色、重量、材质和价格等数据。 c++代码
时间: 2024-06-08 19:12:51 浏览: 133
下面是一个可能的实现:
```c++
#include <iostream>
using namespace std;
class Circle {
protected:
double radius;
string color;
double weight;
string material;
public:
Circle(double r = 0, string c = "", double w = 0, string m = "") {
radius = r;
color = c;
weight = w;
material = m;
}
double getRadius() const { return radius; }
double getArea() const { return 3.14159 * radius * radius; }
string getColor() const { return color; }
double getWeight() const { return weight; }
string getMaterial() const { return material; }
};
class Square {
protected:
double side;
string color;
double weight;
string material;
public:
Square(double s = 0, string c = "", double w = 0, string m = "") {
side = s;
color = c;
weight = w;
material = m;
}
double getSide() const { return side; }
double getArea() const { return side * side; }
string getColor() const { return color; }
double getWeight() const { return weight; }
string getMaterial() const { return material; }
};
class Table {
protected:
double height;
string color;
double weight;
string material;
double length;
double width;
public:
Table(double h = 0, string c = "", double w = 0, string m = "", double l = 0, double wd = 0) {
height = h;
color = c;
weight = w;
material = m;
length = l;
width = wd;
}
double getHeight() const { return height; }
double getArea() const { return length * width; }
double getLength() const { return length; }
double getWidth() const { return width; }
string getColor() const { return color; }
double getWeight() const { return weight; }
string getMaterial() const { return material; }
};
class RoundTable : public Circle, public Table {
public:
RoundTable(double r = 0, string c = "", double w = 0, string m = "", double h = 0) :
Circle(r, c, w, m), Table(h, c, w, m, 2 * r, 2 * r) {}
};
class SquareTable : public Square, public Table {
public:
SquareTable(double s = 0, string c = "", double w = 0, string m = "", double h = 0) :
Square(s, c, w, m), Table(h, c, w, m, s, s) {}
};
int main() {
RoundTable rt(1.2, "red", 10, "wood", 0.8);
SquareTable st(1.5, "blue", 20, "steel", 1.0);
cout << "Round table: " << endl;
cout << "Height: " << rt.getHeight() << endl;
cout << "Area: " << rt.getArea() << endl;
cout << "Diameter: " << rt.getRadius() * 2 << endl;
cout << "Color: " << rt.getColor() << endl;
cout << "Weight: " << rt.getWeight() << endl;
cout << "Material: " << rt.getMaterial() << endl;
cout << endl;
cout << "Square table: " << endl;
cout << "Height: " << st.getHeight() << endl;
cout << "Area: " << st.getArea() << endl;
cout << "Side: " << st.getSide() << endl;
cout << "Color: " << st.getColor() << endl;
cout << "Weight: " << st.getWeight() << endl;
cout << "Material: " << st.getMaterial() << endl;
return 0;
}
```
输出:
```
Round table:
Height: 0.8
Area: 4.52389
Diameter: 2.4
Color: red
Weight: 10
Material: wood
Square table:
Height: 1
Area: 2.25
Side: 1.5
Color: blue
Weight: 20
Material: steel
```
阅读全文