1.以下面的类声明为基础:
class Cd
{
public:
Cd();
Cd(char* s1, char* s2, int n, double x);
Cd(const Cd& d);
~Cd();
void Report()const;
Cd& operator=(const Cd& d);
private:
char performers[50];
char label[20];
int selections;
double playtime;
};
派生出一个Classic类,并添加一组char成员,用于存储指出CD中主要作品的字符串。修改上述声明,使基类的所有函数都是虚的。如果上述定义声明的某个方法并不需要,则请删除它。使用下面的程序测试您的产品:
#include<iostream>
using namespace std;
#include"classic.h" //which will contain #include cd.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 classic object
cout << "Calling a function with a Cd reference argument:\n";
Bravo(c1);
Bravo(c2);
cout << "Testing assignment: ";
Classic copy;
copy = c2;
copy.Report();
system("pause");
return 0;
}
void Bravo(const Cd& disk)
{
disk.Report();
}
//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
class Cd
{
public:
Cd();
Cd(const char* s1, const char* s2, int n, double x);
virtual ~Cd() {};
virtual void Report() const;
//由于这里没有采用动态内存分配,所以复制构造函数和赋值运算符用默认的就行了
private:
char performers[50];
char label[20];
int selections;
double playtime;
};
class Classic:public Cd
{
public:
Classic();
Classic(const char* song, const char* s1, const char* s2, int n, double x);
void Report()const;
private:
char mainsong[20];
};
#endif // !CLASS_H_
//class.cpp
#include <iostream>
#include <cstring>
#include "class.h"
using namespace std;
Cd::Cd()
{
performers[0] = '\0';
label[0] = '\0';
selections = 1;
playtime = 0.0;
}
Cd::Cd(const char* s1, const char* s2, int n, double x)
{
strcpy_s(performers, strlen(s1) + 1, s1);
strcpy_s(label, strlen(s2) + 1, s2);
selections = n;
playtime = x;
}
void Cd::Report()const
{
cout << "Performers: " << performers << endl;
cout << "Label: " << label << endl;
cout << "Selections: " << selections << endl;
cout << "Playtime: " << playtime << endl;
}
Classic::Classic():Cd()
{
mainsong[0] = '\0';
}
Classic::Classic(const char* song, const char* s1, const char* s2, int n, double x):Cd(s1,s2,n,x)
{
strcpy_s(mainsong, strlen(song)+1, song);
}
void Classic::Report()const
{
Cd::Report();
std::cout << "Main song: " << mainsong << std::endl;
}
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/4/29
* 描述:
************************************************* */
#include<iostream>
#include"class.h"
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); //c2初始化
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 classic object
cout << "Calling a function with a Cd reference argument:\n";
Bravo(c1);
Bravo(c2);
cout << "Testing assignment: ";
Classic copy;
copy = c2;
copy.Report();
system("pause");
return 0;
}
void Bravo(const Cd& disk)
{
disk.Report();
}
2.完成练习1,但让两个类使用动态内存分配而不是长度固定的数组来记录字符串。
//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
class Cd
{
public:
Cd();
Cd(const char* s1, const char* s2, int n, double x);
Cd(const Cd& d);
virtual ~Cd() ;
virtual void Report() const;
virtual Cd& operator=(const Cd& d); //
private:
char *performers;
char *label;
int selections;
double playtime;
};
class Classic:public Cd
{
public:
Classic();
Classic(const char* song, const char* s1, const char* s2, int n, double x);
Classic(const Classic& d); //
~Classic();
void Report()const;
Classic& operator=(const Classic& d);
private:
char *mainsong;
};
#endif // !CLASS_H_
//class.cpp
#include <iostream>
#include <cstring>
#include "class.h"
using namespace std;
Cd::Cd()
{
performers = new char[1];
performers[0] = '\0';
label = new char[1];
label[0] = '\0';
selections = 1;
playtime = 0.0;
}
Cd::Cd(const char* s1, const char* s2, int n, double x)
{
performers = new char[strlen(s1) + 1];
strcpy_s(performers, strlen(s1) + 1, s1);
label = new char[strlen(s2) + 1];
strcpy_s(label, strlen(s2) + 1, s2);
selections = n;
playtime = x;
}
Cd::Cd(const Cd& d)
{
delete[] performers;
delete[] label;
performers = new char[strlen(d.performers) + 1];
strcpy_s(performers, strlen(d.performers) + 1, d.performers);
label = new char[strlen(d.label) + 1];
strcpy_s(label, strlen(d.label) + 1, d.label);
selections = d.selections;
playtime = d.playtime;
}
Cd::~Cd()
{
delete[] performers;
delete[] label;
}
void Cd::Report()const
{
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;
delete[] label;
performers = new char[strlen(d.performers) + 1];
strcpy_s(performers, strlen(d.performers) + 1, d.performers);
label = new char[strlen(d.label) + 1];
strcpy_s(label, strlen(d.label) + 1, d.label);
selections = d.selections;
playtime = d.playtime;
return *this;
}
}
Classic::Classic():Cd()
{
mainsong = new char[1];
mainsong[0] = '\0';
}
Classic::Classic(const char* song, const char* s1, const char* s2, int n, double x):Cd(s1,s2,n,x)
{
mainsong = new char[strlen(song)+1];
strcpy_s(mainsong, strlen(song)+1, song);
}
Classic::Classic(const Classic& d):Cd(d)
{
mainsong = new char[strlen(d.mainsong) + 1];
strcpy_s(mainsong, strlen(d.mainsong) + 1, d.mainsong);
}
void Classic::Report()const
{
Cd::Report();
std::cout << "Main song: " << mainsong << std::endl;
}
Classic& Classic::operator=(const Classic& d)
{
if (this == &d)
return *this;
else
{
delete[] mainsong;
Cd::operator=(d);
mainsong = new char[strlen(d.mainsong) + 1];
strcpy_s(mainsong, strlen(d.mainsong) + 1, d.mainsong);
return *this;
}
}
Classic::~Classic()
{
delete[] mainsong;
}
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/4/29
* 描述:
************************************************* */
#include<iostream>
#include"class.h"
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); //c2初始化
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 classic object
cout << "Calling a function with a Cd reference argument:\n";
Bravo(c1);
Bravo(c2);
cout << "Testing assignment: ";
Classic copy;
copy = c2;
copy.Report();
system("pause");
return 0;
}
void Bravo(const Cd& disk)
{
disk.Report();
}
3.修改baseDMA-lacksDMA-hasDMA类层次,让三个类都从一个ABC派生而来,然后使用与程序清单13.10相似的程序对结果进行测试。也就是说,它应使用ABC指针数组,并让用户决定要创建的对象类型。在类定义中添加virtual View( )方法以处理数据显示。
//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
class ABC
{
public:
ABC(const char* l = "null", int r = 0);
ABC(const ABC& d); //复制构造函数
virtual ~ABC();
virtual ABC& operator=(const ABC& d) ;
virtual void view()const;
/*friend std::ostream& operator<<(std::ostream& os, const ABC& d);*/
private:
char* label;
int rating;
};
class baseDMA:public ABC //无动态内存分配,可使用默认复制构造函数和赋值运算符
{
public:
baseDMA(const char* l = "null", int r = 0, int f = 0);
~baseDMA();
void view()const;
/*friend std::ostream& operator<<(std::ostream& os, const baseDMA& d);*/
private:
int flag; //增加一个标记变量,以示区别
};
class lacksDMA:public ABC //也没有动态内存分配,使用默认复制构造函数和赋值运算符
{
public:
lacksDMA(const char* co = "red",const char* l = "null", int r = 0);
~lacksDMA() {};
void view()const;
/*friend std::ostream& operator<<(std::ostream& os, const lacksDMA& d);*/
private:
enum { COL_LEN = 40 };
char color[40];
};
class hasDMA:public ABC //使用了动态内存分配了
{
public:
hasDMA(const char*s ="none",const char* l="null", int r = 0);
hasDMA(const hasDMA& d); //复制构造函数
~hasDMA();
hasDMA& operator=(const hasDMA& d);
void view()const;
/*friend std::ostream& operator<<(std::ostream& os, const hasDMA& d);*/
private:
char* style;
};
#endif // !CLASS_H_
//class.cpp
#include"class.h"
using namespace std;
ABC::ABC(const char* l, int r )
{
label = new char[strlen(l) + 1];
strcpy_s(label, strlen(l) + 1, l);
rating = r;
}
ABC::ABC(const ABC& d)
{
delete[] label;
label = new char[strlen(d.label) + 1];
strcpy_s(label, strlen(d.label) + 1, d.label);
rating = d.rating;
}
ABC::~ABC()
{
delete[] label;
}
ABC& ABC::operator=(const ABC& d)
{
if (this == &d)
return *this;
else
{
delete[] label;
label = new char[strlen(d.label) + 1];
strcpy_s(label, strlen(d.label) + 1, d.label);
}
}
void ABC::view() const
{
cout << "Label: " << label << endl;
cout << "Rating: " << rating << endl;
}
//std::ostream& operator<<(std::ostream& os, const ABC& d)
//{
// os << "Label: " << d.label << endl;
// os << "Rating: " << d.rating << endl;
// return os;
//}
baseDMA::baseDMA(const char* l, int r, int f):ABC(l,r)
{
flag = f;
}
baseDMA::~baseDMA()
{
}
void baseDMA::view() const
{
ABC::view();
cout << "Flag: " << flag << endl;
}
//std::ostream& operator<<(std::ostream& os, const baseDMA& d)
//{
// os << (const ABC&)d; //友元函数就必须得用强制类型转换才能访问基类成员了
// os << "Flag: " << d.flag << endl;
// return os;
//}
//std::ostream& operator<<(std::ostream& os, const lacksDMA& d)
//{
// os << (const ABC&)d;
// os << "Color: " << d.color<< endl;
// return os;
//}
//
//std::ostream& operator<<(std::ostream& os, const hasDMA& d)
//{
// os << (const ABC&)d;
// os << "Style: " << d.style << endl;
// return os;
//}
lacksDMA::lacksDMA(const char* co, const char* l, int r):ABC(l,r)
{
strcpy_s(color, strlen(co) + 1, co);
}
void lacksDMA::view() const
{
ABC::view();
cout << "Color: " << color << endl;
}
hasDMA::hasDMA( const char* s, const char* l, int r):ABC(l,r)
{
style = new char[strlen(s) + 1];
strcpy_s(style, strlen(s) + 1, s);
}
hasDMA::hasDMA(const hasDMA& d):ABC(d)
{
delete[] style;
style = new char[strlen(d.style) + 1];
strcpy_s(style, strlen(d.style) + 1, d.style);
}
hasDMA::~hasDMA()
{
delete[] style;
}
hasDMA& hasDMA::operator=(const hasDMA& d)
{
if (this == &d)
return *this;
else
{
delete[] style;
style = new char[strlen(d.style) + 1];
strcpy_s(style, strlen(d.style) + 1, d.style);
}
}
void hasDMA::view() const
{
ABC::view();
cout << "Style: " << style << endl;
}
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/4/29
* 描述:
************************************************* */
#include<iostream>
#include<string>
#include"class.h"
using namespace std;
int main()
{
int LEN = 3;
ABC* pa[3];
string chtemp;
string ltemp;
int ftemp;
int rtemp;
char kind;
for (int i = 0;i < LEN;i++)
{
cout << "Enter label: " << endl;
getline(cin, ltemp);
cout << "Enter rating: " << endl;
cin >> rtemp;
cin.clear();
cout << "Enter 1 for baseDMA or 2 for lacksDMA "
<< "or LEN for hasDMA.\n";
while (cin >> kind && (kind != '1' && kind != '2' && kind != 'LEN'))
cout << "Enter 1 or 2 or LEN.\n";
if (kind == '1')
{
cout << "Enter flag: " << endl;
cin >> ftemp;
pa[i] = new baseDMA(ltemp.c_str(), rtemp,ftemp);
}
else if (kind == '2')
{
cout << "Enter color: " << endl;
getline(cin, chtemp);
pa[i] = new lacksDMA(chtemp.c_str(), ltemp.c_str(), rtemp);
}
else
{
cout << "Enter style: " << endl;
getline(cin, chtemp);
pa[i] = new hasDMA(chtemp.c_str(), ltemp.c_str(), rtemp);
}
while (cin.get() != '\n')
continue;
}
cout << endl;
for (int i = 0;i < LEN;i++)
{
pa[i]->view();
delete[] pa[i];
}
return 0;
}
4. Benevolent Order of Programmers 用 来 维 护 瓶 装 葡 萄 酒 箱。 为 描 述 它, BOPPortmaster设置了一个Port类,其声明如下:
class Port
{
public:
Port(const char* br = "none", const char* st = "none", int b = 0);
Port(const Port& p);
virtual ~Port();
Port& operator=(const Port& p);
Port& operator+=(int b);
Port& operator-=(int b);
int BottleCount()const { return bottles; }
virtual void Show()const;
friend ostream& operator<<(ostream& os, const Port& p);
private:
char* brand;
char style[20];
int bottles;
};
show( )方法按下面的格式显示信息:
Brand:Gallo
kind:tawny
Bottles:20
operator<<( )函数按下面的格式显示信息(末尾没有换行符):
Gallo,tawny,20
PortMaster完成了Port类的方法定义后派生了VintagePort类,然后被解职——因为不小心将一瓶45度Cockburn泼到了正在准备烤肉调料的人身上,VintagePort类如下所示:
class VintagePort:public Port
{
public:
VintagePort();
VintagePort(const char* br, int b, const char* nn, int y);
~VintagePort() { delete[]nickname; }
VintagePort& operator=(const VintagePort& vp);
void Show()const;
friend ostream& operator<<(ostream& os, const VintagePort& vp);
private:
char* nickname;
int year;
};
您被指定负责完成VintagePort。
a.第一个任务是重新创建Port方法定义,因为前任被开除时销毁了方法定义。
b.第二个任务是解释为什么有的方法重新定义了,而有些没有重新定义。
c.第三个任务是解释为何没有将operator=( )和operator<<( )声明为虚的。
d.第四个任务是提供VintagePort中各个方法的定义。
a和d:方法定义如下
//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
class Port
{
public:
Port(const char* br = "none", const char* st = "none", int b = 0);
Port(const Port& p);
virtual ~Port();
Port& operator=(const Port& p); //
Port& operator+=(int b); //
Port& operator-=(int b); //这两个方法没有被重新定义是因为子类可以直接用,不涉及动态内存分配
int BottleCount()const { return bottles; } //同上
virtual void Show()const;
friend std::ostream& operator<<(std::ostream& os, const Port& p);
private:
char* brand;
char style[20];
int bottles;
};
class VintagePort:public Port
{
public:
VintagePort();
VintagePort(const char* br, int b, const char* nn, int y);
~VintagePort() { delete[]nickname; }
VintagePort& operator=(const VintagePort& vp);
void Show()const;
friend std::ostream& operator<<(std::ostream& os, const VintagePort& vp);
private:
char* nickname;
int year;
};
#endif // !CLASS_H_
//class.cpp
#include"class.h"
using namespace std;
Port::Port(const char* br, const char* st, int b)
{
brand = new char[strlen(br) + 1];
strcpy_s(brand, strlen(br) + 1, br);
strcpy_s(style, strlen(st) + 1, st);
bottles = b;
}
Port::Port(const Port& p)
{
brand = new char[strlen(p.brand) + 1];
strcpy_s(brand, strlen(p.brand) + 1, p.brand);
strcpy_s(style, strlen(p.style) + 1, p.style);
bottles = p.bottles;
}
Port::~Port()
{
delete[] brand;
}
Port& Port::operator=(const Port& p)
{
if (this == &p)
return *this;
else
{
delete[] brand;
brand = new char[strlen(p.brand) + 1];
strcpy_s(brand, strlen(p.brand) + 1, p.brand);
strcpy_s(style, strlen(p.style) + 1, p.style);
bottles = p.bottles;
return *this;
}
}
Port& Port::operator+=(int b)
{
bottles += b;
return *this;
}
Port& Port::operator-=(int b)
{
bottles -= b;
return *this;
}
void Port::Show() const
{
cout << "Brand: " << brand << endl;
cout << "Style: " << style << endl;
cout << "Bottles: " << bottles << endl;
}
std::ostream& operator<<(std::ostream& os, const Port& p)
{
os << "Brand: " << p.brand << endl;
os << "Style: " << p.style << endl;
os << "Bottles: " << p.bottles << endl;
return os;
}
std::ostream& operator<<(std::ostream& os, const VintagePort& vp)
{
os << (const Port&)vp;
os << "Nickname: " << vp.nickname << endl;
os << "Year: " << vp.year << endl;
return os;
}
VintagePort::VintagePort():Port()
{
nickname = new char[1];
nickname[0] = '\0';
year = 0;
}
VintagePort::VintagePort(const char* br, int b, const char* nn, int y):Port(br,"none",b)
{
nickname = new char[strlen(nn) + 1];
strcpy_s(nickname, strlen(nn) + 1, nn);
year = y;
}
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_s(nickname, strlen(vp.nickname) + 1, vp.nickname);
year = vp.year;
}
}
void VintagePort::Show() const
{
Port::Show();
cout << "Nickname: " << nickname << endl;
cout << "Year: " << year << endl;
}
c.operator=( )可以不声明为虚函数,是因为在子类中中重新定义了operator=( )这个成员函数,两个operator=( )分别是Port::operator=( )和VintagePort::operator=( ),用限定符来区分它们的不同。用这个方法来保证多态。而且父类和子类都有使用动态内存分配,子类的operator=( )里面还需要调用父类的operator=( )。
而virtual的声明主要是为了根据引用或者指针类型所指向的具体的变量类型来调用对应的函数,这里并不需要这样做。
operator<<( )不声明为虚函数,因为这个函数是友元函数,无法被继承。