设计一个矩形类Rectangle 数据成员: length(私有),width(私有)和color(公有) 成员函数:(均为公有) SetLengthWidth,GetLengthWi GetArea, ShowColor,SetColor 并设计一个主函数来测试你所编写的类。
时间: 2024-05-14 11:12:50 浏览: 106
```c++
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
string color;
void SetLengthWidth(double l, double w) {
length = l;
width = w;
}
double GetLength() {
return length;
}
double GetWidth() {
return width;
}
double GetArea() {
return length * width;
}
void ShowColor() {
cout << "Color: " << color << endl;
}
void SetColor(string c) {
color = c;
}
};
int main() {
Rectangle rect;
rect.SetLengthWidth(4.5, 3.2);
rect.SetColor("red");
rect.ShowColor();
cout << "Length: " << rect.GetLength() << endl;
cout << "Width: " << rect.GetWidth() << endl;
cout << "Area: " << rect.GetArea() << endl;
return 0;
}
```
阅读全文