C++ 面向对象- -一些习题(二)

1、设计一个学生类Student,该类包含 保护性 数据成员: 学号(NUM),姓名(Name),性别(Sex);定义公用成员函数:ReadData() 用于输入Num, Name和Sex, OutputData() 用于输出Num, Name和Sex。

在学生类的基础上采用公用继承派生出研究生类Graduate, 在Graudate类中增加私有数据成员研究方向(R_Interest),增加成员函数Set_Research()设置研究方向,增加成员函数ResearchWork()用于输出学号和研究方向。

#include <iostream>
#include <string>
using namespace std;

class Student{
	protected:
		int num ;
		string name ;
		string sex ;
	public:
		Student(int n=0,string m="xu",string s="nan"):num(n),name(m),sex(s){}
		void ReadData(){
			cout << "输入学生的信息: "<<endl;
			cin>> num >> name >> sex ;
		}
		void OutputData(){
			cout << "该学生的信息是:    "<<endl;
			cout << "学号:		" << num << endl <<"姓名:		"<< name << endl <<"性别:		"<< sex << endl; 
		}
		void display(){
			cout << num ;
		}	
};
class Graduate : public Student{
		string R_Interest ;
	public:
		Graduate(string r="mu"):R_Interest(r){}
		void Set_Research(){
			cout << "输入这个研究生的发展方向:  " ;
			cin >> R_Interest ;
		}
		void ResearchWork(Student &s){
			cout << "输出学生的学号和研究方向:   "<< endl;
			cout << "学号:		";s.display();cout<<endl;
			cout <<  "研究方向:	" << R_Interest << endl;
		}
};
int main(){
	Student s ;
	s.ReadData() ;
	s.OutputData() ;
	cout << "读到研究生后: " << endl;
	Graduate g ;
	g.Set_Research() ;cout<<endl;
	g.ResearchWork(s) ;
}

其运行结果如下:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值