/*1.请定义一个矩形类(Rectangle),私有数据成员为矩形的长度( len)和宽度(wid), 缺省构造函数置len和wid为0,有参构造函数置len和wid为对应形参的值, 另外还包括求矩形周长、求矩形面积、取矩形长度和宽度、修改矩形长度和宽度为对应形参的值、 输出矩形尺寸等公有成员函数。要求输出矩形尺寸的格式为“length:长度,width:宽度”。 编写主函数对定义的类进行测试。*/ #include<iostream> using namespace std; class Rectangle { private: float len,wid; public: Rectangle(); Rectangle(float i,float j); float area(); float perimeter(); ~Rectangle(); void setlenwid(float a,float b); void outlenwid(); }; Rectangle::Rectangle(float i,float j) { len = i; wid = j; } Rectangle::Rectangle() { len = 0; wid = 0; } Rectangle::~Rectangle() { } void Rectangle::setlenwid(float a,float b) { len = a; wid = b; } void Rectangle::outlenwid() { cout << "length:" << len << " " << "width" << wid << endl; } float Rectangle::area() { return len * wid; } float Rectangle::perimeter() { return 2*(len + wid); } int main() { Rectangle A1; Rectangle A2(4,5); cout << A1.perimeter() << endl; cout << A2.perimeter() << endl; cout << A1.area() << endl; cout << A2.area() << endl; A2.outlenwid(); A2.setlenwid(5,6); cout << A2.perimeter() << endl; cout << A2.area() << endl; A2.outlenwid(); return 0; }
类与对象1.1
猜你喜欢
转载自blog.csdn.net/qq_38053395/article/details/79997770
今日推荐
周排行