## C++类定义与类构造，重载

Echo1 发布于 2014/04/03 21:37

（1）将以下内容保存为rect.h

class Rectangle;

Rectangle operator+(Rectangle &, Rectangle&);

Rectangle operator-(Rectangle &, Rectangle&);

//-----------------------------------------------------------------------

class Rectangle {

int left, top ;

int right, bottom;

public:

Rectangle(int l=0, int t=0, int r=0, int b=0);

Rectangle(Rectangle &);                            //"拷贝构造"，用于对象复制

~Rectangle();                                     //析构函数

void Assign(int l, int t, int r, int b);

void SetLeft(int t){ left = t;}               //以下四个函数皆为内联成员函数

void SetRight(int t ){ right = t;}

void SetTop(int t ){ top = t;}

void SetBottom(int t ){ bottom = t;}

void Show();

void operator+=(Rectangle&);

void operator-=(Rectangle&);

friend Rectangle operator+(Rectangle &, Rectangle&);

friend Rectangle operator-(Rectangle &, Rectangle&);

};

（2）将以下内容保存为rect.cpp

#include <iostream>

using namespace std;

#include "rect.h"

//-----------------------------------------------------------------------

Rectangle::Rectangle(int l , int t, int r, int b) {

left = l; top = t;

right = r; bottom = b;

} //构造函数，带缺省参数，缺省值为全0，在类的声明中指定

Rectangle::Rectangle(Rectangle &x) {

left = x.left; top = x.top;

right = x.right; bottom = x.bottom;

} //拷贝构造函数，用于"对象复制"

Rectangle::~Rectangle() {

} //析构函数

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;

}

void Rectangle::operator+=(Rectangle& rect){

int x = rect.right - rect.left;

int y = rect.bottom - rect.top;

right += x;

bottom += y;

}

void Rectangle::operator-=(Rectangle& rect){

int x = rect.right - rect.left;

int y = rect.bottom - rect.top;

right -= x;

bottom -= y;

}

Rectangle operator-( Rectangle &rect1, Rectangle& rect2){

//矩形相减，从rect1中减去rect2的长度和宽度

rect1 -= rect2;

return rect1;

}

Rectangle operator+( Rectangle &rect1, Rectangle& rect2){

//矩形相加，从rect1中加上rect2的长度和宽度

rect1 += rect2;

return rect1;

}

（3）将以下内容保存为main.cpp

#include <iostream>

using namespace std;

#include "rect.h"

//-----------------------------------------------------------------------

int main(){

Rectangle rect;

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;

rect2 = rect + rect1;

cout<<"rectrect1相加所得rect2:"<<endl;

rect2.Show();

rect2 = rect - rect1;

cout<<"rect减去rect1所得rect2:"<<endl;

rect2.Show();

return 0;

}

1.        在上面程序的基础上，为矩形类增加一个静态数据成员Counter，用来统计产生了多少个矩形对象。为Counter做如下工作：（1）添加定义；（2）赋初值；（3）在构造函数中累加，并输出“现有矩形数为：”和Counter值；（4）在析构函数中自减，并输出“现有矩形数为：”和Counter值。注意静态数据成员应该有的定义性说明。

int main(){

Rectangle rect;

rect.Show();

rect.Assign(100, 200, 300, 400);

rect.Show();

cout<<endl;

Rectangle rect1(0, 0, 200, 200);

rect1.Show();

rect += rect1;

rect.Show();

rect -= rect1;

rect.Show();

cout<<endl;

Rectangle rect2;

rect2 = rect + rect1;    //注意此处求和时会创建一个临时对象

rect2.Show();

cout<<endl;

rect2 = rect - rect1;     //注意此处求差时会创建一个临时对象

rect2.Show();

cout<<endl;

return 0;

}