实验三虚继承

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>
#include <string>
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::LastName() const
{
    return lastname;
}
 
string TableTennisPlayer::FirstName() const
{
    return firstname;
}
 
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";
        }
        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
{
protected:
	string name;
public:
	virtual void input();
	virtual void display();
};
void Person::input()
{
	string n;
	cin >> n;
	name = n;
}
void Person::display()
{
	cout << name << endl;
}
 
class Student :public Person
{
private:
	string ID;
public:
	void input();
	void display();
 
};
 
void Student::input()
{
	string m;
	string n;
	cin >> m >> n;
	ID = m;
	name = n;
}
void Student::display()
{
	cout << ID << ' ' << 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>
#include<string>
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
{
	double pr;
	pr = num * price;
	return pr;
}
 
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) //注意是变量名不带int等类型名 
{
	this->min_qty = min_qty;
	this->discount = discount;
 
}
 
double Bulk_Item::net_price(int n) const
{
	double pr;
	pr = price * n * (1 - discount);
	return(pr);
}
 
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, string);
	virtual void display() {}
};
 
Vehicle::Vehicle(string n, string c)
{
	name = n;
	color = c;
}
 
class Car :public Vehicle
{
protected:
	int people;
public:
	Car(string, string, int);
	void display();
};
 
Car::Car(string n, string c, int p) :Vehicle(n, c)
{
	people = p;
}
 
void Car::display()
{
	cout << "Car name:" << name << " Car color:" << color << " Car passenger:" << people << endl;
}
 
 
class Truck :public Vehicle
{
protected:
	double weight;
public:
	Truck(string, string, double);
	void display();
 
};
 
Truck::Truck(string n, string c, double w) :Vehicle(n, c)
{
	weight = w;
}
 
void Truck::display()
{
	cout << "Truck name:" << name << " Truck color:" << color << " Truck capacity:" << weight << endl;
}
int main()
 
{
 
	Vehicle* p;
 
	char type;
 
	char name[110], color[110];
 
	int car_;
 
	double tru_;
 
	while (cin >> type)
 
	{
 
		cin >> name >> color;
 
		if (type == 'C')
 
		{
 
			cin >> car_;
 
			Car car(name, color, car_);
 
			p = &car;
 
			p->display();
 
		}
 
		else if (type == 'T')
 
		{
 
			cin >> tru_;
 
			Truck truck(name, color, tru_);
 
			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() {}
protected:
	double a, b, c, radius,radius1, high;
};
 
 
class Cylinder :public Shape
{
public:
	void input()
	{
		cin >> radius >> high;
	 }
	double area()
	{
		return 2 * pi * radius * radius + 2 * pi * radius * high;
	}
	double volume()
	{
		return pi * radius * radius * high;
	}
};
class Cuboid :public Shape
{
public:
	void input()
	{
		cin >> a >> b >> c;
	}
	double area()
	{
		return 2*(a * b + a * c + b * c);
	}
	double volume()
	{
		return a * b * c;
	}
};
 
class Ball :public Shape
{
public:
	void input()
	{
		cin >> radius1;
	}
	double area()
	{
		return 4 * pi * radius1 * radius1;
	}
	double volume()
	{
		return 4.0 / 3.0 * pi * radius1 * radius1 * radius1;
	}
};
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;
 
}
Share

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值