📄 程序10.06:负号运算符全局函数重载.cpp
字号:
/* 程序10.6:负号运算符全局函数重载.cpp:*/
#include<iostream> //包含头文件
using namespace std; //使用名字空间std
class Person //声明一个类Person
{
private:
int iApple; //声明私有成员变量
public:
Person(int iApple); //声明构造符函数
void operator -(); //声明负号运算符函数
void display(); //声明显示成员变量函数
};
int main() //main()函数开始
{
Person Xiaowang(5); //声明类对象Xiaowang,自动调用构造符
cout<<"\n调用operator -()负号运算符函数前"<<endl;
Xiaowang.display(); //调用显示成员变量函数
-Xiaowang; //等价于operator -(Xiaowang)
cout<<"\n调用operator -()负号运算符函数后"<<endl;
Xiaowang.display(); //调用显示成员变量函数
return 0;
} //main()函数结束
Person::Person(int iApple) //定义构造符函数
{
this->iApple=iApple;
}
void Person::operator -() //定义负号运算符函数
{
iApple=-iApple;
}
void Person::display() //定义显示成员变量函数
{
cout<<" iApple="<<iApple<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -