#include <iostream>
using namespace std;
class people {
// friend ostream& operator <<(ostream& out, const people p1);
/* friend people& operator ++(people& p1);
friend people& operator ++(people& p1, int wuyong);*/// 什么时候加上友元函数声明 比如需要访问类中私有类成员 否则不用添加
public:
int age;
int num;
people(int age,int num)
{
this->num = num;
this->age = age;
}
~people() {
}
people& operator ++() {
(this->age)++;
(this->num)++;
return *this;
}
};
ostream& operator <<(ostream &out, const people& p1)//输出运算符重载
{
out << "p1的值" << p1.age << endl;
return out;
}
istream& operator >>(istream& ifs, people& p1) {//重载输入运算符
ifs >> p1.age;
ifs >> p1.num;
return ifs;
}
people& operator ++(people& p1) {//前置++数重载据类型利用非成员函数实现
p1.age++;
p1.num++;
return p1;
}
people& operator ++(people& p1, int wuyong) {//后置++运算符重载利用非成员函数重载
people temp(p1.age, p1.num);
p1.age++;
p1.num++;
return temp;
}
people& operator --(people& p1, int wuyong) {//后置--运算符重载利用非成员函数重载
people temp(p1.age, p1.num);
p1.age--;
p1.num--;
return temp;
}
int main()
{
people p1(2,3);
cin >> p1;
cout << p1.age << endl;
}
|