BJFU oj C++实验(2)

本文详细介绍了BJFU在线判题平台的C++编程实验,包括TableTennisPlayer和RatedPlayer类的设计,Person和Student类的继承,图书商品的打折逻辑,Vehicle类的抽象及派生,以及长方体、圆柱体和球的表面积和体积计算。每个主题都提供了代码实现和测试用例。
摘要由CSDN通过智能技术生成

BJFU oj C++实验(2)

TableTennisPlayer

描述
编写TableTennisPlayer类和RatedPlayer类(RatedPlayer类继承TableTennisPlayer类),其中TableTennisPlayer类的定义如下所示:
class TableTennisPlayer{
private:
string firstname;
string lastname;
bool hasTable;
public:
TableTennisPlayer(const string &, const string &, bool);
string FirstName() const;
string LastName() const;
bool HasTable() const;
};

实现后,通过以下main函数的测试:
int main(){
string firstname, lastname;
bool hasTable;
int rating;
char flag;
while(cin>>flag){
if(flag==‘T’){
cin>>firstname>>lastname>>hasTable;
TableTennisPlayer tp(firstname,lastname,hasTable);
if(tp.HasTable())
cout<<tp.FirstName()<<" “<<tp.LastName()<<” has a table.\n";
else
cout<<tp.FirstName()<<" “<<tp.LastName()<<” hasn’t a table.\n";
} else if(flag==‘R’){
cin>>firstname>>lastname>>hasTable>>rating;
RatedPlayer rp(rating,firstname,lastname,hasTable);
if(rp.HasTable())
cout<<rp.FirstName()<<" “<<rp.LastName()<<” has a table. The rating is “<<rp.Rating()<<”.\n";
else
cout<<rp.FirstName()<<" “<<rp.LastName()<<” hasn’t a table. The rating is “<<rp.Rating()<<”.\n";
}
}
return 0;
}
输入
输入多行,每一行以’T’或’R’开头,'T’表示本行接下来输入一个TableTennisPlayer对象的信息,包括firstname,lastname和hasTable(是否有乒乓球台);'R’表示本行接下来输入一个RatedPlayer对象的信息,包括firstname,lastname,hasTable和rating(选手的得分)。
输出
一行输入对应一行输出,输出详见main函数
输入样例 1
T Bill Gates 1
输出样例 1
Bill Gates has a table.
输入样例 2
R Jike Zhang 0 19000
输出样例 2
Jike Zhang hasn’t a table. The rating is 19000.
提示
bool类型的输入:0表示false,1表示true

#include<iostream>
using namespace std;

class TableTennisPlayer
{
   
	private:
		string firstname;
		string lastname;
		bool hasTable;
	public:
		TableTennisPlayer(const string &, const string &, bool);
		string FirstName() const;
		string LastName() const;
		bool HasTable() const;
};

TableTennisPlayer::TableTennisPlayer(const string &f, const string &l, bool h) 
{
   
	firstname=f;
	lastname=l;
	hasTable=h;
}

string TableTennisPlayer::FirstName() const
{
   
	return firstname;
}

string TableTennisPlayer::LastName() const
{
   
	return lastname;
}

bool TableTennisPlayer::HasTable() const
{
   
	return hasTable;
}


class RatedPlayer:public TableTennisPlayer
{
   
	protected:
		int rating;
	public:
		RatedPlayer(int,const string &, const string &, bool);
		int Rating();
};

RatedPlayer::RatedPlayer(int r,const string &f, const string &l, bool h) :TableTennisPlayer(f,l,h)
{
   
	rating=r;
}

int RatedPlayer::Rating()
{
   
	return(rating);
}
 
int main()
{
   
	string firstname, lastname;
	bool hasTable;
	int rating;
	char flag;
	while(cin>>flag)
	{
   
		if(flag=='T')
			{
   
				cin>>firstname>>lastname>>hasTable;
				TableTennisPlayer tp(firstname,lastname,hasTable);
				if(tp.HasTable())
					cout<<tp.FirstName()<<" "<<tp.LastName()<<" has a table.\n";
				else
					cout<<tp.FirstName()<<" "<<tp.LastName()<<" hasn't a table.\n";
			
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值