继承方式
继承的语法: class 子类 :继承方式 父类
继承方式一共有三种:
1.公共继承
2.保护继承
3.私有继承
//继承方式
#include<iostream>
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 = 100;//父类中公共成员,到子类中变为公共权限
m_B = 100;//父类中保护成员,到子类中变为公共权限
//m_C = 100;//父类中私有成员 子类访问不到
}
};
class Base2
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son2:protected Base2
{
public:
void func()
{
m_A = 100;//父类中公共成员,到子类中变为保护权限
m_B = 100;//父类中保护成员,到子类中变为保护权限
//m_C = 100;//父类中私有成员 子类访问不到
}
};
class Base3
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son3:private Base3
{
public:
void func()
{
m_A = 100;//父类中公共成员,到子类中变为私有权限
m_B = 100;//父类中保护成员,到子类中变为私有权限
//m_C = 100;//父类的私有成员 子类访问不到
}
};
class Grandson3 :public Son3
{
public:
void func()
{
//m_A = 100;//Grandson3的父类为Son3,父类中成员都是私有成员,所以访问不到
//m_B = 100;//Grandson3的父类为Son3,父类中成员都是私有成员,所以访问不到
//m_C = 100;//Grandson3的父类为Son3,父类中成员都是私有成员,所以访问不到
}
};
int main()
{
return 0;
}