#include<iostream> #include<iomanip> using namespace std; class Rectangle { public: Rectangle(double length = 2.0, double width = 1.0); Rectangle(const Rectangle& obj); ~Rectangle() = default; double len()const; double wide()const; double area()const; double circumference()const; void resize(double times); void resize(double times, double w_times); private: double _length; double _width; }; Rectangle::Rectangle(double length, double width) { _length = length; _width = width; } Rectangle::Rectangle(const Rectangle& obj) { _length = obj._length; _width = obj._width; } double Rectangle::len()const { return _length; } double Rectangle::wide()const { return _width; } double Rectangle::area()const { return _length * _width; } double Rectangle::circumference()const { return _length * 2 + _width * 2; } void Rectangle::resize(double times) { _length *=times; _width *=times; } void Rectangle::resize(double times, double w_times) { _length*=times; _width *=w_times; } void output(const Rectangle& rect) { cout << "矩形信息:\n"; cout << fixed << setprecision(2); cout << "长: " << rect.len() << endl; cout << "宽: " << rect.wide() << endl; cout << "面积: " << rect.area() << endl; cout << "周长: " << rect.circumference() << endl<<endl; } int main() { Rectangle rect1; output(rect1); Rectangle rect2(10, 5); output(rect2); Rectangle rect3(rect1); rect3.resize(2); output(rect3); rect3.resize(5, 2); output(rect3); system("pause"); return 0; }
标签:const,double,length,times,width,任务,实验,Rectangle From: https://www.cnblogs.com/xelfin/p/16739166.html