为什么编译不了?哪里错了吗?

本文介绍了一个使用C++实现的矩形类,包括构造函数、成员变量、运算符重载等功能,并展示了如何通过实例化对象进行矩形的加减操作。
//#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文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值