码迷,mamicode.com
首页 > 编程语言 > 详细

C++每日一题2020.5.19(继承与派生类)

时间:2020-05-19 22:26:01      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:ima   ESS   digital   return   lang   函数   mat   form   ios   

继承与派生实验

定义一个本科生类Undergraduate,包括学号、姓名、专业这三个数据成员,在此基础上派生出研究类Graduate,并增加导师、发表论文数等属性,可以通过显示个人信息函数ShowInfo来查个人档案。

#include<iostream>

#include<string>

using  namespace  std;

class  Undergraduate

{

public:

	int id;

	string name;

	string profession;

  
	
	void  ShowInfo()

	{

		cout <<  "Name:"  << name << endl;

		cout <<  "ID:"  << id << endl;

		cout <<  "profession:"  << profession << endl;

	}

};

class  Graduate : public  Undergraduate

{

public:

	int id;

	string name;

	string profession;

	string tutor;

	int essay_num;

  

	void  ShowInfo()

	{

	cout <<  "Name:"  << name << endl;

	cout <<  "ID:"  << id << endl;

	cout <<  "profession:"  << profession << endl;

	cout <<  "tutor:"  << tutor << endl;

	cout <<  "Essay Number:"  << essay_num << endl;

	}

};

void  test01()

{

	Undergraduate u1;

	u1.name="LiJiayan";

	u1.id=2019070918;

	u1.profession="digital information";

	Graduate g1;

	g1.name="ZhangBo";

	g1.id=1991;

	g1.profession="software engineering";

	g1.tutor="LiJiayan";

	g1.essay_num=100;

	u1.ShowInfo();

	g1.ShowInfo();
}
int  main()

{

	test01();

	return  0;

}

输出结果:
技术图片

加油吧!

C++每日一题2020.5.19(继承与派生类)

标签:ima   ESS   digital   return   lang   函数   mat   form   ios   

原文地址:https://www.cnblogs.com/lijiayan/p/12919872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!