protect继承

实例:

#include <iostream>
using namespace std;
class parent{
public:
	void print(void)
	{
		//cout << "a=:" << a << "    b=:" << b <<"    c=:"<< d << endl;
	}
	int a;
private:
	int b;
protected:
	int d;
};
#if 0
class child1 :public parent
{
public:
	void SetVar(int _a, int _b, int _c)
	{
		a = _a;
		//b = _b;//不可以访问,因为是基类的私有属性
		d = _c;//可以在子类(派生类)中使用,但也仅限于在类中,不可以再类外使用
	}
private:
protected:
};
#endif

/*注释*/
#if 0
class child2: private parent
{
	void SetVar(int _a, int _b, int _c)
	{
		a = _a;
		//b = _b;//不可以访问,因为是基类的私有属性
		d = _c;//可以在子类(派生类)中使用,但也仅限于在类中,不可以再类外使用
	}
public:
private:
protected:
};
#endif
class child3 :protected parent
{
	void SetVar(int _a, int _b, int _c)
	{
		a = _a;//a在基类中的访问属性是public,在protected继承中变成protect属性,所以仅限于在基类和当前子类中使用不可以再类的外部使用
		//b = _b;//不可以访问,因为是基类的私有属性
		d = _c;//可以在子类(派生类)中使用,但也仅限于在类中,不可以再类外使用
	}
public:
private:
protected:
};
int main()
{
	child3 oop;
	/*这三种方式都不可以使用,因为在子类中的集成方式为protect,所以,a在派生类中的访问属性成为protected的了,
	b的原属性为private,所以在protected中任然是private属性
	d在基类的属性为protect,所以在protected集成中任然是protected,在类的外部不可以使用
	*/
	//oop.a = 100;
	//oop.b = 1000;
	//oop.d=10000  
	system("pause");
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值