《C++第十二周实验报告2-1》--分别定义Teacher(教师)类和Cadre(干部采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)...

本文详细阐述了如何在C++中利用多重继承特性,通过定义Teacher和Cadre类来创建Teacher_Cadre类。该类不仅继承了教师和干部的基本属性,还实现了特定的功能,如输出姓名、年龄、性别、职称、职务和工资。通过展示类的定义、构造函数、成员函数和实例化过程,本文为理解面向对象编程和继承机制提供了深入的实践案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
【任务2】(教材P394习题9)分别定义Teacher(教师)类和Cadre(干部)类,
采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求: 
(1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。 
(2)在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),
在Teacher_Cadre类中还包含数据成员wages(工资)。 
(3)对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。 
(4)在类体中声明成员函数,在类外定义成员函数。 
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,
输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。
*/
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生 
* All rights reserved.
* 文件名称:    Person.cpp                          
* 作    者:   计114-3 王兴锋     
* 完成日期:    2012  年   5    月   7    日
* 版 本 号:       V 4.0
* 程序头部的注释结束
*/

#include <iostream>
#include <string>

using namespace std;

class Person
{
public:
	Person(string name, int age, char sex, string add, string tel);
protected:
	string name, add,  tel;
	int age;
	char sex;	
};
Person::Person(string name, int age, char sex, string add, string tel)
{
	this->name = name, this->age = age, this->sex = sex, this->add = add, this->tel = tel;
}

class Teacher : virtual public Person
{
public:
	Teacher(string name, int age, char sex, string add, string tel, string title);
protected:
	string title;
	void display();
};
Teacher::Teacher(string name, int age, char sex, string add, string tel, string title):Person(name, age, sex, add, tel)
{
	this->title = title;
}
void Teacher::display()
{
	cout << "姓名:" << name << endl;
	cout << "年龄:" << age << endl;
	cout << "性别:" << sex << endl;
	cout << "职称:" << title << endl;
	cout << "地址:" << add << endl;
	cout << "电话:" << tel << endl;
}

class Cadre : virtual public Person
{
public:
	Cadre(string name, int age, char sex, string add, string tel, string post);
protected:
	string post;
};
Cadre::Cadre(string name, int age, char sex, string add, string tel, string post):Person(name, age, sex, add, tel)
{
	this->post = post;
}

class Teacher_Cadre : public Cadre, public Teacher
{
public:
	Teacher_Cadre(string name, int age, char sex, string add, string tel, string post, string title, double wages);
	void show();
private:
	double wages;
};
Teacher_Cadre::Teacher_Cadre(string name, int age, char sex, string add, string tel, string post, string title, double wages):
	Teacher(name, age, sex, add, tel, title),
	Cadre(name, age, sex, add, tel, post),
	Person(name, age, sex, add, tel)
{
	this->wages = wages;
}
void Teacher_Cadre::show()
{
	display();
	cout << "职务:" << post << endl;
	cout << "工资:" << wages << endl;
}

int main()
{
	Teacher_Cadre tc("张三",32,'f',"烟台大学","18253576921","主任","教师",3500);
	tc.show();

	system("PAUSE");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值