[Lang] 类成员修饰符
1. 访问
public
成员可以被任何代码访问,包括类的外部和派生类。protected
成员不能被类的外部直接访问,但可以被该类的派生类访问。private
成员只能在类内部访问,不能被类的外部或派生类直接访问。
2. 继承
public
继承:基类public,protected,private在派生类中分别变成:public, protected, privateprotected
继承:基类public,protected,private在派生类中分别变成:protected, protected, privateprivate
继承:基类public,protected,private在派生类中分别变成:private, private, private
3. 示例
public继承:
#include<iostream>
using namespace std;
class ParentClass
{
public:
int x;
protected:
int y;
private:
int z;
};
class ChildClass : public ParentClass // x-public, y-protected, z-private
{
public:
void fun()
{
cout << "x = " << x << endl;
cout << "y = " << y << endl;
// cout << "z = " << z << endl; // error: 'z' is a private member of 'ParentClass'
}
};
class GrandChildClass : public ChildClass // x-public, y-protected, z-private
{
public:
void fun()
{
cout << "x = " << x << endl;
cout << "y = " << y << endl;
// cout << "z = " << z << endl; // error: 'z' is a private member of 'ChildClass'
}
};
int main()
{
ChildClass obj;
obj.x = 10;
// obj.y = 20; // error: 'y' is a protected member of 'ChildClass'
// obj.z = 30; // error: 'z' is a private member of 'ChildClass'
return 0;
}
protected继承:
#include<iostream>
using namespace std;
class ParentClass
{
public:
int x;
protected:
int y;
private:
int z;
};
class ChildClass : protected ParentClass // x-protected, y-protected, z-private
{
public:
void fun()
{
cout << "x = " << x << endl;
cout << "y = " << y << endl;
// cout << "z = " << z << endl; // error: 'z' is a private member of 'ParentClass'
}
};
class GrandChildClass : protected ChildClass // x-protected, y-protected, z-private
{
public:
void fun()
{
cout << "x = " << x << endl;
cout << "y = " << y << endl;
// cout << "z = " << z << endl; // error: 'z' is a private member of 'ChildClass'
}
};
int main()
{
ChildClass obj;
// obj.x = 10; // error: 'x' is a protected member of 'ChildClass'
// obj.y = 20; // error: 'y' is a protected member of 'ChildClass'
// obj.z = 30; // error: 'z' is a private member of 'ChildClass'
return 0;
}
private继承:
#include<iostream>
using namespace std;
class ParentClass
{
public:
int x;
protected:
int y;
private:
int z;
};
class ChildClass : private ParentClass // x-private, y-private, z-private
{
public:
void fun()
{
cout << "x = " << x << endl;
cout << "y = " << y << endl;
// cout << "z = " << z << endl; // error: 'z' is a private member of 'ParentClass'
}
};
class GrandChildClass : private ChildClass // x-private, y-private, z-private
{
public:
void fun()
{
// cout << "x = " << x << endl; // error: 'x' is a private member of 'ChildClass'
// cout << "y = " << y << endl; // error: 'y' is a protected member of 'ParentClass'
// cout << "z = " << z << endl; // error: 'z' is a private member of 'ChildClass'
}
};
int main()
{
ChildClass obj;
// obj.x = 10; // error: 'x' is a private member of 'ChildClass'
// obj.y = 20; // error: 'y' is a private member of 'ChildClass'
// obj.z = 30; // error: 'z' is a private member of 'ChildClass'
return 0;
}
标签:Lang,int,成员,修饰符,private,protected,派生类,ParentClass,public
From: https://www.cnblogs.com/yaoguyuan/p/18355932