C++PrimerPlus:第十三章类和继承:编程练习总览

第十三章类和继承:练习总览

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第十三章类和继承:练习总览


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

例如:第十三章类和继承:练习总览


提示:以下是本篇文章正文内容,下面案例可供参考

一、题

以下面的类声明为基础

//base class
	class Cd	//represents a CD disk
	{
	private:
		char performers[50];
		char label[20];
		int selections;	//number of selections
		double playtime;//playing time in minutes
	public:
		Cd(char* s1, char* s2, int n, double x);
		Cd(const Cd& d);
		Cd();
		~Cd() {};
		void Report()const;//reports all CD data
		Cd& operator=(const Cd& d);
	}

派生出一个 Classic 类,并添加一组 char 成员,用于存储指出 CD 中主要作品的字符串。修改上述声明,使基类的所有函数都是虚的。如果上述定义声明的某个方法并不需要,则请删除它。使用下面的程序测试您的产品:


#include<iostream>
using namespace std;
void Bravo(const Cd& disk);
int main()
{
	Cd c1("Beatles", "Capitol", 14, 35.5);
	Classic c2 = Classic("Piano Sonata in B flat,Fantasia in c",
		"Alfred Brendel", "philips", 2, 57.17);
	Cd* pcd = &c1;

	cout << "Using object directly:\n";
	c1.Report();//use Cd method
	c2.Report();//use Classic method


	cout << "Using object directly:\n";
	pcd->Report();//use cd method for cd object
	pcd = &c2;
	pcd->Report();//use classic method ofr classic object
	Bravo(c1);
	Bravo(c2);

	cout << "Testing assignment: ";
	Classic copy;
	copy = c2;
	copy.Report();

	return 0;
}
void Bravo(const Cd& disk)
{
	disk.Report();
}

二、题

完成练习1,但让这两个类使用动态内存分配而不是长度固定的数据来记录字符串

代码如下(示例):

在这里插入代码片

三,题

修改baseDMA-lacksDMA-hasDMA类的层次,让三个类都从一个ABC派生而来,然后使用于程序清单13.10相类似的程序对结果进行测试,也就是说,它应使用ABC指针素组,并让用户决定要创建的对象类型,在类定义中添加virtual View()方法以处理数据显示

四,题

Benevolent Order Programmers用来维护瓶装葡萄酒箱,为描述它,BOP portmaster设置了一个Port类,其声明如下:

#include<iostream>
using namespace std;
class Port
{
private:
	char* brand;
	char style[20];
	int bottles;
public:
	Port(const char* br = "none", const char* st = "none", int b = 0);
	Port(const Port& p);
	virtual ~Port() { delete[]brand; }
	Port& operator=(const Port& p);
	Port& operator+=(int b);//adds b to bottles
	Port& operator-=(int b);//subtracts b from bottles,if available
	int BootleCount()const { return bottles; }
	virtual void Show()const;
	friend ostream& operator<<(ostream& os, const Port& p);
};

show()方法按下面的格式显示信息:
Brand:Gallo
Kind:tawnyBottles:20
operator<<()函数按下面的格式显示信息(末尾没有换行符):Gallo,tawny,20
PortMaster 完成了 Port 类的方法定义后派生了 VintagePort 类,然后被解职–因为不小心将一瓶 45度 Cockburn 泼到了正在准备烤肉调料的人身上,VintagePort 类如下所示:

//style necessarily = "vintage"
class VintagePort :public Port
{
private:
	char* nickname;
	int year;

public:
	VintagePort();
	VintagePort(const char* br, int b, const char* nn, int y);
	VintagePort(const VintagePort& vp);
	~VintagePort() { delete[]nickname ;}
	VintagePort& operator=(const VintagePort& vp);
	void Show()const;
	friend ostream& operator<<(ostream& os, const VintagePort& vp);
};

您被指定负责完成 VintagePort。

  • a.第一个任务是重新创建 Port 方法定义,因为前任被开除时销毁了方法定义
  • b.第二个任务是解释为什么有的方法重新定义了,而有些没有重新定义。
  • c.第三个任务是解释为何没有将operator-()和 operator<<( )声明为虚的。
  • d.第四个任务是提供 VintagePort 中各个方法的定义。

总结

提示:这里对文章进行总结:

1,本章练习太少。
2,后面我们加入C++ how to promram的内容

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值