继承的三类

继承的三类_第1张图片nice.来吧那就.nice

#include
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 = 10;//父类->子类
		m_B = 10;//父类保护,子类依然保护
		/*m_C = 10;*/  /*报错,不可访问.*/
	}
};
class Son2 :protected Base1
{
public:
	void func()
	{
		m_A = 10;
		m_B = 10;
		/*m_C = 10;*/  /*报错,不可访问.*/
	}
};
class Son3 :private Base1
{
public:
	void func()
	{
		m_A = 10;
		m_B = 10;
		/*m_C = 10;*/  /*报错,不可访问.*/
	}
};
void test01() {
	Son1 s1;
	s1.m_A = 100;
	/*s1.m_B = 100;*/  //报错,protected,类外访问不到
}
void test02() {
	Son2 s2;
	/*s1.m_A = 100;*/  //报错,protected继承,public编程protected
	/*s1.m_B = 100;*/  //报错,protected,类外访问不到
}
void test03() {
	Son3 s3;
/*	s1.m_A = 100;*//*  *///报错,private继承 私有成员 访问不到
	/*s1.m_B = 100;*/  //报错,protected,类外访问不到
}
class grandson :public Son3 {
public:
	void func() {
		/*m_A = 100;*/ //呵呵...
	}
};

这里博主就直接上代码了,有些注释掉的东西给他打开,自己试一下.

你可能感兴趣的:(c++,算法,开发语言)