#include<iostream.h>
class point
{
public:
int x,y;
point operator ++(); //自加
point operator ++(int); //
point operator --(); //自减
point operator --(int); //
point operator + (point &a);
point operator - (point &a);
friend ostream& operator << (ostream &,point &);
friend istream& operator << (istream &,point &);
point (int a=0,int b=0)
{
x=a;y=b;
}
};
ostream& operator << (ostream & out ,point & c)
{
out<<"("<<c.x<<","<<c.y<<")"<<endl;
return out;
}
istream& operator >> (istream & ina ,point & c)
{
cout<<"输入坐标:";
ina>>c.x>>c.y;
return ina;
}
point point::operator +(point &a)
{
return point(x+a.x,y+a.y);
}
point point::operator -(point &a)
{
return point(x-a.x,y-a.y);
}
point point::operator ++ () //前置
{
x++;
y++;
return *this;
}
point point::operator ++(int) //hou置
{
point temp(*this);
x++;
y++;
return temp;
}
point point::operator -- () //前置
{
x--;
y--;
return *this;
}
point point::operator --(int) //hou置
{
point temp(*this);
x--;
y--;
return temp;
}
int main()
{
point t1,t2,t3;
cin>>t1>>t2;
cout<<"t1="<<t1;
cout<<"t2="<<t2;
cout<<"t1++="<<++t1;
cout<<"t1--="<<--t1;
cout<<"两坐标之和为:";
t3=t1+t2;
cout<<t3;
cout<<t3.x<<t3.y;
return 0;
}
c++ 对象坐标相加重载
最新推荐文章于 2022-10-07 21:26:22 发布