类的继承与派生简单习题

设计一个学生类Student,该类包含保护性数据成员: 学号(NUM),姓名(Name),性别(Sex);定义公用成员函数:ReadData() 用于输入Num, Name和Sex, OutputData() 用于输出Num, Name和Sex。
在学生类的基础上采用公用继承派生出研究生类Graduate, 在Graudate类中增加私有数据成员研究方向(R_Interest),增加成员函数Set_Research()设置研究方向,增加成员函数ResearchWork()用于输出学号和研究方向。

注意:数据成员的类型可以根据需要设定,成员函数的参数可以根据需要设定。
程序代码:

#include<iostream>
#include<string.h>
using namespace std;
class Student
{
	public:
		void ReadData();
		void OutputData();
	protected:
		int Num;
		string Name;
		char Sex;
};
void Student::ReadData()
{
	cin>>Num>>Name>>Sex;
}
void Student::OutputData()
{
	cout<<"学号:"<<Num<<endl;
	cout<<"姓名:"<<Name<<endl;
	cout<<"性别:"<<Sex<<endl; 
}

class Graduate:public Student
{
	public:
		void Set_Research();
		void ResearchWork();
	private:
		string R_Interest;
};
void Graduate::Set_Research()
{
	cin>>R_Interest;
}
void Graduate::ResearchWork()
{
	cout<<"研究方向:"<<R_Interest;
}

int main()
{
	Graduate t1;
	cout<<"输入一个学生的学号,姓名,性别,研究方向。"<<endl;
	cout<<"例如:1710252387 Tom B math (男用B表示,女用G表示)"<<endl; 
	t1.ReadData();
	t1.OutputData();
	t1.Set_Research();
	t1.ResearchWork();
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值