C++ Primer Plus第六版编程题(第13章)

这篇博客介绍了C++ Primer Plus第六版中的编程练习,涉及派生类、动态内存管理和虚函数的应用。通过创建Classic类、使用动态内存分配以及设计Port和 VintagePort 类,讨论了如何重定义基类方法、处理对象类型的动态选择以及理解何时声明虚函数和友元函数。

C++ Primer Plus第六版编程题(第13章)

题目

1.以下面的类声明为基础:

{……代码省略……}

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

{……代码省略……}

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

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

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

{……代码省略……}

show()方法按下面的格式显示信息:

Brand: Gallo

Kind: tawny

Bottles: 20

operator<<()函数按下面的格式显示信息(末尾没有换行符):

Gallo, tawny, 20

PortMaster完成了Port类的方法定义后派生了VintagePort类,然后被解职——因为不小心将一瓶45度Cockburn泼到了正在准备烤肉调料的人身上,VintagePort类如下所示:

{……代码省略……}

您被指定负责完成VintagePort。

a. 第一个任务是重新创建Port方法定义,因为前任被开除时销毁了方法定义。

b. 第二个任务是解释为什么有的方法重新定义了,而有些没有重新定义。

c. 第三个任务是解释为何没有将operator=()和operator<<()声明为虚的。

d. 第四个任务是提供VintagePort中各个方法的定义。

程序

//classic.h
#ifndef CLASSIC_H_
#define CLASSIC_H_
#include <iostream>

class Cd
{
   
   
private:
	char performers[50];
	char label[20];
	int selections;
	double playtime;
public:
	Cd(char *s1,char *s2,int n,double x);
//	Cd(const Cd &d);
	Cd();
	virtual ~Cd();
	virtual void Report() const;
	Cd &operator=(const Cd &d);
};

class Classic:public Cd
{
   
   
private:
	char works[40];
public:
	Classic(char *s0,char *s1,char *s2,int n,double x);
	Classic(char *s,const Cd &d);
	Classic();
	virtual void Report()const;
	Classic &operator=(const Classic &d);
};
#endif
//classic.cpp
#include <iostream>
#include <cstring>
using namespace std;
#include "classic.h"

Cd::Cd(char *s1,char *s2,int n,double x)
{
   
   
	strcpy(performers,s1);
	strcpy(label,s2);
	selections=n;
	playtime=x;
}
//Cd::Cd(const Cd &d)
//{
   
   
//	strcpy(performers,d.performers);
//	strcpy(label,d.label);
//	selections=d.selections;
//	playtime=d.playtime;
//}
Cd::Cd()
{
   
   
	strcpy(performers,"null");
	strcpy(label,"null");
	selections=0;
	playtime=0;
}
Cd::~Cd()
{
   
   
}
void Cd::Report() const
{
   
   
	cout<<"The performers: "<<performers<<endl;
	cout<<"The label: "<<label<<endl;
	cout<<"The selections: "<<selections<<endl;
	cout<<"The playtime: "<<playtime<<endl;
	cout<<endl;
}
Cd &Cd::operator=(const Cd &d)
{
   
   
	if(this==&d)
		return *this;
	strcpy(performers,d.performers);
	strcpy(label,d.label);
	selections=d.selections;
	playtime=d.playtime;
	return *this;
}

Classic::Classic(char *s0,char *s1,char *s2,int n,double x)
	:Cd(s1,s2,n,x)
{
   
   
	strcpy(works,s0);
}
Classic::Classic(char *s,const Cd &d)
	:Cd(d)
{
   
   
	strcpy(works,s);
}
Classic::Classic()
	:Cd()
{
   
   
	strcpy(works,"null");
}
void Classic::Report()const
{
   
   
	cout<<"The works: "<<works<<endl;
	Cd::Report();
}
Classic &Classic::operator=(const Classic &d)
{
   
   
	if(this==&d)
		return *this;
	Cd::operator=(d);
	strcpy(works,d.works);
	return *this;
}
//main.cpp
#include <iostream>
using namespace std;
#include "classic.h"
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();
	c2.Report();
	
	cout<<"Using type cd *pointer to objects:\n";
	pcd->Report();
	pcd=&c2;
	pcd->Report();
	
	cout<<"Calling a function with a Cd reference argument:\n";
	Bravo(c1);
	Bravo(c2);
	
	cout<<"Testing assignment: ";
	Classic copy;
	copy=c2;
	copy.Report();
	 
	return 0;
}
void Bravo(const Cd &disk)
{
   
   
	disk.Report();
}
//classic2.h
#ifndef CLASSIC_H_
#define CLASSIC_H_
#include <iostream>

class Cd
{
   
   
private:
	char *performers;
	char *label;
	int selections;
	double playtime;
public:
	Cd(char *s1,char *s2,int n,double x);
	Cd(const Cd &d);
	Cd();
	virtual ~Cd();
	virtual void 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值