题目
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

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

被折叠的 条评论
为什么被折叠?



