#include "stdafx.h"
#include <iostream>
using namespace std;
//类内部的private protected public 影响访问权限
//继承中 private protected public,
//影响父类中的成员在子类中的访问权限,不影响子类现有的成员访问方式
//99% 只使用public继承方式
#if 0
public protected private
pub pub pro pri
pro pro pro pri
pri inaccess inaccess inaccess
#endif
//public 提供接口用的
//protected 隐藏数据,传承数据
//private 隐藏数据
//继承方式总结
//public 传承接口,也传承了数据
//protected 传承了数据
//private 既没有传承接口,也没有传承数据
class Base
{
public:
int pub;
protected:
int pro;
private:
int pri;
};
class Derive :public Base
{
public:
int x;
void func()
{
pub = 100;
pro = 200;
//pri=200; pri不可见
}
protected:
int y;
private:
int z;
};
int _tmain(int argc, _TCHAR* argv[])
{
Derive d;
d.x = 100;
return 0;
}
私有继承 private:
私有继承父类之后,父类中的成员函数在子类中就为私有成员函数了(前提是该函数在父类中访问权限不是private),不能在类外部用类对象的形式去调用,但是成员函数内部是可以调用的,也就是说通过私有继承,子类“隐藏”了父类的接口
class A
{
public: //或者protected
void fun()
{
cout << "ok" << endl;
}
};
class B : private A
{
public:
void foo()
{
fun();
}
};
int main()
{
B b;
b.foo(); //可以调用foo进而使用fun函数
//b.fun(); 而不能在类外对象直接调用fun函数
}