# include<iostream>
# include<string>
using namespace std;
//继承的语法:class 子类;继承方式:父类
//继承方式有三种
//1、公共继承
class Base1
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son1 :public Base1
{
public:
void func()
{
m_A = 10; //父类中的公共权限 到子类中依然是公共权限
m_B = 10; //父类中的保护权限 到子类中依然是保护权限
//m_C = 10;//父类中的私有权限 到子类中访问不到
}
};
//2、保护继承
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 = 10;//父类中的私有权限 到子类中访问不到
}
};
//3、私有继承
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 = 10;//父类中的私有权限 到子类中访问不到
}
};
class GrandSon3 :public Son3
{
public:
void func()
{
//m_A = 100;
//m_B = 100;
}
};
void test01()
{
Son1 s1;
s1.m_A = 100;
//s1.m_B = 100; //到了Son1中是保护权限 类外访问不到
}
void test02()
{
Son2 s2;
//s2.m_A = 1000; 为保护属性 不可访问
//s2.m_B = 100; //到了Son1中是保护权限 类外访问不到
}
void test03()
{
GrandSon3 s3;
s3.func();
//s3.m_A = 1000; 为私有属性 不可访问
//s2.m_B = 100; //到了Son1中是保护权限 类外访问不到
}
int main()
{
test02();
system("pause");
return 0;
}