实验2:C++类的继承与派生——任务四

[实验任务四]多继承

学生具有姓名,班级,学号等属性,有上课等行为;教师具有工号,工资等属性,有教课等行为;助教既是学生,又是老师,具有学生和老师的双重属性。请用类的多继承机制实现上述问题。

实验要求:

1.注意加上必要的输入输出提示;

2.注意开头的标注部分,加上自己的姓名以及修改日期;

3.附上实验结果。

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

class Student
{
public:
	Student() {};
	Student(string x, int y, int z) {//有参数的构造函数
		name = x;
		grad = y;
		StuNo = z;
	}
	void Print()
	{
		cout << "姓名: " << name << endl;
		cout << "班级: " << grad << endl;
		cout << "学号: " << StuNo << endl;
	}
	void classing()
	{
		cout << "上课" << endl;
	}
protected:
	string name;
	int grad;
	int StuNo;
};
class Teacher
{
public:
	Teacher() {};
	Teacher(string q, int w, int e){
		na = q;
		TecNo = w;
		Pay = e;
	}

	void Print()
	{
		cout << "工号: " << TecNo << endl;
		cout << "工资: " << Pay << endl;
	}
	void teaching()
	{
		cout << "授课" << endl;
	}
protected:
	string TecNo;
	string Pay;
};

class Assistant :public Student, public Teacher
{
public:
	Assistant(string na, int grad, int StuNo, int TecNo, int Pay) :Student( na,  grad, StuNo), Teacher(na,TecNo,  Pay)
	{
	}
	void Print()
	{
		Student::Print();
		Teacher::Print();
	}
};
int main()
{
	string x, q; int y, z, w, e;
	cout << "请输入学生的姓名,班级和学号" << endl;
	cin >> x >> y >> z ;
	Student stu(x,y,z);
	stu.Print();
	stu.classing();
	cout << "请输入老师的姓名,工号和工资" << endl;
	cin >> q >> w >> e;
	Teacher tea(q,w,e);
	tea.Print();
	tea.teaching();
	cout << "请输入助教的的姓名,班级和学号,工号和工资" << endl;
	cin >> x >> y >> z >> w >> e;
	Assistant assistant1(x,y,z,w,e);
	assistant1.Print();
	assistant1.classing();
	assistant1.teaching();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值