c++第五次实验--多继承
来源:互联网 发布:java获取星期几 编辑:程序博客网 时间:2024/06/02 01:42
一、问题及代码
项目2: 教师干部类
分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:
(1)在两个基类中都包含姓名、年龄、性别等数据成员。
(2)在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre类中还包含数据成员wages(工资)。
(3)对两个基类中的姓名、年龄、性别等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
(4)在类体中声明成员函数,在类外定义成员函数。
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称,然后再用cout语句输出职务与工资。
/* * 文件名称:多继承 * 作 者:孙红亮 * 完成日期:2016 年 5 月 11 日 * 版 本 号:v1.0 * 对任务及求解方法的描述部分:无* 输入描述: 无* 问题描述: 熟悉程序的编辑运行过程* 程序输出: 得到图中的结果 * 问题分析:无 * 算法设计: 略*/ #include<string> #include <iostream> using namespace std; class Teacher { public: Teacher(string nam,int a,string s,string tit); void display(); protected: string name; int age; string sex; string title; }; Teacher::Teacher(string nam,int a,string s,string tit): name(nam),age(a),sex(s),title(tit) { } void Teacher::display() { cout<<"姓名:"<<name<<endl; cout<<"年龄:"<<age<<endl; cout<<"性别:"<<sex<<endl; cout<<"职称:"<<title<<endl; } class Cadre { public: Cadre(string nam,int a,string s,string p); void display(); protected: string name; int age; string sex; string post; }; Cadre::Cadre(string nam,int a,string s,string p): name(nam),age(a),sex(s),post(p) {} void Cadre::display() { cout<<"姓名:"<<name<<endl; cout<<"年龄:"<<age<<endl; cout<<"性别:"<<sex<<endl; cout<<"职称:"<<post<<endl; } class Teacher_Cadre:public Teacher,public Cadre { public: Teacher_Cadre(string nam,int a,string s,string tit,string p,float w); void show( ); private: float wage; }; Teacher_Cadre::Teacher_Cadre(string nam,int a,string s,string t,string p,float w): Teacher(nam,a,s,t),Cadre(nam,a,s,p),wage(w) {} void Teacher_Cadre::show( ) { Teacher::display(); cout<<"职称:"<<Cadre::post<<endl; cout<<"工资:"<<wage<<endl; } int main( ) { Teacher_Cadre te_ca("曾辉",42,"男","副教授","主任",1534.5); te_ca.show( ); return 0; }
二,运行结果
三,心得体会
继承和派生还不是很熟练,格式记忆不清楚,编的比较困难。继承和派生还不是很熟练,格式记忆不清楚,编的比较困难。
四,知识点总结
公有继承,派生类构造函数。
0 0
- C++第五次实验-------多继承
- c++第五次实验--多继承
- C++第五次实验—多继承 作业
- c++第五次实验——多继承
- 第五次C语言上机实验报告
- 第五次C程序设计实验报告
- 第五次C语言上机实验报告
- C语言第五次上机实验
- 第五次实验代码(其他班)——继承多态
- 第五次实验
- 第五次上机实验
- 第五次上机实验
- 第五次上机实验
- 第五次上机实验
- 第五次上机实验
- 第五次上机实验
- 第五次实验报告
- 第五次上机实验
- 调试基础知识0(串口调试助手对比 丁丁 大傻 友善)
- 常用工具类
- 字节流与字符流的区别详解
- Char 转int JAVA
- 用 Python 和 OpenCV 检测图片上的条形码Detecting Barcodes in Images with Python and OpenCV
- c++第五次实验--多继承
- transpose with gpu
- C++中的三目运算符号
- 一个自己整合的访问网络的类
- 【C语言】判断字符串是否是回文
- VLC视频播放器原理详细分析含TS流格式分析
- cenos 6.4 安装memcached 服务端
- android工程下drawable文件夹的作用
- Style.xml的妙用(一)