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