class person
{
public:
int age;
string name;
public:
person()
{
age = 30;
name = "vinny";
}
friend ostream& operator<<(ostream& os, person& per);//<<运算符重载
friend istream& operator>>(istream& is, person& per);//>>运算符重载
person& operator++()//重载前置++
{
age++;
return *this;
}
person& operator++(int)//重载后置++
{
person tmp;
tmp.age = this->age;
tmp.name = this->name;
age++;
return tmp;
}
person& operator--()//前置--重载
{
age--;
return *this;
}
person& operator--(int)//后置--重载
{
person tmp;
tmp.age = this->age;
tmp.name = this->name;
age--;
return tmp;
}
person& operator=(const person& per)//=运算符重载
{
age = per.age;
name = per.name;
return *this;
}
};
ostream& operator<<(ostream& os, person& per)//<<运算符重载
{
os << "age:" << per.age << "name:" << per.name;
return os;
}
istream& operator >> (istream& is, person& per)//>>运算符重载
{
is >> per.age >> per.name;
return is;
}
void test()
{
person per;
cin >> per;//>>运算符重载
cout << per.age << endl;//<<运算符重载
++per;//前置运算符重载
per++;//后置运算符重载
cout << per<<endl;
per--;//后置--重载
cout << per<<endl;
--per;//前置--重载
cout << per<<endl;
}
{
public:
int age;
string name;
public:
person()
{
age = 30;
name = "vinny";
}
friend ostream& operator<<(ostream& os, person& per);//<<运算符重载
friend istream& operator>>(istream& is, person& per);//>>运算符重载
person& operator++()//重载前置++
{
age++;
return *this;
}
person& operator++(int)//重载后置++
{
person tmp;
tmp.age = this->age;
tmp.name = this->name;
age++;
return tmp;
}
person& operator--()//前置--重载
{
age--;
return *this;
}
person& operator--(int)//后置--重载
{
person tmp;
tmp.age = this->age;
tmp.name = this->name;
age--;
return tmp;
}
person& operator=(const person& per)//=运算符重载
{
age = per.age;
name = per.name;
return *this;
}
};
ostream& operator<<(ostream& os, person& per)//<<运算符重载
{
os << "age:" << per.age << "name:" << per.name;
return os;
}
istream& operator >> (istream& is, person& per)//>>运算符重载
{
is >> per.age >> per.name;
return is;
}
void test()
{
person per;
cin >> per;//>>运算符重载
cout << per.age << endl;//<<运算符重载
++per;//前置运算符重载
per++;//后置运算符重载
cout << per<<endl;
per--;//后置--重载
cout << per<<endl;
--per;//前置--重载
cout << per<<endl;
}