C++ 继承与多重继承
来源:互联网 发布:linux eth1 改为eth0 编辑:程序博客网 时间:2024/06/13 14:27
(1)编程:编写一个学生类Student。要求:
① 学生的保护属性有:学号(Num),姓名(Name),性别(Sex);
学生的公用成员函数有:ReadData( )用于输入Num、Name、Sex。OutputData( )用于输出Num、Name、Sex。
② 然后,在学生类的基础上派生出研究生类Graduate,该类新增私有属性研究方向(ResearchD),新增公用成员函数ResearchWork( ),该函数功能为输出学号和研究方向。
③ 编写main函数测试基类和派生类的各属性很函数的访问。修改派生类的派生方式以及基类成员的访问方式,再测试看看基类和派生类的属性访问方式有何不同。
(2)编程:在上述工程中增加一个Teacher(教师)类,采用多重继承方式由Student和Teacher类公有派生出新类Student_Teacher。要求:
① Teacher类的保护成员有:Name、Sex、Title(职称)、Wages(工资)。两个基类中的Name、Sex同名,在引用这些数据成员时,指定作用域。
② 在派生类Student_Teacher中,增加两个成员函数:ReadF1()用于读入:Num、Name、Sex、Title、Wages。Show()用于输出数据,在Show()中,首先调用Student类的display()函数用于输出Num、Name、Sex,然后再用cout语句输出Title、Wages。
③ 在类体中声明成员函数,在类外定义成员函数。
#include<iostream>using namespace std;class People { protected: int Num; string Name; string Sex; public: void ReadData() { } void OutputData() { }};class Student:virtual public People { public: void ReadData(int Num,string Name,string Sex) { this->Num = Num; this->Name = Name; this->Sex = Sex; } void OutputData() { cout<<"学号:"<<Num<<endl; cout<<"姓名:"<<Name<<endl; cout<<"性别:"<<Sex<<endl; }};class Teacher:virtual public People { protected: string subject; public: void ReadData(int Num,string Name,string Sex,string subject) { this->Num = Num; this->Name = Name; this->Sex = Sex; this->subject = subject; } void OutputData() { cout<<"学号:"<<Num<<endl; cout<<"姓名:"<<Name<<endl; cout<<"性别:"<<Sex<<endl; cout<<"科目:"<<subject<<endl; }};class Graduate:public Student { private: string ResearchID = "计算机"; public: void ResearchWork() { cout<<"研究方向是:"<<ResearchID<<endl; }};class Student_Teacher:public Student,public Teacher { protected: string Title; double Wages; public: void ReadData(int Num,string Name,string Sex,string subject,string Title,double Wages) { this->Num = Num; this->Name = Name; this->Sex = Sex; this->subject = subject; this->Title = Title; this->Wages = Wages; } void OutputData() { cout<<"学号:"<<Num<<endl; cout<<"姓名:"<<Name<<endl; cout<<"性别:"<<Sex<<endl; cout<<"科目:"<<subject<<endl; cout<<"职称:"<<Title<<endl; cout<<"工资:"<<Wages<<endl; }};int main() { Student s; s.ReadData(1,"张三","男"); s.OutputData(); Graduate g; g.ReadData(2,"李四","女"); g.OutputData(); g.ResearchWork(); Teacher t; t.ReadData(3,"王五","男","数学"); t.OutputData(); Student_Teacher st; st.ReadData(4,"马六","女","语文","教授",5000); st.OutputData();}
运行结果:
0 0
- 多重继承与虚继承
- 多重继承与虚继承
- 多重继承与虚继承
- 单一继承与多重继承
- 多重继承与虚继承
- 多重继承与虚继承
- 多重继承与虚继承
- 多重继承与虚拟继承
- 多重继承与虚继承
- C++ 继承与多重继承
- 多继承与多重继承
- 单一继承与多重继承
- C++-继承:多重继承 && 虚拟继承
- C++_Primer_chapter17 3.多重继承与虚继承
- 11 继承,多重继承与虚继承
- (C++)多重继承
- C++--多重继承
- 接口与多重继承
- Segmentation fault 问题浅谈
- SQL中concat()函数、substr()函数与length()函数
- 对一组数乱序
- 解决DB2的一个BUG收获(SQLCode=-805, SQLState=51002, error message=DB2 SQL Error: SQLCODE=-805, SQLSTATE=5100
- linux下c/c++实例之十一简单的计算器程序
- C++ 继承与多重继承
- URL中的特殊字符
- Onmeasure报空指针
- win32游戏编程——贪吃蛇游戏
- rk3288_lubuntu 定制问题
- runtime运行时
- SpringMVC源码分析 之 HandlerMapping和HandlerAdapter
- Jenkins 邮件配置 (使用 Jenkins Email Extension Plugin)
- app-文档预览