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";