#include "stdafx.h"
#include <iostream>
using namespace std;
//继承方式:
//一共有三种:公有继承,私有继承,保护继承;
//
class Base1
{
public:
int m_i;
private:
int m_j;
protected:
int m_k;
};
//公有继承:
class Son1 :public Base1
{
public:
void func()
{
//cout << m_j << endl;//m_j提示不可访问,基类中私有属性不可访问;
cout << m_i << endl;//可以访问,继承的仍然是public属性;
cout << m_k << endl;//可以访问,继承后是protected属性;
}
};
void myfunc01()
{
Son1 s1;
s1.m_i;
//s1.m_k;//protected属性类外访问不到;
}
//保护继承//************************************
class Base2
{
public:
int m_i;
private:
int m_j;
protected:
int m_k;
};
class Son2:protected Base2
{
public:
void func2()
{
//cout << m_j << endl;//基类中的私有属性,不可继承;
cout << m_k << endl;//基类中的protected属性,可继承,还是protected属性;
cout << m_i << endl;//基类中的公共属性,可继承,但是是protected属性;
}
};
void myfunc02()
{
Son2 s2;
//s2.m_i;//m_i现在是保护属性,所以也不能访问;
}
//私有继承//************************************
class Base3
{
public:
int m_i;
private:
int m_j;
protected:
int m_k;
};
class Son3 :private Base3
{
public:
void func3()
{
//cout << m_j << endl;//基类中的私有属性,不可继承;
cout << m_k << endl;//基类中的protected属性,可继承,还是protected属性;
cout << m_i << endl;//基类中的公共属性,可继承,但是是protected属性;
}
};
class Grandson :public Son3
{
public:
void func4()
{
//cout << m_i << endl;
//cout << m_k << endl;
//均不能访问,因为在Son3中都是私有权限;
}
};
void myfunc03()
{
Son3 s3;
//s3.m_i;//m_i现在是保护属性,所以也不能访问;
}
int main()
{
return 0;
}