1. 程式人生 > >c++第五次上機實驗——教師幹部類

c++第五次上機實驗——教師幹部類

上機內容:多重繼承派生類的使用

上機目的:掌握多重繼承

/* 
* 檔名稱:教師幹部類 
* 作    者:陳德強  
* 完成日期: 2016 年 5  月 7  日 
* 版 本 號:v1.0 
* 對任務及求解方法的描述部分:略
* 輸入描述:略  
* 問題描述:略 
* 程式輸出:略 
* 問題分析:略 
* 演算法設計:略  
*/ 
#include<iostream>      
#include<string>     
using namespace std;    
class Teacher    
{    
public:    
    Teacher(string n, int a, string s, string ti);    
    void display();    
protected:    
    string name;    
    int age;    
    string sex;            
    string title; 
};    
  
class Cadre    
{    
public:    
    Cadre(string n,int a,string s,string p);    
    void display();    
protected:    
    string name;    
    int age;    
    string sex;           
    string post;
};    
  
class Teacher_Cadre:public Teacher, public Cadre 
{    
public:    
    Teacher_Cadre(string n,int a,string s,string ti,string p,double w);    
    void show();    
protected:    
    double wages;    
};    
  
Teacher::Teacher(string n,int a,string s,string ti)    
{    
    name = n;    
    age = a;    
    sex = s;  
    title = ti;    
}    
  
Cadre::Cadre(string n,int a,string s,string p)    
{    
    name = n;    
    age = a;    
    sex = s;            
    post = p; 
}    
  
void Teacher::display()    
{    
     cout << "姓名: " << name << endl;    
    cout << "年齡: " << age << endl;    
    cout << "性別: " << sex << endl;    
    cout << "職稱: " << title << endl;            
}    
  
void Cadre::display()    
{    
    cout << "姓名: " << name << endl;    
    cout << "年齡: " << age << endl;    
    cout << "性別: " << sex << endl;    
    cout << "職稱: " << post << endl;      
}    
  
void Teacher_Cadre::show()  
{    
    Teacher::display();    
    cout << "職稱: " << Cadre::post << endl; 
    cout << "工資: " << wages << endl;    
}    
Teacher_Cadre::Teacher_Cadre(string n, int a, string s, string ti, string p,double w):Teacher(n, a, s, ti),Cadre(n, a, s, p)    
{    
    wages = w;    
}    
  
int main( )    
{        
    Teacher_Cadre p1("曾輝",42,"男","副教授","主任",1534.5);  
    p1.show();       
    return 0;    
}   
執行結果:

心得體會:繼承的方式不同

知識點總結:多重繼承的基類建構函式的使用