栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Public继承

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Public继承

#include
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() 
{
	ClassB number(4, 5, 6, 1, 2, 3);
	number.PublicShow();
	number.PublicShow();
	return 0;
}




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都能访问
}



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无法访问
}

private:一个类固有属性,一个类区别于另一个类,就靠它,不与外界直接交流 protected:一种对子类完全开放的属性,不与外界直接交流 public:一个类与外界交流的唯一出入口
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/311929.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号