访问范围:
private: 只能由该类中的函数、其友元函数访问,不能被任何其他访问,该类的对象也不能访问.
protected: 可以被该类中的函数、子类的函数、以及其友元函数访问,但不能被该类的对象访问
public: 可以被该类中的函数、子类的函数、其友元函数访问,也可以由该类的对象访问
注:友元函数包括两种:设为友元的全局函数,设为友元类中的成员函数
父类与其直接子类的访问关系如上,无论是哪种继承方式(private继承、protected继承、public继承)。
对于三种继承关系的不同:
public继承:public继承后,从父类继承来的函数属性不变(private、public、protected属性不变,)。
private继承:private继承后,从父类继承来的函数属性都变为private
protected继承:protected继承后,从父类继承过来的函数,public、protected属性变为protected,private还是private。
代码:定义了一个父类CFather,三个子类(CSonPrivate、CSonPublic、CSonProtected),三个孙子类(CGrandsonFromPrivate、CGrandsonFromProtected、CGrandsonFromPublic)
父类CFather:
class CFather
{
public:
CFather();
virtual ~CFather();
void FuncFatherPublic()
{
printf("FuncFatherPublic\r\n");
}
protected:
void FuncFatherProtected()
{
printf("FuncFatherProtected\r\n");
}
private:
void FuncFatherPrivate()
{
printf("FuncFatherPrivate\r\n");
}
};
子类CSonPrivate:
class CSonPrivate : private CFather
{
public:
CSonPrivate();
virtual ~CSonPrivate();
void FuncSonPrivatePublic()
{
printf("FuncSonPrivatePublic\r\n");
}
void FuncSonTestCall()
{
FuncFatherPublic();
FuncFatherProtected();
//FuncFatherPrivate();//错误,父类私有函数不可调用
}
protected:
void FuncSonPrivateProtected()
{
printf("FuncSonPrivateProtected\r\n");
}
private:
void FuncSonPrivatePrivate()
{
printf("FuncSonPrivatePrivate\r\n");
}
};
子类CSonProtected:
#include "Father.h"
class CSonProtected : protected CFather
{
public:
CSonProtected();
virtual ~CSonProtected();
void FuncSonProtectedPublic()
{
printf("FuncSonProtectedPublic\r\n");
}
void FuncSonTestCall()
{
FuncFatherPublic();
FuncFatherProtected();
//FuncFatherPrivate();//错误,父类私有函数不可调用
}
protected:
void FuncSonProtectedProtected()
{
printf("FuncSonProtectedProtected\r\n");
}
private:
void FuncSonProtectedPrivate()
{
printf("FuncSonProtectedPrivate\r\n");
}
};
子类CSonPublic:
#include "Father.h"
class CSonPublic:public CFather
{
public:
CSonPublic();
virtual ~CSonPublic();
void FuncSonPublicPublic()
{
printf("FuncSonPublicPublic\r\n");
}
void FuncSonTestCall()
{
FuncFatherPublic();
FuncFatherProtected();
//FuncFatherPrivate();//错误,父类私有函数不可调用
}
protected:
void FuncSonPublicProtected()
{
printf("FuncSonPublicProtected\r\n");
}
private:
void FuncSonPublicPrivate()
{
printf("FuncSonPublicPrivate\r\n");
}
};
孙子类CGrandsonFromPrivate:
#include "SonPrivate.h"
class CGrandsonFromPrivate : public CSonPrivate
{
public:
CGrandsonFromPrivate();
virtual ~CGrandsonFromPrivate();
void FuncSonTestCall()
{
//FuncFatherPublic();//祖父类的公共函数被父类私有继承后属性变为private,因此也不能在外部或其子类中访问
//FuncFatherProtected();//祖父类的保护函数被父类私有继承后属性变为private,因此也不能在外部或其子类中访问
//FuncFatherPrivate();//祖父类的私有函数不可访问
}
};
孙子类CGrandsonFromProtected:
class CGrandsonFromProtected : public CSonProtected
{
public:
CGrandsonFromProtected();
virtual ~CGrandsonFromProtected();
void FuncSonTestCall()
{
FuncFatherPublic();//父类protected继承祖父类,祖父类的public函数属性变成protected
FuncFatherProtected();//父类protected继承祖父类,祖父类的protected函数属性还是protected
//FuncFatherPrivate();//祖父类的私有函数不可访问
}
};
孙子类CGrandsonFromPublic:
#include "SonPublic.h"
class CGrandsonFromPublic : public CSonPublic
{
public:
CGrandsonFromPublic();
virtual ~CGrandsonFromPublic();
void FuncSonTestCall()
{
FuncFatherPublic();//父类public继承祖父类,祖父类的public函数属性还是public
FuncFatherProtected();//父类public继承祖父类,祖父类的protected函数属性还是protected
//FuncFatherPrivate();//祖父类的私有函数不可访问
}
};
main函数:
int main(int argc, char* argv[])
{
CGrandsonFromProtected grandson_fromprotected;
//grandson_fromprotected.FuncFatherPublic();//错误,经过protected继承后,public变为protected
return 0;
}