|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: C++ 继承与多重继承 |
| 4 | +date: 2015-11-12 16:56 |
| 5 | +categories: C++ |
| 6 | +tags: [C++] |
| 7 | +--- |
| 8 | +1. 编写一个学生类Student。要求: |
| 9 | + - 学生的保护属性有:学号(Num),姓名(Name),性别(Sex).学生的公用成员函数有:ReadData( )用于输入Num、Name、Sex。OutputData( )用于输出Num、Name、Sex。 |
| 10 | + - 在学生类的基础上派生出研究生类Graduate,该类新增私有属性研究方向(ResearchD),新增公用成员函数ResearchWork( ),该函数功能为输出学号和研究方向。 |
| 11 | + - 编写main函数测试基类和派生类的各属性很函数的访问。修改派生类的派生方式以及基类成员的访问方式,再测试看看基类和派生类的属性访问方式有何不同。 |
| 12 | +2. 在上述工程中增加一个Teacher(教师)类,采用多重继承方式由Student和Teacher类公有派生出新类Student_Teacher。要求: |
| 13 | + - Teacher类的保护成员有:Name、Sex、Title(职称)、Wages(工资)。两个基类中的Name、Sex同名,在引用这些数据成员时,指定作用域。 |
| 14 | + - 在派生类Student_Teacher中,增加两个成员函数:ReadF1()用于读入:Num、Name、Sex、Title、Wages。Show()用于输出数据,在Show()中,首先调用Student类的display()函数用于输出Num、Name、Sex,然后再用cout语句输出Title、Wages。 |
| 15 | + - 在类体中声明成员函数,在类外定义成员函数。 |
| 16 | +```cpp |
| 17 | +#include<iostream> |
| 18 | +using namespace std; |
| 19 | +class People { |
| 20 | + protected: |
| 21 | + int Num; |
| 22 | + string Name; |
| 23 | + string Sex; |
| 24 | + public: |
| 25 | + void ReadData() { |
| 26 | + } |
| 27 | + void OutputData() { |
| 28 | + } |
| 29 | +}; |
| 30 | +class Student:virtual public People { |
| 31 | + public: |
| 32 | + void ReadData(int Num,string Name,string Sex) { |
| 33 | + this->Num = Num; |
| 34 | + this->Name = Name; |
| 35 | + this->Sex = Sex; |
| 36 | + } |
| 37 | + void OutputData() { |
| 38 | + cout<<"学号:"<<Num<<endl; |
| 39 | + cout<<"姓名:"<<Name<<endl; |
| 40 | + cout<<"性别:"<<Sex<<endl; |
| 41 | + } |
| 42 | +}; |
| 43 | +class Teacher:virtual public People { |
| 44 | + protected: |
| 45 | + string subject; |
| 46 | + public: |
| 47 | + void ReadData(int Num,string Name,string Sex,string subject) { |
| 48 | + this->Num = Num; |
| 49 | + this->Name = Name; |
| 50 | + this->Sex = Sex; |
| 51 | + this->subject = subject; |
| 52 | + } |
| 53 | + void OutputData() { |
| 54 | + cout<<"学号:"<<Num<<endl; |
| 55 | + cout<<"姓名:"<<Name<<endl; |
| 56 | + cout<<"性别:"<<Sex<<endl; |
| 57 | + cout<<"科目:"<<subject<<endl; |
| 58 | + } |
| 59 | +}; |
| 60 | +class Graduate:public Student { |
| 61 | + private: |
| 62 | + string ResearchID = "计算机"; |
| 63 | + public: |
| 64 | + void ResearchWork() { |
| 65 | + cout<<"研究方向是:"<<ResearchID<<endl; |
| 66 | + } |
| 67 | +}; |
| 68 | +class Student_Teacher:public Student,public Teacher { |
| 69 | + protected: |
| 70 | + string Title; |
| 71 | + double Wages; |
| 72 | + public: |
| 73 | + void ReadData(int Num,string Name,string Sex,string subject,string Title,double Wages) { |
| 74 | + this->Num = Num; |
| 75 | + this->Name = Name; |
| 76 | + this->Sex = Sex; |
| 77 | + this->subject = subject; |
| 78 | + this->Title = Title; |
| 79 | + this->Wages = Wages; |
| 80 | + } |
| 81 | + void OutputData() { |
| 82 | + cout<<"学号:"<<Num<<endl; |
| 83 | + cout<<"姓名:"<<Name<<endl; |
| 84 | + cout<<"性别:"<<Sex<<endl; |
| 85 | + cout<<"科目:"<<subject<<endl; |
| 86 | + cout<<"职称:"<<Title<<endl; |
| 87 | + cout<<"工资:"<<Wages<<endl; |
| 88 | + } |
| 89 | +}; |
| 90 | +int main() { |
| 91 | + Student s; |
| 92 | + s.ReadData(1,"张三","男"); |
| 93 | + s.OutputData(); |
| 94 | + Graduate g; |
| 95 | + g.ReadData(2,"李四","女"); |
| 96 | + g.OutputData(); |
| 97 | + g.ResearchWork(); |
| 98 | + Teacher t; |
| 99 | + t.ReadData(3,"王五","男","数学"); |
| 100 | + t.OutputData(); |
| 101 | + Student_Teacher st; |
| 102 | + st.ReadData(4,"马六","女","语文","教授",5000); |
| 103 | + st.OutputData(); |
| 104 | +} |
| 105 | +``` |
| 106 | +
|
| 107 | +运行结果: |
| 108 | + |
0 commit comments