OOP 谁是名人堂球员(多重继承)

【id:274】【16分】C. OOP 谁是名人堂球员(多重继承)
时间限制
1s
内存限制
128MB
题目描述

1、建立如下的类继承结构:

1)一个球员类Player作为基类,具有name、height、weight等数据成员,display()等成员函数

2)从Player类派生出最有价值球员类MVP,添加属性:获奖年year

3)从Player类派生出最佳防守球员类DPOY,添加属性:获奖年year

4)从MVP和DPOY派生出名人堂类HallOfFame

2、分别定义以上类的构造函数、输出函数display及其他函数(如需要)。

3、在主函数中定义各种类的对象,并测试之,通过对象调用display函数产生输出。


输入

第一行输入球员的名字,身高,体重

第二行输入MVP获奖年

第三行输入DPOY获奖年


输出

构造四个类对象,并按照如下格式进行输出。


样例查看模式 
正常显示
查看格式
输入样例1 <-复制
Michael 198 96
2010
2011
输出样例1
Player:
name:Michael
height:198
weight:96

MVP:
name:Michael
height:198
weight:96
reward:win the MVP reward in 2010

DPOY:
name:Michael
height:198
weight:96
reward:win the DPOY reward in 2011

Hall of fame:
name:Michael
height:198
weight:96
reward1:win the MVP reward in 2010
reward2:win the DPOY reward in 2011

在输出时,每一个都需要在题目中cv,然后在代码中裁剪

reward1:win the MVP reward in 2010

reward2:win the DPOY reward in 2011

#include "iostream"

using namespace std;

// 1)一个球员类 Player 作为基类,具有name、height、weight 等数据成员,display()等成员函数
class Player {
public:
    string name;
    int height;
    int weight;

    Player(const string &name, int height, int weight) : name(name), height(height), weight(weight) {}

    void display() {
        cout << "Player:" << endl;
        cout << "name:" << name << endl;
        cout << "height:" << height << endl;
        cout << "weight:" << weight << endl;
    }
};

// 2)从Player类派生出最有价值球员类MVP,添加属性:获奖年year
class MVP : virtual public Player {
public:
    int year;

    MVP(const string &name, int height, int weight, int year) : Player(name, height, weight), year(year) {}

    void display() {
        cout << "MVP:" << endl;
        cout << "name:" << name << endl;
        cout << "height:" << height << endl;
        cout << "weight:" << weight << endl;
        cout << "reward:win the MVP reward in " << year << endl;
    }
};

// 3)从Player类派生出最佳防守球员类 DPOY,添加属性:获奖年year
class DPOY : virtual public Player {
public:
    int year;

    DPOY(const string &name, int height, int weight, int year) : Player(name, height, weight), year(year) {}

    void display() {
        cout << "DPOY:" << endl;
        cout << "name:" << name << endl;
        cout << "height:" << height << endl;
        cout << "weight:" << weight << endl;
        cout << "reward:win the DPOY reward in " << year << endl;
    }
};

// 4)从MVP和DPOY派生出名人堂类 HallOfFame
class HallOfFame : virtual public MVP, virtual public DPOY {
public:
    HallOfFame(const string &name, int height, int weight, int one, int two) : Player(name, height, weight),
                                                                               MVP(name, height, weight, one),
                                                                               DPOY(name, height, weight, two) {}

    void display() {
        cout << "Hall of fame:" << endl;
        cout << "name:" << name << endl;
        cout << "height:" << height << endl;
        cout << "weight:" << weight << endl;
        cout << "reward1:win the MVP reward in " << MVP::year << endl;
        cout << "reward2:win the DPOY reward in " << DPOY::year << endl;
    }
};

int main() {
    string name;
    int height;
    int weight;
    cin >> name >> height >> weight;
    int one, two;
    cin >> one >> two;
    Player player(name, height, weight);
    MVP mvp(name, height, weight, one);
    DPOY dpoy(name, height, weight, two);
    HallOfFame hallOfFame(name, height, weight, one, two);
    player.display();
    cout << endl;
    mvp.display();
    cout << endl;
    dpoy.display();
    cout << endl;
    hallOfFame.display();
}

猜你喜欢

转载自blog.csdn.net/m0_62288512/article/details/131580446
OOP
今日推荐