设计图形类CShp,包含成员函数设置/获取图形类型(string)、设置/获取图形颜色(整数)。从CShp派生两个类CTri(三角形),CCir(圆), 并有成员函数设置/获取 数值函数和求周长函数。在main 函数中测试CTri、CCir,调用以上函数(共7个函数)
时间: 2023-05-11 21:04:23 浏览: 181
好的,以下是我为您准备的代码:
```
#include <iostream>
#include <string>
using namespace std;
class CShp {
protected:
string type;
int color;
public:
void setType(string t) {
type = t;
}
string getType() {
return type;
}
void setColor(int c) {
color = c;
}
int getColor() {
return color;
}
};
class CTri : public CShp {
private:
double a, b, c;
public:
void setSides(double x, double y, double z) {
a = x;
b = y;
c = z;
}
double getSideA() {
return a;
}
double getSideB() {
return b;
}
double getSideC() {
return c;
}
double getPerimeter() {
return a + b + c;
}
};
class CCir : public CShp {
private:
double radius;
public:
void setRadius(double r) {
radius = r;
}
double getRadius() {
return radius;
}
double getArea() {
return 3.14 * radius * radius;
}
double getPerimeter() {
return 2 * 3.14 * radius;
}
};
int main() {
CTri tri;
tri.setType("Triangle");
tri.setColor(1);
tri.setSides(3, 4, 5);
cout << "Triangle type: " << tri.getType() << endl;
cout << "Triangle color: " << tri.getColor() << endl;
cout << "Triangle side A: " << tri.getSideA() << endl;
cout << "Triangle side B: " << tri.getSideB() << endl;
cout << "Triangle side C: " << tri.getSideC() << endl;
cout << "Triangle perimeter: " << tri.getPerimeter() << endl;
CCir cir;
cir.setType("Circle");
cir.setColor(2);
cir.setRadius(5);
cout << "Circle type: " << cir.getType() << endl;
cout << "Circle color: " << cir.getColor() << endl;
cout << "Circle radius: " << cir.getRadius() << endl;
cout << "Circle area: " << cir.getArea() << endl;
cout << "Circle perimeter: " << cir.getPerimeter() << endl;
return 0;
}
```
希望这个代码能够满足您的需求,如果您有任何问题或需要进一步的帮助,请随时告诉我。
阅读全文