Public继承

#include<iostream>
using namespace std;

class ClassA
{
public:
	int x;
	void PublicShow();

	ClassA(int x = 1, int y = 2, int z = 3);
	~ClassA();

protected:
	int y;
	void ProtectedShow();

private:
	int z;
	void PrivateShow();
};

class ClassB:public ClassA
{
public:
	int a;
	void PublicShow();

	ClassB(int a = 3, int b = 4, int c = 5, int x = 1, int y = 2, int z = 3);
	~ClassB();

protected:
	int b;
	void ProtectedShow();

private:
	int c;
	void PrivateShow();
};

int main() 
{/*
	ClassA number(1, 2, 3);

	number.x = 1;// public属性 外部可以访问
	number.PublicShow();// public方法 外部可以访问

	number.y = 2;//(X) protected属性 外部无法访问
	number.ProtectedShow();//(X) protected方法 外部无法访问

	number.z = 3;//(X) private属性 外部无法访问
	number.PrivateShow();//(X) private方法 外部无法访问
*/
	ClassB number(4, 5, 6, 1, 2, 3);
	number.PublicShow();
	number.PublicShow();
	return 0;
}


/***************************AAAAAAAAAAAAA******************************/

ClassA::ClassA(int x, int y, int z)
{
	this->x = x;
	this->y = y;
	this->z = z;
}

ClassA::~ClassA()
{
}

void ClassA::PublicShow()
{
	cout << x << "+" << y << "+" << z << endl;
	//Public中方法,x y z都能访问
}

void ClassA::ProtectedShow()
{
	cout << x << "+" << y << "+" << z << endl;
	//Protected中方法,x y z都能访问
}

void ClassA::PrivateShow()
{
	cout << x << "+" << y << "+" << z << endl;
	//Private中方法,x y z都能访问
}

/***************************BBBBBBBBBBBBBBBBBBB*************************/

ClassB::ClassB(int a, int b, int c,int x,int y,int z):ClassA(x, y, z)
{
	this->a = a;
	this->b = b;
	this->c = c;
}

ClassB::~ClassB()
{
}

void ClassB::PublicShow()
{
	cout << x << "+" << y << "+" << a << "+" << b << "+" << c << endl;
	//cout << x << "+" << y << "+" << z << endl;
	//ClassB Public方法中,z无法访问
}

void ClassB::ProtectedShow()
{
	cout << x << "+" << y << "+" << a << "+" << b << "+" << c << endl;
	//cout << x << "+" << y << "+" << z << endl;
	//ClassB Protected方法中,z无法访问
}

void ClassB::PrivateShow()
{
	cout << x << "+" << y << "+" << a << "+" << b << "+" << c << endl;
	//cout << x << "+" << y << "+" << z << endl;
	//ClassB Private方法中,z无法访问
}
/*总结
* 1.z是A的隐私部属性(private),ClassB采用public继承了ClassA,但是只能访问public和protected
* 2.如果子类和父类方法相同,优先调用子类自己的方法,PublicShow()
*/

private:一个类固有属性,一个类区别于另一个类,就靠它,不与外界直接交流

protected:一种对子类完全开放的属性,不与外界直接交流

public:一个类与外界交流的唯一出入口

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值