本文主要是介绍第十二周实验指导--任务2--分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)
* 作 者: 雷恒鑫
* 完成日期: 2012 年 04 月 29 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述: 1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
(2)在Teacher类中还包含数据成员title(职称),在Cadre类中还包 含数据成员post(职务),在Teacher_Cadre类中还包含 数据成员 wages(工资)。
(3)对两个基类中的姓名、年龄、性别、地址、电话 等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
(4)在类体中声明成员函数,在类外定义成员函数。
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用 cout语句输出职务与工资。
* 问题描述:* 程序输出:
* 程序头部的注释结束
*/
#include<iostream>
#include<string>
#include <iomanip>
using namespace std;class Teacher
{
public: Teacher(string nam,int Age,int Sex,char *Department, int phone,string title1);~Teacher( );void display();
protected: string name;int m_nAge; int m_nSex;//0:women,1:man char *m_szDepartment; int m_phone; string title;
};
Teacher::Teacher(string nam,int Age,int Sex,char *Department,int phone,string title1)
{name=nam;m_nAge=Age;m_nSex=Sex;int i;m_szDepartment=new char[30];for(i=0;i<30;++i)m_szDepartment[i]=*(Department+i); //此处的处理是个重点,处理不好,会在程序中植入非常严重且隐蔽性极强的"定时炸弹" //strcpy(m_szDepartment,Department); m_phone=phone;title=title1;
}Teacher::~Teacher()
{ delete [] m_szDepartment;
}
void Teacher::display()
{cout << setiosflags(ios :: left) << setw(5) <<name<< setw(5) <<m_nAge << setw(5)<<((m_nSex==0)?"women":"man")<<setw(8)<<m_szDepartment<<setw(5)<<title<<setw(5)<<m_phone;
}
class Cadre
{
public: Cadre(string nam,int Age,int Sex,char *Department,int phone,string post1) ;~Cadre( );
protected: string name;int m_nAge; int m_nSex;//0:women,1:man char *m_szDepartment; int m_phone; string post;
};
Cadre::Cadre(string nam,int Age,int Sex,char *Department,int phone,string post1)
{name=nam;m_nAge=Age;m_nSex=Sex;int i;m_szDepartment=new char[30];for(i=0;i<30;++i)m_szDepartment[i]=*(Department+i); m_phone=phone; post=post1;
}
Cadre::~Cadre()
{ delete [] m_szDepartment;
}
class Teacher_Cadre:public Teacher,public Cadre
{
public: Teacher_Cadre(string nam,int Age,int Sex,char *Department,int phone,string title1,string post1,float wages1) ;//~Teacher_Cadre( ); void show();friend ostream & operator<<(ostream &,const Teacher_Cadre &);//重载运算符"<<"
protected: /*string name;int m_nAge; int m_nSex;//0:women,1:man char *m_szDepartment; int m_phone; */float wages;
};
Teacher_Cadre::Teacher_Cadre(string nam,int Age,int Sex,char *Department,int phone,string title1,string post1,float wages1):Teacher(nam,Age,Sex,Department,phone,title1),Cadre(nam,Age,Sex,Department,phone,post1),wages(wages1){}
/*Teacher_Cadre::~Teacher_Cadre()
{ delete [] m_szDepartment;
}*/
void Teacher_Cadre::show()
{display();
}
ostream & operator<<(ostream &output,const Teacher_Cadre &p)
{ output<<" 工资为:";output<<setw(5)<<p.wages<<" "; output<<"职务为:";output<<p.title<<endl; return output;
}int main( )
{string name,title1,post1;char department[10]; int sex,age;int phone; float wages; cout<<"input Teacher_Cadre's name,Age,sex(0:women,1:man),department,phone,title,post,wages:\n"; cin>>name>>age>>sex>>department>>phone>>title1>>post1>>wages; Teacher_Cadre Teacher_Cadre1(name,age,sex,department,phone,title1,post1,wages); Teacher_Cadre1.show(); cout<<Teacher_Cadre1;system("PAUSE");return 0;
}
运行结果:
这篇关于第十二周实验指导--任务2--分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!