#include<bits/stdc++.h> using namespace std; class Person { public: //姓名设置可读可写 void setName(string name) { m_Name = name; } string getName() { return m_Name; } //获取年龄 int getAge() { return m_Age; } //设置年龄 void setAge(int age) { if (age < 0 || age > 150) { 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("张三"); cout << "姓名: " << p.getName() << endl; //年龄设置 p.setAge(-8); cout << "年龄: " << p.getAge() << endl; //情人设置 p.setLover("苍井"); //cout << "情人: " << p.m_Lover << endl; //只写属性,不可以读取 system("pause"); return 0; }