C++有三种继承方式
- 公共继承
- 保护继承
- 私有继承
公共继承
公共继承是指保持最大权限进行继承,将继承过来的成员都变成自己的公共属性保护继承
保护继承指将继承过来的成员都变成自己的保护属性私有继承
私有继承指将继承过来的变量成为自己的私有属性
不论哪种继承,都相当与把继承方式作为一种宏观调整来保存过来。
class Father {
public:
int m_a;
protected:
int m_b;
private:
int m_c;
};
class son : public Father {
//相当于:
public:
public:
int m_a;
protected:
int m_b;
private:
int m_c;
};
#include <iostream>
#include <string>
using namespace std;
class Base1
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
//公共继承
class Son1 :public Base1
{
public:
void func()
{
m_A; //可访问 public权限
m_B; //可访问 protected权限
//m_C; //不可访问
}
};
void myClass()
{
Son1 s1;
s1.m_A; //其他类只能访问到公共权限
}
//保护继承
class Base2
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son2 :protected Base2
{
public:
void func()
{
m_A; //可访问 protected权限
m_B; //可访问 protected权限
//m_C; //不可访问
}
};
void myClass2()
{
Son2 s;
//s.m_A; //不可访问
}
//私有继承
class Base3
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son3 :private Base3
{
public:
void func()
{
m_A; //可访问 private权限
m_B; //可访问 private权限
//m_C; //不可访问
}
};
class GrandSon3 :public Son3
{
public:
void func()
{
//Son3是私有继承,所以继承Son3的属性在GrandSon3中都无法访问到
//m_A;
//m_B;
//m_C;
}
};