这个比上一个简单多了,做oj做到心态炸裂
A 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
AC代码
//
// main.cpp
// 1
//
// Created by Joth Jiang on 2021/6/26.
//
#include <iostream>
#include <cstring>
using namespace std;
class TableTennisPlayer
{
protected:
string firstname;
string lastname;
bool hasTable;
public:
TableTennisPlayer(){}
TableTennisPlayer(const string &a, const string &b, bool c){
firstname=a;
lastname=b;
hasTable=c;
};
string FirstName() const{
return firstname;
};
string LastName() const{
return lastname;
};
bool HasTable() const{
return hasTable;
};
};
class RatedPlayer:public TableTennisPlayer{
private:
int rating;
public:
RatedPlayer(){}
RatedPlayer(int,const string &,const string &,bool );