//#include <iostream.h> //注意尖括号是表示嵌入的是标准C++库函数
#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<<"rect与rect1相加所得rect2:"<<endl;
rect2.Show();
rect2 = rect-rect1;
cout<<"rect减去rect1所得rect2:"<<endl;
rect2.Show();
cout<<"共产生矩行个数为:" <<Rectangle::Counter<< endl;
return 0;
}
// 将上述内容保存为Exp12_1.cpp文件
#include <iostream.h>
#include "rect.h"
int Rectangle:: Counter=0;
Rectangle::Rectangle(int l , int t, int r, int b) {
left = l; top = t;
right = r; bottom = b;
Counter++;
} // 构造函数,带缺省参数,缺省值为全0,在声明中指定
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;
}//当用.h头文件时,不用endl输出次序可能出错
void Rectangle:perator+=(Rectangle& rect){
int x = rect.right - rect.left;
int y = rect.bottom - rect.top;
right += x;
bottom += y;
}
void Rectangle:perator-=(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;
}
// 将上述内容保存为rect.cpp文件
class Rectangle {
int left, top ;
int right, bottom;
public:
Rectangle(int l=0, int t=0, int r=0, int b=0);
~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();
int Counter();
void operator+=(Rectangle&);
void operator-=(Rectangle&);
friend Rectangle operator+ (Rectangle &, Rectangle&);
friend Rectangle operator- (Rectangle &, Rectangle&);
};
//将上述内容保存为rect.h文件
#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<<"rect与rect1相加所得rect2:"<<endl;
rect2.Show();
rect2 = rect-rect1;
cout<<"rect减去rect1所得rect2:"<<endl;
rect2.Show();
cout<<"共产生矩行个数为:" <<Rectangle::Counter<< endl;
return 0;
}
// 将上述内容保存为Exp12_1.cpp文件
#include <iostream.h>
#include "rect.h"
int Rectangle:: Counter=0;
Rectangle::Rectangle(int l , int t, int r, int b) {
left = l; top = t;
right = r; bottom = b;
Counter++;
} // 构造函数,带缺省参数,缺省值为全0,在声明中指定
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;
}//当用.h头文件时,不用endl输出次序可能出错
void Rectangle:perator+=(Rectangle& rect){
int x = rect.right - rect.left;
int y = rect.bottom - rect.top;
right += x;
bottom += y;
}
void Rectangle:perator-=(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;
}
// 将上述内容保存为rect.cpp文件
class Rectangle {
int left, top ;
int right, bottom;
public:
Rectangle(int l=0, int t=0, int r=0, int b=0);
~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();
int Counter();
void operator+=(Rectangle&);
void operator-=(Rectangle&);
friend Rectangle operator+ (Rectangle &, Rectangle&);
friend Rectangle operator- (Rectangle &, Rectangle&);
};
//将上述内容保存为rect.h文件