# include <iostream> # include <string> using namespace std; class rect { private: double length; double width; public: rect(double length=0,double width=0); //rect(const rect&); //对象复制构造函可有可无 void output() { if(length<=0 || width<=0) cout<<"0,0"<<endl; else cout<<length<<','<<width<<endl; } void calculate1() { if(length<=0 || width<=0) cout<<'0'<<endl; else cout<<2*(length+width)<<endl; } void calculate2() { if(length<=0 || width<=0) cout<<'0'<<endl; else cout<<length*width<<endl; } void input() { cin>>length>>width; } }; rect::rect(double h,double w) { length=h; width=w; } //rect::rect(const rect&b) //{ // length=b.length; // width=b.width; //} int main() { rect r1(3.0,2.0); r1.input(); //注意调用成员函数的括号必须得写 cout<<"the length and width of r1 is:"; r1.output(); cout<<"the perimeter of r1 is:"; r1.calculate1(); cout<<"the area of r1 is:"; r1.calculate2(); rect r2(r1);//对象的复制。 cout<<"the length and width of r2 is:"; r2.output(); cout<<"the perimeter of r2 is:"; r2.calculate1(); cout<<"the area of r2 is:"; r2.calculate2(); return 0; }
相关文章: