YTU-OJ-多重繼承

kewlgrl發表於2015-06-24

Problem G: C++習題 多重繼承

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1052  Solved: 554
[Submit][Status][Web Board]

Description

分別宣告Teacher(教師)類和Cadre(幹部)類,採用多重繼承方式由這兩個類派生出新類Teacher_Cadre(教師兼幹部)類。要求: 
(1) 在兩個基類中都包含姓名、年齡、性別、地址、電話等資料成員。
(2) 在Teacher類中還包含資料成員title(職稱),在Cadre類中還包含資料成員post(職務)。在Teacher_Cadre類中還包含資料成員wages(工資)。
(3) 對兩個基類中的姓名、年齡、性別、地址、電話等資料成員用相同的名字,在引用這些資料成員時,指定作用域。
(4)  在類體中宣告成員函式,在類外定義成員函式。
(5) 在派生類Teacher_Cadre的成員函式show中呼叫Teacher類中的display函式,輸出姓名、年齡、性別、職稱、地址、電話,然後再用cout語句輸出職務與工資。

Input

 姓名、年齡、性別、職稱、職務、地址、電話、工資

Output

 姓名、年齡、性別、職稱、地址、電話、職務、工資

Sample Input

Wang-li 50 f prof. president
135 Beijing Road,Shanghai
(021)61234567 1534.5

Sample Output

name:Wang-li
age:50
sex:f
title:prof.
address:135 Beijing Road,Shanghai
tel:(021)61234567
post:president
wages:1534.5

HINT

  前置程式碼及型別定義已給定如下,提交時不需要包含,會自動新增到程式前部



/* C++程式碼 */

#include<string>

#include <iostream>

using namespace std;

class Teacher

{

public:

    Teacher(string nam,int a,char s,string tit,string ad,string t);

    void display();

protected:

    string name;

    int age;

    char sex;

    string title;

    string addr;

    string tel;

};

class Cadre

{

public:

    Cadre(string nam,int a,char s,string p,string ad,string t);

    void display();

protected:

    string name;

    int age;

    char sex;

    string post;

    string addr;

    string tel;

};

class Teacher_Cadre:public Teacher,public Cadre

{

public:

    Teacher_Cadre(string nam,int a,char s,string tit,string p,string ad,string t,float w);

    void show( );

private:

    float wage;

};



主函式已給定如下,提交時不需要包含,會自動新增到程式尾部

/* C++程式碼 */



int main( )

{

    string name,title,post,address,tele;

    int age;

    char sex;

    float wages;

    cin>>name>>age;

    cin>>sex>>title>>post;

    cin.ignore(2,'\n');

    getline(cin,address);

    cin>>tele>>wages;

    Teacher_Cadre te_ca(name,age,sex,title,post,address,tele,wages);

    te_ca.show( );

    return 0;

}

#include<string>
#include <iostream>
using namespace std;
class Teacher
{
public:
    Teacher(string nam,int a,char s,string tit,string ad,string t);
    void display();
protected:
    string name;
    int age;
    char sex;
    string title;
    string addr;
    string tel;
};
class Cadre
{
public:
    Cadre(string nam,int a,char s,string p,string ad,string t);
    void display();
protected:
    string name;
    int age;
    char sex;
    string post;
    string addr;
    string tel;
};
class Teacher_Cadre:public Teacher,public Cadre
{
public:
    Teacher_Cadre(string nam,int a,char s,string tit,string p,string ad,string t,float w);
    void show( );
private:
    float wage;
};

Teacher::Teacher(string nam,int a,char s,string tit,string ad,string t):
    name(nam),age(a),sex(s),title(tit),addr(ad),tel(t) { }

void Teacher::display()
{
    cout<<"name:"<<name<<endl;
    cout<<"age:"<<age<<endl;
    cout<<"sex:"<<sex<<endl;
    cout<<"title:"<<title<<endl;
    cout<<"address:"<<addr<<endl;
    cout<<"tel:"<<tel<<endl;
}
Cadre::Cadre(string nam,int a,char s,string p,string ad,string t):
    name(nam),age(a),sex(s),post(p),addr(ad),tel(t) {}

void Cadre::display()
{
    cout<<"name:"<<name<<endl;
    cout<<"age:"<<age<<endl;
    cout<<"sex:"<<sex<<endl;
    cout<<"post:"<<post<<endl;
    cout<<"address:"<<addr<<endl;
    cout<<"tel:"<<tel<<endl;
}
Teacher_Cadre::Teacher_Cadre(string nam,int a,char s,string t,string p,string ad,string tel,float w):
    Teacher(nam,a,s,t,ad,tel),Cadre(nam,a,s,p,ad,tel),wage(w) {}

void Teacher_Cadre::show( )
{
    Teacher::display();
    cout<<"post:"<<Cadre::post<<endl;
    cout<<"wages:"<<wage<<endl;
}

/* C++程式碼 */



int main( )
{
    string name,title,post,address,tele;
    int age;
    char sex;
    float wages;
    cin>>name>>age;
    cin>>sex>>title>>post;
    cin.ignore(2,'\n');
    getline(cin,address);
    cin>>tele>>wages;
    Teacher_Cadre te_ca(name,age,sex,title,post,address,tele,wages);
    te_ca.show( );
    return 0;
}

相關文章