第12周任务2(分别定义Teacher(教师)类和Cadre(*)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼*))

时间:2022-09-07 19:17:10
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 臧鹏
* 完成日期: 2012 年 5 月 8 日
* 版 本 号:

* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:分别定义Teacher(教师)类和Cadre(*)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼*)。要求:
(1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
(2)在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre类中还包含数据成员wages(工资)。
(3)对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
(4)在类体中声明成员函数,在类外定义成员函数。
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。
* 程序输出:
* 程序头部的注释结束
*/

#include<iostream>

#include<string>

using namespace std;

class Teacher
{
public:
Teacher(string n, int a, char s, string add, long int te, string ti);
void display();
protected:
string name;
int age;
char sex;
string address;
long int tel;
string title; //职称
};

class Cadre
{
public:
Cadre(string n, int a, char s, string add, long int t, string p);
void display();
protected:
string name;
int age;
char sex;
string address;
long int tel;
string post; //职务
};

class Teacher_Cadre:public Teacher, public Cadre //声明多重继承的Teacher_Cadre类
{
public:
Teacher_Cadre(string n, int a, char s, string add, long int t, string ti, string p,double w);
void show();
protected:
double wages;
};

Teacher::Teacher(string n, int a, char s, string add, long int te, string ti)
{
name = n;
age = a;
sex = s;
address = add;
tel = te;
title = ti;
}

Cadre::Cadre(string n, int a, char s, string add, long int t, string p)
{
name = n;
age = a;
sex = s;
address = add;
tel = t;
post = p;
}

void Teacher::display()
{
cout << "name: " << name << endl;
cout << "age: " << age << endl;
cout << "sex: " << sex << endl;
cout << "address: " << address << endl;
cout << "tel: " << tel << endl;
cout << "title: " << title << endl;
}

void Cadre::display()
{
cout << "name: " << name << endl;
cout << "age: " << age << endl;
cout << "sex: " << sex << endl;
cout << "address: " << address << endl;
cout << "tel: " << tel << endl;
cout << "post: " << post << endl;
}

void Teacher_Cadre::show() //在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数
{
Teacher::display();
cout << "post: " << Cadre::post << endl; //对两个基类中的数据成员用相同的名字,在引用这些数据成员时,指定作用域
cout << "wages: " << wages << endl;
}

Teacher_Cadre::Teacher_Cadre(string n, int a, char s, string add, long int t, string ti, string p,double w):Teacher(n, a, s, add, t, ti),Cadre(n, a, s, add, t, p)
{
wages = w;
}

int main( )
{

Teacher_Cadre ct1("永罡", 24, 'm', "shandong", 152999111, "主任","技术工",10000);

ct1.show();

cout << endl;

system("PAUSE");

return 0;
}


第12周任务2(分别定义Teacher(教师)类和Cadre(*)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼*))

经验积累:

这个知识点需要多次看书,很好理解但是很容易出错,要多看课本,特别是当数据成员多的时候,会很乱,要细心写