C++ Primier Plus(第六版) 第十三章 类继承 编程练习答案
1.以下面的类声明为基础:
// base class
class Cd { // repersents 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中主要作品的字符串。修改上述声明,使基类的所有函数都是虚的。如果上述定义声明的某个方法并不需要,则请删除它。使用下面的程序测试您的产品:
// useclassic.cpp -- test the Classic class
// compile with classic.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(); // use Cd method
c2.Report(); // use Classic method
cout << "Using type cd * pointer to objects:\n";
pcd->Report(); // use Cd method for cd object
pcd = &c2;
pcd->Report(); // use Classic method for class object
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();
}
本题不算困难,由于没有使用new分配动态空间,因此不需要显式复制构造函数、析构函数和赋值运算符函数。由于要是想Report()函数的动态联编,因此该函数为virtual关键字,(ps:构造函数使用字符串常指针参数编译错误,改成字符串指针)代码入下:
// classic.h -- definition of Classic class
#ifndef CLASSIC_H_
#define CLASSIC_H_
// base class
class Cd { // repersents 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();
virtual void Report() const; // reports all CD data
};
class Classic : public Cd
{
private:
char song[60];
public:
Classic(char * fn, char * pf, char * lb, int sel, double pt);
Classic();
virtual void Report() const;
};
#endif
// classic.cpp -- methods for Classic class
#include <iostream>
#include <cstring>
#include "classic.h"
// methods of Cd
Cd::Cd(char * s1, char * s2, int n, double x)
{
strcpy(performers, s1);
strcpy(label, s2);
selections = n;
playtime = x;
}
Cd::Cd()
{
}
void Cd::Report() const
{
using std::cout;
using std::endl;
cout << "Performers: " << performers << endl;
cout << "Label: " << label << endl;
cout << "Selections: " << selections << endl;
cout << "Playtime: " << playtime << endl;
}
// methods of Classic
Classic::Classic(char * fn, char * pf, char * lb,
int sel, double pt) : Cd(pf, lb, sel, pt)
{
strcpy(song, fn);
}
Classic::Classic()
{
}
void Classic::Report() const
{
std::cout << "Song: " << song << std::endl;
Cd::Report();
}
主函数测试文件使用题目给出的,运行结果如下:
2. 完成练习1,但让两个类使用动态内存分配,而不是长度固定的数组来记录字符串。
本题使将练习1的静态数组变成动态分配内存,首先修改数据成员,修改完成之后修改实现构造函数,注意基类析构函数应该为虚析构函数,添加复制构造函数,赋值运算符函数。ps:复制构造函数不需要释放指针空间,赋值运算符函数需要。代码如下:
// classic.h -- definition of Classic class
#ifndef CLASSIC_H_
#define CLASSIC_H_
// base class
class Cd { // repersents a CD disk
private:
char * performers;
char * label;
int selections; // number of selections
double playtime; // playing time in minutes
public:
Cd(const char * s1, const char * s2, int n, double x);
Cd(const Cd & d);
Cd();
virtual ~Cd();
virtual void Report() const; // reports all CD data
Cd & operator=(const Cd & d);
};
class Classic : public Cd
{
private:
char * song;
public:
Classic(const char * sg, const char * pf, const char * lb, int sel, double pt);
Classic(const Classic & c);
Classic();
~Classic();
virtual void Report() const;
Classic & operator=(const Classic & c);
};
#endif
// classic.cpp -- methods for Classic class
#include <iostream>
#include <cstring>
#include "classic.h"
// methods of Cd
Cd::Cd(const char * s1, const char * s2, int n, double x)
{
performers = new char[strlen(s1) + 1];
strcpy(performers, s1);
label = new char[strlen(s2) + 1];
strcpy(label, s2);
selections = n;
playtime = x;
}
Cd::Cd(const Cd & d)
{
performers = new char[strlen(d.performers) + 1];
strcpy(performers, d.performers);
label = new char[strlen(d.label) + 1];
strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
}
Cd::Cd()
{
performers = new char[1];
performers[0] = '\0';
label = new char[1];
label[0] = '\0';
selections = 0;
playtime = 0.0;
}
Cd::~Cd()
{
delete [] performers;
delete [] label;
}
void Cd::Report() const
{
using std::cout;
using std::endl;
cout << "Performers: " << performers << endl;
cout << "Label: " << label << endl;
cout << "Selections: " << selections << endl;
cout << "Playtime: " << playtime << endl;
}
Cd & Cd::operator=(const Cd & d)
{
if (this == &d)
return *this;
else
{
delete [] performers;
performers = new char[strlen(d.performers) + 1];
strcpy(performers, d.performers);
delete [] label;
label = new char[strlen(d.label) + 1];
strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
return *this;
}
}
// methods of Classic
Classic::Classic(const char * sg, const char * pf, const char * lb,
int sel, double pt) : Cd(pf, lb, sel, pt)
{
song = new char[strlen(sg) + 1];
strcpy(song, sg);
}
Classic::Classic(const Classic & c) : Cd(c)
{
song = new char[strlen(c.song) + 1];
strcpy(song, c.song);
}
Classic::Classic():Cd()
{
song = new char[1];
song[0] = '\0';
}
Classic::~Classic()
{
delete [] song;
}
void Classic::Report() const
{
std::cout << "Song: " << song << std::endl;
Cd::Report();
}
Classic & Classic::operator=(const Classic & c)
{
if (this == &c)
return *this;
else
{
delete [] song;
song = new char[strlen(c.song) + 1];
strcpy(song, c.song);
Cd::operator=(c);
return *this;
}
}
// useclassic.cpp -- test the Classic class
// compile with classic.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(); // use Cd method
c2.Report(); // use Classic method
cout << "Using type cd * pointer to objects:\n";
pcd->Report(); // use Cd method for cd object
pcd = &c2;
pcd->Report(); // use Classic method for class object
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();
}
运行结果同上,这里不在提供。
3. 修改baseDMA-lacksDMA-hasDMA类层次,让三个类都从一个ABC派生而来,然后使用与程序清单13.10相似的程序对结果进行测试。也就是说,它应使用ABC指针数组,并让用户决定要创建的对象类型。在类定义中添加virtual View()方法以处理数据显式。
本题是创建一个抽象类,利用抽象类派生出三个具体类。抽象类需要一个纯虚函数,这里使用View()=0来使定义出的DMAABC称为抽象类,这里的代码没有困难。Ps:写主函数时,忽略的字符串数字混合输入,需要将将数字的换行符吸收,加上.get()解决;还有就是删除指针时添加[]导致不匹配错误,删除[]解决样例代码如下:
// dma.h -- inheritance and dynamic memory allocation
#ifndef DMA_H_
#define DMA_H_
# include <iostream>
// ABC Class using DMA
class DMAABC
{
private:
char * label;
int rating;
public:
DMAABC(const char * l = "null", int r = 0);
DMAABC(const DMAABC & rda);
virtual ~DMAABC();
DMAABC & operator=(const DMAABC & rda);
virtual void View() const =0;
};
// BaseDMA class definition
class baseDMA : public DMAABC
{
public:
baseDMA(const char * l = "null", int r = 0) : DMAABC(l, r) { }
virtual void View() const;
};
// derived class without DMA
// no destructor needed
// uses implicit copy constructor
// uses implicit assignment operator
class lacksDMA : public DMAABC
{
private:
enum{ COL_LEN = 40};
char color[COL_LEN];
public:
lacksDMA(const char * c = "blank", const char * l = "null",
int r = 0);
lacksDMA(const char * c, const baseDMA & rs);
virtual void View() const;
};
// derived class with DMA
class hasDMA : public DMAABC
{
private:
char * style;
public:
hasDMA(const char * s = "none", const char * l = "null",
int r = 0);
hasDMA(const char * s, const baseDMA & rs);
hasDMA(const hasDMA & hs);
~hasDMA();
hasDMA & operator=(const hasDMA & hs);
virtual void View() const;
};
#endif
// dma.cpp -- methods for Class DMAABC
#include <cstring>
#include "dma.h"
// methods for DMAABC class
DMAABC::DMAABC(const char * l, int r)
{
label = new char[strlen(l) + 1];
strcpy(label, l);
rating = r;
}
DMAABC::DMAABC(const DMAABC & rda)
{
label = new char[strlen(rda.label) + 1];
strcpy(label, rda.label);
rating = rda.rating;
}
DMAABC::~DMAABC()
{
delete [] label;
}
DMAABC & DMAABC::operator=(const DMAABC & rda)
{
if (this == &rda)
return *this;
else
{
delete [] label;
label = new char[strlen(rda.label) + 1];
strcpy(label, rda.label);
rating = rda.rating;
return *this;
}
}
void DMAABC::View() const
{
std::cout << "Lable: " << label << std::endl;
std::cout << "Rating: " << rating << std::endl;
}
// methods for BaseDMA class
void baseDMA::View() const
{
DMAABC::View();
}
// methods for lacksDMA class
lacksDMA::lacksDMA(const char * c, const char * l, int r) : DMAABC(l, r)
{
strcpy(color, c);
}
lacksDMA::lacksDMA(const char * c, const baseDMA & rs) : DMAABC(rs)
{
strcpy(color, c);
}
void lacksDMA::View() const
{
std::cout << "Color: " << color << std::endl;
DMAABC::View();
}
// derived class with DMA
hasDMA::hasDMA(const char * s, const char * l, int r) : DMAABC(l, r)
{
style = new char[strlen(s) + 1];
strcpy(style, s);
}
hasDMA::hasDMA(const char * s, const baseDMA & rs) : DMAABC(rs)
{
style = new char[strlen(s) + 1];
strcpy(style, s);
}
hasDMA::hasDMA(const hasDMA & hs) : DMAABC(hs)
{
style = new char[strlen(hs.style) + 1];
strcpy(style, hs.style);
}
hasDMA::~hasDMA()
{
delete [] style;
}
hasDMA & hasDMA::operator=(const hasDMA & hs)
{
if (this == &hs)
return *this;
else
{
DMAABC::operator=(hs);
delete [] style;
style = new char[strlen(hs.style) + 1];
strcpy(style, hs.style);
return * this;
}
}
void hasDMA::View() const
{
std::cout << "Style: " << style << std::endl;
DMAABC::View();
}
#include "dma.h"
const int MAX = 3;
const int CMAX = 40;
int main()
{
using std::cin;
using std::cout;
using std::endl;
char temp1[CMAX];
int tempr;
char kind;
DMAABC * p_dma[MAX];
for(int i = 0; i < MAX; i++)
{
cout << "Enter the label: ";
cin.getline(temp1,CMAX);
cout << "Enter the rating: ";
(cin >> tempr).get();
cout << "Enter 1 for baseDMA or "
<< "2 for lacksDMA or "
<< "3 for hasDMA: ";
while (cin >> kind &&(kind != '1' && kind != '2' && kind != '3'))
{
cout << "Enter either 1 or 2 or 3: ";
}
while(cin.get() != '\n')
continue;
if(kind == '1')
p_dma[i] = new baseDMA(temp1, tempr);
else if (kind == '2')
{
char temp2[CMAX];
cout << "Enter the color: ";
cin.getline(temp2,CMAX);
p_dma[i] = new lacksDMA(temp2, temp1, tempr);
}
else
{
char temp2[CMAX];
cout << "Enter the style: ";
cin.getline(temp2,CMAX);
p_dma[i] = new hasDMA(temp2, temp1, tempr);
}
}
cout << endl;
for(int i = 0; i < MAX; i++)
{
p_dma[i]->View();
cout << endl;
}
for(int i = 0; i < MAX; i++)
delete p_dma[i];
cout << "Done\n";
return 0;
}
运行结果如下:
4. Benevolent Order of Programmers 用来维护瓶装葡萄酒箱。为描述它,BOP Portmaster设置了一个Port类,其声明如下:
#include <iostream>
using namespace std;
class Port
{
private:
char * brand;
char style[20]; // i.e., tawny, ruby, vintage
int bottles;
public:
Port(const char * br = "none", const char * st = "none", int b = 0);
Port(const Port & p); // copy constructor
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 availiable
int BottleCount() const { return bottles; }
virtual void Show() const;
friend ostream & operator<<(ostream & os, const Port & p);
};
show()方法按下面的格式显式信息:
Brand: Gallo
Kind: tawny
Bottles: 20
operator<<()函数按照下面的格式显式信息(末尾没有换行符)
Gallo, tawny, 20
PortMaster完成了Port类方法的定义后,派生了VintagePort类,然后被解职——因为不小心将一瓶45度Cockburn泼到了正在准备烤肉调料的人身上,VintagePort类如下所示:
class VintagePort : public Port // style necessarily = "vintage"
{
private:
char * nickname; // i.e.,"The Noble" or "Old Velet", etc.
int year; // vintage 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中各个方法的定义。
本题a和d的答案在代码文件中;
b. 重新定义的代码是Show()代码,因为在两个类中显示的不一样,还有输出运算符,输出在两个类中也不相同,没有重新定义的是操作bottles的方法,这对两个类来说是一样的,因此不需要重新定义;
c.operator<<()函数不是Port类的成员函数,虚函数必须是成员函数,而operator=()函数两个类的参数不一样,声明虚函数也起不到相应的效果。
样例代码如下:
// vintageport.h -- definition for Port class
#ifndef VINTAGEPORT_H_
#define VINTAGEPORT_H_
#include <iostream>
using namespace std;
class Port
{
private:
char * brand;
char style[20]; // i.e., tawny, ruby, vintage
int bottles;
public:
Port(const char * br = "none", const char * st = "none", int b = 0);
Port(const Port & p); // copy constructor
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 availiable
int BottleCount() const { return bottles; }
virtual void Show() const;
friend ostream & operator<<(ostream & os, const Port & p);
};
class VintagePort : public Port // style necessarily = "vintage"
{
private:
char * nickname; // i.e.,"The Noble" or "Old Velet", etc.
int year; // vintage 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);
virtual void Show() const;
friend ostream & operator<<(ostream & os, const VintagePort & vp);
};
#endif
// vintageport.cpp -- methods for VintagePort class
#include <cstring>
#include "vintageport.h"
// methods for Port class
Port::Port(const char * br, const char * st, int b)
{
brand = new char[strlen(br) + 1];
strcpy(brand, br);
strcpy(style, st);
bottles = b;
}
Port::Port(const Port & p)
{
brand = new char[strlen(p.brand) + 1];
strcpy(brand, p.brand);
strcpy(style, p.style);
bottles = p.bottles;
}
Port & Port::operator=(const Port & p)
{
if (this == &p)
return *this;
else
{
delete [] brand;
brand = new char[strlen(p.brand) + 1];
strcpy(brand, p.brand);
strcpy(style, p.style);
bottles = p.bottles;
return *this;
}
}
Port & Port::operator+=(int b)
{
bottles += b;
return *this;
}
Port & Port::operator-=(int b)
{
if (b <= bottles)
bottles -= b;
else
cout << b << " > " << bottles << endl;
return *this;
}
void Port::Show() const
{
cout << "Brand: " << brand << endl;
cout << "Kind: " << style << endl;
cout << "Bottles: " << bottles << endl;
}
ostream & operator<<(ostream & os, const Port & p)
{
os << p.brand << ", " << p.style << ", " << p.bottles;
return os;
}
// methods for VintagePort class
VintagePort::VintagePort() : Port("none","vintage")
{
nickname = new char[5];
strcpy(nickname, "none");
year = 0;
}
VintagePort::VintagePort(const char * br, int b, const char * nn, int y) : Port(br, "vintage", b)
{
nickname = new char[strlen(nn) + 1];
strcpy(nickname, nn);
year = y;
}
VintagePort::VintagePort(const VintagePort & vp) : Port(vp)
{
nickname = new char[strlen(vp.nickname) + 1];
strcpy(nickname, vp.nickname);
year = vp.year;
}
VintagePort & VintagePort::operator=(const VintagePort & vp)
{
if (this == &vp)
return *this;
else
{
Port::operator=(vp);
delete [] nickname;
nickname = new char[strlen(vp.nickname) + 1];
strcpy(nickname, vp.nickname);
year = vp.year;
return *this;
}
}
void VintagePort::Show() const
{
Port::Show();
cout << "Nick Name: " << nickname << endl;
cout << "Year: " << year << endl;
}
ostream & operator<<(ostream & os, const VintagePort & vp)
{
os << (const Port &)vp << ", " << vp.nickname << ", " << vp.year;
return os;
}
// usevintageport.cpp -- test VintagePort class
// compile with vintageport.cpp
#include "vintageport.h"
int main()
{
Port gal("Gallo", "tawny", 20);
Port * pp;
VintagePort vp("Chateau Margaux", 10, "The Noble",1999);
VintagePort * pvp;
pp = &vp;
pvp = &vp;
cout << "Output with Show():\n";
pp->Show();
pp = &gal;
pp->Show();
cout << "Take out 30 bottles:\n";
*pp -= 30;
pp->Show();
cout << "After add 10 bottles:\n";
*pp += 10;
pp->Show();
pp = &vp;
cout << "Output with operator<< :\n";
cout << "Object: " << vp << endl;
cout << "Pointer Port: " << *pp << endl;
cout << "Pointer VintagePort: " << *pvp << endl;
return 0;
}
运行结果如下:
Ps:从运行结果可以看出,友元运算符<<函数不具有虚拟函数的功能,不能根据对象输出相应内容