/*用友元函数重载常用运算符*/
#include<iostream>
#include<fstream>
using namespace std;
class Person
{
int a,b,c;
public:
Person(int i=0,int j=0,int k=0)
{
a=i;
b=j;
c=k;
}
friend istream & operator>>(istream & input,Person & p);
friend ostream & operator<<(ostream & output,Person & p);
//friend Person & operator+=(Person & a1,Person & a2);//重载+=
friend Person & operator++(Person & a1);//前置
friend Person & operator++(Person & a1,int);//后置
friend Person & operator-(Person & a1);//重载-运算符
};
istream &operator >>(istream & input,Person & p)//重载输入运算符
{
input>>p.a>>p.b>>p.c;
return input;
}
ostream & operator <<(ostream & output,Person & p)//重载输出运算符
{
output<<p.a<<p.b<<p.c;
return output;
}
/*Person & operator+=(Person &a1,Person &a2)//重载单目运算符+=
{
a1.a=a1.a+a2.a;
a1.b=a1.b+a2.b;
a1.c=a1.c+a2.c;
return a1;
}*/
/*Person & operator++(Person & a1)//前置
{
a1.a++;
a1.b++;
a1.c++;
return a1;
}
Person & operator++(Person & a1,int )//后置
{
Person a2(a1);
a2.a++;
a2.b++;
a2.c++;
return a2;
}*/
Person & operator-(Person & a1)//重载-运算符
{
a1.a=-a1.a;
a1.b=-a1.b;
a1.c=-a1.c;
return a1;
}
int main()
{
Person a(1,2,3),b;
cin>>b;
b=-b;
cout<<b<<endl;
system("pause");
return 0;
}
#include<iostream>
using namespace std;//输入输出运算符只能用友元函数重载
class Person
{
int a,b,c;
public:
Person(int i=0,int j=0,int k=0)
{
a=i;
b=j;
c=k;
}
friend istream & operator>>(istream & input,Person & p);
friend ostream & operator<<(ostream & output,Person & p);
/*Person & operator+(Person & a1)//成员函数重载+
{
Person temp;
temp.a=a+a1.a;
temp.b=b+a1.b;
temp.c=c+a1.c;
return temp;
}
*/
/* Person & operator +=(Person & a1)
{
a=a+a1.a;
b=b+a1.b;
c=c+a1.c;
return (*this);
}*/
Person & operator++()
{
a++;
b++;
c++;
return(*this);
}
Person & operator++(int)//+后置
{
Person temp(*this);
a++;
b++;
c++;
return temp;
}
};
istream &operator >>(istream & input,Person & p)//重载输入运算符
{
input>>p.a>>p.b>>p.c;
return input;
}
ostream & operator <<(ostream & output,Person & p)//重载输出运算符
{
output<<p.a<<p.b<<p.c;
return output;
}
int main()
{
Person a(1,2,3),b,c;
cin>>b;
c=++b;
cout<<c<<endl;
c=b++;
cout<<c<<endl;
system("pause");
return 0;
}