调用negative()取负成员函数前:5
调用negative()取负成员函数后:-5
#include<iostream>
using namespace std;
class person
{friend void operator -(person);
private:
int iapple;
public:
person(int iapple);//声明无参数构造符原型
void operator -();//声明成员变量取负数函数
void display();//声明显示成员变量函数
};
void operator -(person);
int main()
{person xiaowang(5);
cout<<"\n调用negative()取负成员函数前"<<endl;
xiaowang.display();
operator -(xiaowang);
cout<<"\n调用negative()取负成员函数后"<<endl;
xiaowang.display();
system("pause");
return 0;
}
person::person(int iapple)
{this->iapple=iapple;
}
void operator -(person p1)
{p1.iapple=-p1.iapple;
}
void person::display()
{cout<<"iapple="<<iapple<<endl;
}