c++学习笔记——private、public、protected关键字
初学c++,对private、public、protected三个关键字有很多困惑的地方,将网上和书上查到的资料记在此处,反复温习以免忘记。
一:
class FatherClass{
public:
int a;
int FatherClassFuncA(){ return d; };
private:
int b;
int FatherClassFuncB(){ return d; };
protected:
int d;
int FatherClassFuncC(){ return d; };
};
int main()
{
cout << "Hello World!" << endl;
FatherClass F;
// 调用类的public成员
F.FatherClassFuncA();
F.a = 1;
// 调用类的private成员
//F.FatherClassFuncB();
// F.b = 1;
// 调用类的protected成员
//F.FatherClassFuncC();
// F.d = 1;
return 0;
}
如上所示,对类FatherClass来说,其对象仅能够访问其public成员。
二:public继承
class ChildClassA :
public FatherClass{
public:
int a;
int ChildClassAFuncA(){
// public继承中的public成员调用父类中的public成员
FatherClassFuncA();
// public继承中的public成员调用父类中的private成员
//FatherClassFuncB();
// public继承中的public成员调用父类中的protected成员
FatherClassFuncC();
return a;
};
private:
int b;
int ChildClassAFuncB(){
// public继承中的private成员调用父类中的public成员
FatherClassFuncA();
// public继承中的private成员调用父类中的private成员
//FatherClassFuncB();
// public继承中的private成员调用父类中的protected成员
FatherClassFuncC();
return b;
};
protected:
int c;
int ChildClassAFuncC(){
// public继承中的protected成员调用父类中的public成员
FatherClassFuncA();
// public继承中的protected成员调用父类中的private成员
//FatherClassFuncB();
// public继承中的protected成员调用父类中的protected成员
FatherClassFuncC();
return c;
};
};
对于public继承而言,基类的public成员和protected成员可以被派生类访问。而派生类没有基类的private成员访问权限。
三:private继承。
class ChildClassB :
private FatherClass{
public:
int a;
int ChildClassBFuncA(){
// private继承中的public成员调用父类中的public成员
FatherClassFuncA();
// private继承中的public成员调用父类中的private成员
//FatherClassFuncB();
// private继承中的public成员调用父类中的protected成员
FatherClassFuncC();
return a;
};
private:
int b;
int ChildClassBFuncB(){
// private继承中的private成员调用父类中的public成员
FatherClassFuncA();
// private继承中的private成员调用父类中的private成员
//FatherClassFuncB();
// private继承中的private成员调用父类中的protected成员
FatherClassFuncC();
return b;
};
protected:
int c;
int ChildClassBFuncC(){
// private继承中的protected成员调用父类中的public成员
FatherClassFuncA();
// private继承中的protected成员调用父类中的private成员
//FatherClassFuncB();
// private继承中的protected成员调用父类中的protected成员
FatherClassFuncC();
return c;
};
};
对与private继承来说,派生类没有基类的private成员访问权限,而有public和proctected的访问权限。
四:protected继承:
class ChildClassC :
protected FatherClass{
public:
int a;
int ChildClassCFuncA(){
// protected继承中的public成员调用父类中的public成员
FatherClassFuncA();
// protected继承中的public成员调用父类中的private成员
//FatherClassFuncB();
// protected继承中的public成员调用父类中的protected成员
FatherClassFuncC();
return a;
};
private:
int b;
int ChildClassCFuncB(){
// protected继承中的private成员调用父类中的public成员
FatherClassFuncA();
// protected继承中的private成员调用父类中的private成员
//FatherClassFuncB();
// protected继承中的private成员调用父类中的protected成员
FatherClassFuncC();
return b;
};
protected:
int c;
int ChildClassCFuncC(){
// protected继承中的protected成员调用父类中的public成员
FatherClassFuncA();
// protected继承中的protected成员调用父类中的private成员
//FatherClassFuncB();
// protected继承中的protected成员调用父类中的protected成员
FatherClassFuncC();
return c;
};
};
同样,protected继承的派生类方法能访问基类的public、protected成员。
所以,1.private成员只能被本类成员(类内)和友元访问,不能被派生类访问;2.protected成员可以被派生类访问。
五:派生类调用基类的成员
ChildClassA A;
ChildClassB B;
ChildClassC C;
// public继承的派生类对象调用基类的public成员
A.a = 1;
// public继承的派生类对象调用基类的private成员
// A.b = 2;
// public继承的派生类对象调用基类的protected成员
// A.d = 3;
// public继承的派生类对象调用基类的public成员
// B.a = 1;
// public继承的派生类对象调用基类的private成员
// B.b = 2;
// public继承的派生类对象调用基类的protected成员
// B.d = 3;
// public继承的派生类对象调用基类的public成员
// C.a = 1;
// public继承的派生类对象调用基类的private成员
// C.b = 2;
// public继承的派生类对象调用基类的protected成员
// C.d = 3;
1.public继承:基类public成员,protected成员,private成员的访问属性在派生类中分别变成:public, protected, private
2.protected继承:基类public成员,protected成员,private成员的访问属性在派生类中分别变成:protected, protected, private
3.private继承:基类public成员,protected成员,private成员的访问属性在派生类中分别变成:private, private, private
所以只有public继承的派生类对象能够直接访问其基类的public成员。

浙公网安备 33010602011771号