Lsimos😈 牛奶lmy
关注数: 10 粉丝数: 112 发帖数: 1,109 关注贴吧数: 20
求大神解答 #include<iostream> using namespace std; class Rectangle { int left,top; int right,bottom; private: static int counter; //注意私有性 public: Rectangle() { ++counter;cout<<"对象数量="<<counter<<'\n'; } ~Rectangle() { --counter;cout<<"对象数量="<<counter<<'\n'; } Rectangle(int l=0,int t=0,int r=0,int b=0); //构造函数,带默认参数,默认值全0 void Assign(int l,int t,int r,int b); void SetLeft(int u) {left=u;} //以下4个函数均为内联成员函数 void SetRight(int u) {right=u;} void SetTop(int u) {top=u;} void SetBottom(int u) {bottom=u;} //注意分号在花括号里面 void Show(); Rectangle operator+=(Rectangle &); Rectangle operator-=(Rectangle &); Rectangle operator+(Rectangle &); Rectangle operator-(Rectangle &); }; int Rectangle::counter=0; //对静态数据成员的定义性说明 Rectangle::Rectangle(int l,int t,int r,int b) /*带默认参数的构造函数,声明中 已经指定默认值为0*/ { left=l;top=t; right=r;bottom=b; } void Rectangle::Assign(int l,int t,int r,int b) { left=l;top=t; right=r;bottom=b; } void Rectangle::Show() { cout<<"left-top point is ("<<left<<","<<top<<")"<<'\n'; cout<<"right-bottom point is ("<<right<<","<<bottom<<")"<<endl; } Rectangle Rectangle::operator+=(Rectangle & rect) { int x=rect.right-rect.left; int y=rect.bottom-rect.top; right+=x; bottom+=y; return Rectangle(left,right,top,bottom); } Rectangle Rectangle::operator-=(Rectangle & rect) { int x=rect.right-rect.left; int y=rect.bottom-rect.top; right-=x; bottom-=y; return Rectangle(left,right,top,bottom); } Rectangle Rectangle::operator+(Rectangle & rect) { return Rectangle(left+rect.left,right+rect.right,top+rect.top,bottom+rect.bottom); } Rectangle Rectangle::operator-(Rectangle & rect) { return Rectangle(left-rect.left,right-rect.right,top-rect.top,bottom-rect.bottom); } int main() { Rectangle rect(0,0,0,0); //默认值全0 cout<<"初始的rect:"<<endl; rect.Show(); rect.Assign(100,200,300,400); cout<<"赋值后的rect:"<<endl; rect.Show(); Rectangle rect1(0,0,200,200); cout<<"初始的rect1:"<<endl; rect1.Show(); rect+=rect1; cout<<"与rect1相加后的rect:"<<endl; rect.Show(); rect-=rect1; cout<<"减去rect1后的rect:"<<endl; rect.Show(); Rectangle rect2(200,100,200,100); rect2=rect+rect1; cout<<"rect与rect1相加所得的rect2:"<<endl; rect2.Show(); rect2=rect-rect1; cout<<"rect减去rect1所得的rect2:"<<endl; rect2.Show(); Rectangle count[4]; return 0; } 为什么运行之后对象数量为负数
1 下一页