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

题目描述

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获奖年

输出

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

输入样例

Michael 198 96
2010
2011

输出样例

Player:
name:Michael
height:198
weight:96

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

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

代码

#include<iostream>
using namespace std;
class Player{
	protected:
	 	string name;
		int height,weight;
	public:
		Player(){}	
		Player(string n,int h,int w):name(n),height(h),weight(w){
		}
		void display(){
			cout<<"name:"<<name<<endl;
			cout<<"height:"<<height<<endl;
			cout<<"weight:"<<weight<<endl;
		}		
};
class MVP:virtual public Player{
	private:
		int year1;
	public:
		MVP(){}
		MVP(int y):year1(y){}
		int getyear(){
			return year1;
		}
		void display(){
			cout<<"reward:win the MVP reward in "<<year1<<endl;
		}
};
class DPOY:virtual public Player{
	private:
		int year2;
	public:
		DPOY(){}
		DPOY(int y):year2(y){}
		int getyear(){
			return year2;
		}
		void display(){
			cout<<"reward:win the DPOY reward in "<<year2<<endl;
		}
};
class HallOfFame:public MVP,public DPOY{
	public:
		HallOfFame(){}
		void display(MVP &m,DPOY &d){
			cout<<"reward1:win the MVP reward in "<<m.getyear()<<endl;
			cout<<"reward2:win the DPOY reward in "<<d.getyear();
		}
};
int main()
{
	string n;
	int h,w,y;
	cin>>n>>h>>w;
	cout<<"Player:"<<endl;
	Player player(n,h,w);
	player.display();
	cout<<endl;
	
	cin>>y;
	MVP pp(y);
	cout<<"MVP:"<<endl;
	player.display();
	pp.display();
	cout<<endl;
	
	cin>>y;
	DPOY dd(y);
	cout<<"DPOY:"<<endl;
	player.display();
	dd.display();
	cout<<endl;
	
	HallOfFame hh;
    cout<<"Hall of fame:"<<endl;
	player.display();
	hh.display(pp,dd);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值