类访问顺序

上一篇 / 下一篇  2015-09-25 16:37:00 / 个人分类:c++


#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
class father
{
public:
void b(){
prii();
}
protected:
void pri(){
//cout << "父类受保护"<<endl;
printf("%s\n","父类受保护");
}
private:
void prii(){
cout << "父类私有"<<endl;
}
};

class son : public father
{

public:
void d()
{
//prii();
}
public:
void c(){
pri();
}
};



son a;
a.c();

//a.prii();
system("pause");
return 0;
}


结论:父类的成员如果无论是protected还是privat,在子类中都不能在类外访问(即通过定义一个对象实例访问),但是在子类中可以直接访问在父类中时protected的成员,不能直接访问private的成员(值能通过父类公有成员函数访问)

TAG:

 

评分:0

我来说两句

Open Toolbar