//优点:将所有成员属性设置为私有:可以自己控制读写权限
//对于写权限,我们可以检测数据的有效性
#include<iostream>
using namespace std;
class Person
{
public:
void setName(string name)
{
m_Name = name;
}
string getName()
{
return m_Name;
}
int getAge()
{
//m_Age = 10;
return m_Age;
}
void setAge(int age)
{
if (age < 0||age>150)
{
m_Age = 0;
cout << "你这个老妖精" << endl;
return;
}
m_Age = age;
}
void setLover(string lover)
{
m_Lover = lover;
}
private:
string m_Name;//可读可写
int m_Age;//只读
string m_Lover;//只写
};
int main()
{
Person p;
p.setName("xian");
cout << "姓名:" << p.getName() << endl;
p.setAge(0);
cout << "年龄:" << p.getAge() << endl;
p.setLover("仓井");
system("pause");
return 0;
}
网友评论