求大神。内容:C++程序设计:运算符重载
#include<iostream.h>
//using namespace std;
class Tpoint{
private:int x,y;
public:
    /*Tpoint(){
       x=1;
       y=1;
       cout<<"default constructor"<<endl;
    }*/
    Tpoint(int a=0,int b=0){
       x=a;y=b;
       cout<<"constructor,x="<<x<<"y="<<y<<endl;
    }
    Tpoint(const Tpoint&c ) {
        x = c.x;
        y = c.y;
        cout<<"copy constructor,x="<<x<<"y="<<y<<endl;
    }
    ~Tpoint(){
    cout<<"delete,x="<<x<<"y="<<y<<endl;
    }
    Tpoint operator+(const Tpoint& p){
        Tpoint temp;
        temp.x=x+p.x;
        temp.y=y+p.y;
        return temp;
    }
    Tpoint operator++(int){
        Tpoint before(x,y);
        x++;
        y++;
        return before;
    }
    Tpoint operator++(){
        x++;
        y++;
        return *this;
    }
    bool operator==(const Tpoint& p){
        if((x==p.x)&&(y==p.y))
            return 1;
        else 
            return 0;
    }
friend Tpoint operator-(Tpoint &p,Tpoint &q);
friend Tpoint operator--(Tpoint &p);
friend Tpoint operator--(Tpoint &p,int);
friend ostream& operator<<(ostream &out,Tpoint &p);
friend istream operator>>(istream &in,Tpoint &p);
};
Tpoint operator-(Tpoint &p,Tpoint &q)
{
    Tpoint temp;
    temp.x=p.x-q.x;
    temp.y=p.y-q.y;
    return temp;
}
Tpoint operator--(Tpoint &p)
{
    p.x--;
    p.y--;
    return p;
}
Tpoint operator--(Tpoint &p,int)
{
    Tpoint before(p.x,p.y);
    p.x--;
    p.y--;
    return before;
}
ostream& operator<<(ostream &out,Tpoint &p)
{
    out<<"("<<p.x<<","<<p.y<<")"<<endl;
    return out;
}
void main(){
    Tpoint p1(1,1),p2(3);
    Tpoint p3=p1+p2;
    cout<<(p1==p2)<<endl;
    cout<<p1<<endl;
    ++p1;
    --p1;
    Tpoint p4=p1-p2;

}

图片说明

c++

1个回答

重载为成员函数和友元函数的区别:成员函数有this指针,友元函数没有this指针。成员函数可以继承,友元函数不能继承。

为什么<<和>>运算符只能重载为友元函数:因为定义为成员函数,就隐含this指针作为第一个参数。而调用<<和>>的
对象是cin或cout,所以把它重载为友元才好。

调整了你的代码格式

#include<iostream>
using namespace std;

class Tpoint{ 
private:
    int x,y; 
public: 
    /*Tpoint(){ x=1; y=1; cout<<"default constructor"<<endl; }*/ 
    Tpoint(int a=0,int b=0){ 
        x=a;y=b; 
        cout<<"constructor,x="<<x<<"y="<<y<<endl; 
    } 

    Tpoint(const Tpoint&c ) { 
        x = c.x; y = c.y; 
        cout<<"copy constructor,x="<<x<<"y="<<y<<endl; 
    }

    ~Tpoint(){
        cout<<"delete,x="<<x<<"y="<<y<<endl; 
    }

    Tpoint operator + (const Tpoint& p){ 
        Tpoint temp;
        temp.x=x+p.x; 
        temp.y=y+p.y;
        return temp; 
    }

    Tpoint operator ++ (int){ 
        Tpoint before(x,y);
        x++;
        y++;
        return before; 
    }

    Tpoint operator ++ (){ 
        x++;
        y++;
        return *this;
    }

    bool operator == (const Tpoint& p){ 
        if((x==p.x)&&(y==p.y))
            return 1;
        else 
            return 0; 
    }

    friend Tpoint operator-(Tpoint &p,Tpoint &q);
    friend Tpoint operator--(Tpoint &p);
    friend Tpoint operator--(Tpoint &p,int);
    friend ostream& operator<<(ostream &out,Tpoint &p);
    friend istream operator>>(istream &in,Tpoint &p);
};

Tpoint operator-(Tpoint &p,Tpoint &q) {
    Tpoint temp;
    temp.x=p.x-q.x;
    temp.y=p.y-q.y;
    return temp;
} 

Tpoint operator--(Tpoint &p) { 
    p.x--;
    p.y--;
    return p;
} 

Tpoint operator--(Tpoint &p,int) { 
    Tpoint before(p.x,p.y); 
    p.x--;
    p.y--;
    return before;
}

ostream& operator<<(ostream &out,Tpoint &p) {
    out<<"("<<p.x<<","<<p.y<<")"<<endl;
    return out;
} 

int main(){
    Tpoint p1(1,1),p2(3);
    Tpoint p3=p1+p2;
    cout<<(p1==p2)<<endl;
    cout<<p1<<endl;
    ++p1;
    --p1;
    Tpoint p4=p1-p2;
    return 0;
} 
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问