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& firstname, const string& lastname, bool hastable);
string FirstName() const;
string LastName() const;
bool HasTable() const;
};
TableTennisPlayer::TableTennisPlayer(const string& firstname, const string& lastname, bool hastable) {
this->firstname = firstname;
this->lastname = lastname;
hasTable = hastable;
}
string TableTennisPlayer::FirstName() const{
return this->firstname;
}
string TableTennisPlayer::LastName() const {
return this->lastname;
}
bool TableTennisPlayer::HasTable() const {
return this->hasTable;
}
class RatedPlayer :public TableTennisPlayer {
private:
string firstname;
string lastname;
bool hasTable;
int rating;
public:
RatedPlayer(int rating, string firstname, string lastname, bool hasTable);
int Rating();
string FirstName() const;
string LastName() const;
};
RatedPlayer::RatedPlayer(int rating, string firstname, string lastname, bool hasTable) :TableTennisPlayer(firstname, lastname, hasTable) {
this->rating = rating;
this->firstname = firstname;
this->lastname = lastname;
}
int RatedPlayer::Rating() {
return this->rating;
}
string RatedPlayer::FirstName() const{
return this->firstname;
}
string RatedPlayer::LastName() const {
return this->lastname;
}
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;
}
Person和Student
描述
实现一个Person类,再实现一个Student类,要求Student类继承Person类,通过以下测试:
int main()
{
Person * p;
p = new Person;
p->input();
p->display();
delete p;
p = new Student;
p->input();
p->display();
delete p;
return 0;
}
输入
输入包含两行,第一行为一个姓名(不包含空格);第二行为一个学号和一个姓名(学号、姓名都不包含空格),学号和姓名之间用空格间隔
输出
输出为两行,第一行为一个姓名;第二行为学号和姓名,学号和姓名之间用空格间隔
输入样例 1
Mary 001 Mary
输出样例 1
Mary 001 Mary
提示
不能修改main函数,否则不得分
#include <iostream>
using namespace std;
class Person
{
public:
virtual void input();
virtual void display();
protected:
string name;
};
void Person::input() {
cin >> name;
}
void Person::display() {
cout << name << endl;
}
class Student :public Person
{
private:
string no;
public:
void display();
void input();
};
void Student::input()
{
cin >> this->no >> this->name;
}
void Student::display()
{
cout << no << " " << name << endl;
}
int main()
{
Person* p;
p = new Person;
p->input();
p->display();
delete p;
p = new Student;
p->input();
p->display();
delete p;
return 0;
}
图书商品
描述
编写两个类,分别是:
class Item_base //未打折的图书商品
{
protected:
string ISBN; //图书序列号
double price; //单价
public:
Item_base(const string & book_ISBN = "", double sales_price = 0.0);
string get_ISBN() const;
virtual double net_price(int) const; //返回购买指定数量的图书的总价
virtual ~Item_base();
};
第二个类是:
class Bulk_Item : public Item_base //根据购买数量打折
{
public:
Bulk_Item(const string & book_ISBN = "", double sales_price = 0.0, int min_qty = 0, double discount = 0.0);
double net_price(int) const; //返回根据购买数量打折后的总价
private:
int min_qty; // 买够这个数量可以打相应的折扣
double discount; //折扣
};
实现以上两个类,通过下面main函数的测试
int main()
{
Item_base book("0-001-0001-1", 10.0);
Bulk_Item bulk1("0-001-0001-1",10.0, 5, 0.1);
Bulk_Item bulk2("0-001-0001-1", 10.0, 10, 0.2);
int num;
while (cin >> num)
{
cout << bulk1.get_ISBN() << "\t" << num << "\t";
Item_base * p;
if (num >= 10) p = &bulk2;
else if (num >= 5) p = &bulk1;
else p = &book;
cout << p->net_price(num) << "\n";
}
return 0;
}
输入
图书的数量。
输出
输出购买的图书的ISBN,它的数量以及总的价格。(用main函数中输出的形式即可)
输入样例 1
2 6 11
输出样例 1
0-001-0001-1 2 20 0-001-0001-1 6 54 0-001-0001-1 11 88
#include <iostream>
using namespace std;
class Item_base //未打折的图书商品
{
protected:
string ISBN; //图书序列号
double price; //单价
public:
Item_base(const string& book_ISBN = "", double sales_price = 0.0);
string get_ISBN() const;
virtual double net_price(int) const; //返回购买指定数量的图书的总价
virtual ~Item_base();
};
Item_base::Item_base(const string& book_ISBN, double sales_price) {
ISBN = book_ISBN;
price = sales_price;
}
string Item_base::get_ISBN()const {
return ISBN;
}
double Item_base::net_price(int num)const {
return price*num;
}
Item_base::~Item_base() {}
class Bulk_Item : public Item_base //根据购买数量打折
{
public:
Bulk_Item(const string& book_ISBN = "", double sales_price = 0.0, int min_qty = 0, double discount = 0.0);
double net_price(int) const; //返回根据购买数量打折后的总价
private:
int min_qty; // 买够这个数量可以打相应的折扣
double discount; //折扣
};
Bulk_Item::Bulk_Item(const string& book_ISBN, double sales_price, int min_qty, double discount) :Item_base(book_ISBN, sales_price) {
this->min_qty = min_qty;
this->discount = discount;
}
double Bulk_Item::net_price(int num) const {
return (1 - discount) * price * num;
}
int main()
{
Item_base book("0-001-0001-1", 10.0);
Bulk_Item bulk1("0-001-0001-1", 10.0, 5, 0.1);
Bulk_Item bulk2("0-001-0001-1", 10.0, 10, 0.2);
int num;
while (cin >> num)
{
cout << bulk1.get_ISBN() << "\t" << num << "\t";
Item_base* p;
if (num >= 10) p = &bulk2;
else if (num >= 5) p = &bulk1;
else p = &book;
cout << p->net_price(num) << "\n";
}
return 0;
}
Vehicle类
描述
设计一个抽象类Vehicle,由它派生出类Car和类Truck,类Car包含名称、颜色和载客数三个数据成员,类Truck包含名称、颜色和载重量三个数据成员。
使用如下函数通过测试:
int main()
{
Vehicle *p;
char type;
char name[110],color[110];
int pas;
double cap;
while(cin>>type)
{
cin>>name>>color;
if(type == 'C')
{
cin>>pas;
Car car(name,color,pas);
p = &car;
p->display();
}
else if(type == 'T')
{
cin>>cap;
Truck truck(name,color,cap);
p = &truck;
p->display();
}
}
return 0;
}
输入
多组输入,每组输入的开头是'C'或者'T','C'表示Car,'T'表示Truck,接下来输入Car或Truck的信息,Car的信息包括名称,颜色和载客数,Truck的信息包括名称,颜色和载重量。测试时,用抽象类Vehicle的指针指向派生类对象,实现不同类中display()函数的多态。
输出
根据不同车种类,输出不同信息,具体见样例输出。
输入样例 1
C Benz black 3 T Dongfeng white 8.5
输出样例 1
Car name:Benz Car color:black Car passenger:3 Truck name:Dongfeng Truck color:white Truck capacity:8.5
提示
Vehicle中可包含名称和颜色数据成员,并且有纯虚函数以提供接口完成信息的显示;在派生类Car和Truck中根据需要实现纯虚函数以及添加成员。
仔细读输出样例的格式,输出的不同项目之间用一个空格隔开。
#include <iostream>
using namespace std;
class Vehicle {
protected:
string name;
string color;
public:
Vehicle(string name, string color);
virtual void display();
};
class Car :public Vehicle {
private:
int pasger;
public:
Car(string name,string color, int pasger);
~Car();
void display();
};
class Truck :public Vehicle {
private:
double weight;
public:
Truck(string name,string color,double weight);
~Truck();
void display();
};
Vehicle::Vehicle(string name, string color)
{
}
void Vehicle::display()
{
}
Car::Car(string name, string color, int pasger):Vehicle(name,color)
{
this->pasger = pasger;
}
void Car::display()
{
cout << "Car name:" << name << " Car color:" << color << " Car passenger:" << pasger << endl;
}
Car::~Car()
{
}
Truck::Truck(string name, string color, double weight):Vehicle(name,color)
{
this->weight = weight;
}
void Truck::display()
{
cout << "Truck name:" << name << " Truck color:" << color << " Truck capacity:" << weight << endl;
}
Truck::~Truck()
{
}
int main()
{
Vehicle* p;
char type;
char name[110], color[110];
int pas;
double cap;
while (cin >> type)
{
cin >> name >> color;
if (type == 'C')
{
cin >> pas;
Car car(name, color, pas);
p = &car;
p->display();
}
else if (type == 'T')
{
cin >> cap;
Truck truck(name, color, cap);
p = &truck;
p->display();
}
}
return 0;
}
表面积和体积
描述
编写程序,计算长方体、圆柱体和球的表面积和体积。要求先定义一个抽象类Shape如下:
class Shape {
public:
Shape() {}
virtual double area() = 0;
virtual void input() = 0;
virtual double volume() = 0;
virtual ~Shape() {}
};
使用Shape类派生出长方体类、圆柱体类、球类,在这三个类里实现从Shape类继承来的纯虚函数。使用如下代码通过测试。
void work(Shape *s) {
s->input();
cout << s->area() << " " << s->volume() << endl;
delete s;
}
int main() {
char c;
while (cin >> c) {
switch (c) {
case 'y':
work(new Cylinder());
break;
case 'c':
work(new Cuboid());
break;
case 'q':
work(new Ball());
break;
default:
break;
}
}
return 0;
}
输入
输入包含多行,每行首先是一个字符'c','y','q',分别表示输入长方体、圆柱体或球的信息,接下来是对应的输入。
输出
每行输入对应一行输出,表示该形状的表面积和体积,以空格分隔。
输入样例 1
c 3 4 5 y 3 5 q 5
输出样例 1
94 60 150.796 141.372 314.159 523.599
提示
pi的精度要足够,比如使用 const double pi = acos(-1);
#include <iostream>
#include <cmath>
using namespace std;
const double pi = acos(-1);
class Shape {
public:
Shape() {}
virtual double area() = 0;
virtual void input() = 0;
virtual double volume() = 0;
virtual ~Shape() {}
};
class Cylinder :public Shape {
private:
double r, h;
public:
virtual double area();
virtual void input();
virtual double volume();
};
void Cylinder::input() {
cin >> r >> h;
}
double Cylinder::area() {
return pi * r * r * 2 + 2 * pi * r * h;
}
double Cylinder::volume() {
return pi * r * r * h;
}
class Cuboid :public Shape {
private:
double x, y, z;
public:
virtual double area();
virtual void input();
virtual double volume();
};
void Cuboid::input() {
cin >> x >> y >> z;
}
double Cuboid::area() {
return 2 * (x * y + x * z + z * y);
}
double Cuboid::volume() {
return x * y * z;
}
class Ball :public Shape {
private:
double r;
public:
virtual double area();
virtual void input();
virtual double volume();
};
void Ball::input() {
cin >> r;
}
double Ball::area() {
return 4 * pi * r * r;
}
double Ball::volume() {
return 4 * pi * r * r * r / 3;
}
void work(Shape* s) {
s->input();
cout << s->area() << " " << s->volume() << endl;
delete s;
}
int main() {
char c;
while (cin >> c) {
switch (c) {
case'y':
work(new Cylinder());
break;
case'c':
work(new Cuboid());
break;
case'q':
work(new Ball());
break;
default:
break;
}
}
return 0;
}