C++类中每个成员都有自己的访问修饰符,可以是三种情况之一:
- private:私有的,只有当前类内才有权限访问这个成员。
- protected:受保护的,类内和子类有权限访问这个成员。
- public:公共的,所有位置都有权限访问这个成员。
- 类内的默认访问修饰符为
private
。
#include<bits/stdc++.h>
using namespace std;
class people
{
private:
double weight;
protected:
double wealth;
public:
string name;
int age;
void smoking();
};
void people::smoking()
{
cout<<"smoking"<<endl;
weight=1;
wealth=1;
}
int main()
{
people p;
cin>>p.name>>p.age;
cout<<p.name<<' '<<p.age<<endl;
return 0;
}