多重继承方式派生出新类

分别声明 Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类 Teacher Cadre(教师兼干部)。要求:
①在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
②在 Teacher类中还包含数据成员 title(职称),在 Cadre类中还包含数据成员post(职务)。在 Teacher Cadre类中还包含数据成员wages(工资)。
③对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
④在类体中声明成员函数,在类外定义成员函数。
⑤在派生类 Teacher Cadre的成员函数show中调用 Teacher类中的 display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。
1.程序代码:

#include<iostream>
#include<string>
using namespace std;
class teacher
{
public:
	teacher(string nam, int a, char s, string ad, int n, string t)
	{
		name = nam;
		age = a;
		sex = s;
		addr = ad;
		num = n;
		title = t;
	}
	//void get_value();
	void display();
protected:
	string name;
	int age;
	char sex;
	string addr;
	int num;
	string title;
};

void teacher::display()
{
	cout << "name=" << name << endl;
	cout << "age=" << age << endl;
	cout << "sex=" << sex << endl;
	cout << "addr=" << addr << endl;
	cout << "num=" << num << endl;
	cout << "title=" << title << endl;
}
class cadre
{
public:
	//cadre();
	cadre(string nam, int a, char s, string ad, int n, string p)
	{
		name = nam;
		age = a;
		sex = s;
		addr = ad;
		num = n;
		post = p;
	}
	//void get_value2();
protected:
	string name;
	int age;
	char sex;
	string addr;
	int num;
	string post;
};

class teacher_cadre:public teacher, public cadre
{public:
	//teacher_cadre() { name = 0, age = 0, sex = 0, addr = 0, num = 0, title = 0, post = 0, wages = 0; }
	teacher_cadre(string nam, int a, char s, string ad, int n,string t,string p, int w) :
		teacher(nam, a, s, ad, n,t), cadre(nam, a, s, ad, n,p),wages(w){}
	//void get_value1();
	void show();
private:
	int wages;
};

void teacher_cadre::show()
{
	cout << "post=" << cadre::post << endl;
	cout << "wages=" << wages << endl;
}
int main()
{
	teacher_cadre people1("zhang",12,'w',"beijing",12345678910,"hsu","sh",23);
	//cout << "输入这些值name,age,sex,addr,num,title,post,wages:" << endl;
	
	people1.teacher::display();
	people1.show();
	return 0;
}

2.运行结果:
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值