1479: 多重继承派生(3)--person、account、admin和master类

1479: 多重继承派生(3)--person、account、admin和master类

Description

Person类包含私有成员数据姓名name(string)和编号code(int)。Account类包含私有成员数据姓名name(string)、编号code(int)和工资pay(int)。Admin类包含私有成员数据姓名name(string)、编号code(int)和实践经验experience(string)。Master类包含私有成员数据姓名name(string)、编号code(int)、工资pay(int)和实践经验experience(string)。
请根据给定的main函数以及运行结果设计相应的类及相互间的继承关系。
main函数已给定(如下所示),提交时只需要提交main函数外的代码部分。
int main()
{
    string name,experience;
    int code,pay,Cas=0;
    while(cin>>name>>code>>pay>>experience)
    {
        Cas++;
        cout<<"CASE #"<<Cas<<":"<<endl;
        Person person(name,code);
        Account account(name,code,pay);
        Admin admin(name,code,experience);
        Master master(name,code,pay,experience);
        Show(&person);
        Show(&account);
        Show(&admin);
        Show(&master);
    }
    return 0;
}

Input

包含多组数据(数据均正确合法)
每组测试数据1行,分别表示姓名,编号,工资和实践经验。

Output

每组测试数据输出具体格式详见Sample Output。

Sample Input 

张三 201506 2541 绘画
王大夫 2012510 3654 跳舞
殷文琦 1005210 5412 写代码编程

Sample Output

CASE #1:
Person::Constructor Function is called.
Person::Constructor Function is called.
Account::Constructor Function is called.
Person::Constructor Function is called.
Admin::Constructor Function is called.
Person::Constructor Function is called.
Account::Constructor Function is called.
Admin::Constructor Function is called.
Master::Constructor Function is called.
Show Function is called.
Person::Show Function is called.
NAME:张三  CODE:201506
Show Function is called.
Account::Show Function is called.
NAME:张三 CODE:201506 PAY:2541
Show Function is called.
Admin::Show Function is called.
NAME:张三 CODE:201506 EXPERIENCE:绘画
Show Function is called.
Master::Show Function is called.
NAME:张三 CODE:201506 PAY:2541 EXPERIENCE:绘画
CASE #2:
Person::Constructor Function is called.
Person::Constructor Function is called.
Account::Constructor Function is called.
Person::Constructor Function is called.
Admin::Constructor Function is called.
Person::Constructor Function is called.
Account::Constructor Function is called.
Admin::Constructor Function is called.
Master::Constructor Function is called.
Show Function is called.
Person::Show Function is called.
NAME:王大夫  CODE:2012510
Show Function is called.
Account::Show Function is called.
NAME:王大夫 CODE:2012510 PAY:3654
Show Function is called.
Admin::Show Function is called.
NAME:王大夫 CODE:2012510 EXPERIENCE:跳舞
Show Function is called.
Master::Show Function is called.
NAME:王大夫 CODE:2012510 PAY:3654 EXPERIENCE:跳舞
CASE #3:
Person::Constructor Function is called.
Person::Constructor Function is called.
Account::Constructor Function is called.
Person::Constructor Function is called.
Admin::Constructor Function is called.
Person::Constructor Function is called.
Account::Constructor Function is called.
Admin::Constructor Function is called.
Master::Constructor Function is called.
Show Function is called.
Person::Show Function is called.
NAME:殷文琦  CODE:1005210
Show Function is called.
Account::Show Function is called.
NAME:殷文琦 CODE:1005210 PAY:5412
Show Function is called.
Admin::Show Function is called.
NAME:殷文琦 CODE:1005210 EXPERIENCE:写代码编程
Show Function is called.
Master::Show Function is called.
NAME:殷文琦 CODE:1005210 PAY:5412 EXPERIENCE:写代码编程

代码:

#include <iostream>
#include <string>
using namespace std;


class Person
{
public:
Person(string name,int code):name(name),code(code)
{
cout<<"Person::Constructor Function is called."<<endl;
}
virtual void Show()
{
cout<<"Person::Show Function is called."<<endl;
cout<<"NAME:"<<name<<"  CODE:"<<code<<endl;
}
string GetName()
{
return name;
}
int GetCode()
{
return code;
}
private:
string name;
int code;
};


class Account:virtual public Person
{
public:
Account(string name,int code,int pay):Person(name,code),pay(pay)
{
cout<<"Account::Constructor Function is called."<<endl;
}
void Show()
{
cout<<"Account::Show Function is called."<<endl;
cout<<"NAME:"<<GetName()<<" CODE:"<<GetCode()<<" PAY:"<<pay<<endl;
}
int GetPay()
{
return pay;
}
private:
int pay;
};


class Admin:virtual public Person
{
public:
Admin(string name,int code,string experience):Person(name,code),experience(experience)
{
cout<<"Admin::Constructor Function is called."<<endl;
}
void Show()
{
cout<<"Admin::Show Function is called."<<endl;
cout<<"NAME:"<<GetName()<<" CODE:"<<GetCode()<<" EXPERIENCE:"<<experience<<endl;
}
string GetExperience()
{
return experience;
}
private:
string experience;
};


class Master:public Account,public Admin
{
public:
Master(string name,int code,int pay,string experience):Person(name,code),Account(name,code,pay),Admin(name,code,experience)
{
cout<<"Master::Constructor Function is called."<<endl;
}
void Show()
{
cout<<"Master::Show Function is called."<<endl;
cout<<"NAME:"<<GetName()<<" CODE:"<<GetCode()<<" PAY:"<<GetPay()<<" EXPERIENCE:"<<GetExperience()<<endl;
}
};
void Show(Person* p)
{
cout<<"Show Function is called."<<endl;
p->Show();
}
int main()
{
    string name,experience;
    int code,pay,Cas=0;
    while(cin>>name>>code>>pay>>experience)
    {
        Cas++;
        cout<<"CASE #"<<Cas<<":"<<endl;
        Person person(name,code);
        Account account(name,code,pay);
        Admin admin(name,code,experience);
        Master master(name,code,pay,experience);
        Show(&person);
        Show(&account);
        Show(&admin);
        Show(&master);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40333952/article/details/80411883