public,private,protected的区别,继承方法与访问权限

访问范围:

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值