1,以下面的类声明为基础:
// base class
class Cd{ // represents a CD diskprivate:
char performers[50] ;
char label[20];
int selections;// number of selectionsdouble playtime; // playing time in minutes
public:
Cd(char * sl,char * s2,int n,double x!;Cd(const cd & d!;
Cd{};
-Cd(};
void Report() const; // reports all CD dataCd & operator=(const cd & d);
派生出一个Classic 类,并添加一组 har 成员,用于存储指出CD 中主要作品的字符串。修改上述声明,使基类的所有函数都是虚的。如果上述定义声明的某个方法并不需要,则请删除它。使用下面的程序测试您的产品:
#include
using namespace std;
#include "classic.h"// which will contain #include cd.hvoid Bravo(const Cd & disk);int main()
Cd cl("Beatles","Capitol",14,35,5);Classic c2 = Classic("piano Sonata in B flat,Fantasia in C""Alfred Brendel","Philips",2,57.17);
Cd *pcd = &cl;
cout// use Cd method
c2.Report();
//use Classic method
cout << "Using type cd * pointer to objects:\n";pcd->Report();// use cd method for cd objectpcd = &c2;
pcd->Report(); // use Classic method for classic object
cout << "Calling a function with a Cd reference argument:\n";Bravo(c1);Bravo(c2];
cout cc "Testing assignment: ";Classic copyi
copy= c2;
copy.Report()
return 0;
void Bravo(const Cd & disk)
disk.Report();
- #include
- 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:\n";
- Classic copy;
- copy = c2;
- copy.Report();
-
- return 0;
- }
-
- void Bravo(const Cd &disk)
- {
- disk.Report();
- }
- #ifndef CD_H_
- #define CD_H_
-
- class Cd
- {
- private:
- char performers[50];
- char labels[20];
- int selections;
- double playtime;
-
- public:
- Cd(const char *s1, const char *s2, int n, double x);
- Cd(const Cd &d);
- Cd();
- virtual ~Cd();
- virtual void Report() const;
- Cd &operator=(const Cd &d);
- };
-
- #endif
- #include
- #include "cd.h"
- #include
-
- Cd::Cd(const char *s1, const char *s2, int n, double x) //用户定义构造函数;
- {
- std::strncpy(performers, s1, 50);
- performers[49] = '\0';
- std::strncpy(labels, s2, 20);
- labels[19] = '\0';
- selections = n;
- playtime = x;
- }
-
- Cd::Cd(const Cd &d) //用户定义构造函数;
- {
- std::strncpy(performers, d.performers, 50);
- performers[49] = '\0';
- std::strncpy(labels, d.labels, 50);
- labels[49] = '\0';
- selections = d.selections;
- playtime = d.playtime;
- }
-
- Cd::Cd() //用户定义默认构造函数;
- {
- performers[0] = '\0';
- labels[0] = '\0';
- selections = 0;
- playtime = 0.0;
- }
-
- Cd::~Cd()
- {
- }
-
- void Cd::Report() const
- {
- std::cout << "Performers: " << performers << std::endl;
- std::cout << "Label: " << labels << std::endl;
- std::cout << "Selections: " << selections << std::endl;
- std::cout << "Playtime: " << playtime << std::endl;
- }
-
- Cd &Cd::operator=(const Cd &d)
- {
- if (this == &d)
- {
- return *this;
- }
- std::strncpy(performers, d.performers, 50);
- performers[49] = '\0';
- std::strncpy(labels, d.labels, 20);
- labels[19] = '\0';
- selections = d.selections;
- playtime = d.playtime;
- return *this;
- }
- #ifndef CLASSIC_H_
- #define CLASSIC_H_
- #include "cd.h"
-
- class Classic : public Cd
- {
- private:
- char cdstr[50];
-
- public:
- Classic() : Cd() { cdstr[0] = '\0'; }
- Classic(const char *s, const char *s1, const char *s2, int n, double x);
- Classic(const char *s, const Cd &d);
- ~Classic();
- virtual void Report() const;
- Classic &operator=(const Classic &cs);
- };
-
- #endif
- #include
- #include
- #include "classic.h"
-
- Classic::Classic(const char *s, const char *s1, const char *s2, int n, double x) : Cd(s1, s2, n, x)
- {
- std::strncpy(cdstr, s, 50);
- cdstr[49] = '\0';
- }
-
- Classic::Classic(const char *s, const Cd &d) : Cd(d)
- {
- std::strncpy(cdstr, s, 50);
- cdstr[49] = '\0';
- }
-
- Classic::~Classic()
- {
- }
-
- void Classic::Report() const
- {
- Cd::Report();
- std::cout << "Major article in the CD is: " << cdstr << std::endl;
- std::cout.put('\n');
- }
-
- Classic &Classic::operator=(const Classic &cs)
- {
- if (this == &cs)
- {
- return *this;
- }
- Cd::operator=(cs);
- std::strncpy(cdstr, cs.cdstr, 50);
- cdstr[49] = '\0';
- return *this;
- }
2.完成练习 1,但让两个类使用动态内存分配而不是长度固定的数组来记录字符串。
- #include
- 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:\n";
- Classic copy;
- copy = c2;
- copy.Report();
-
- return 0;
- }
-
- void Bravo(const Cd &disk)
- {
- disk.Report();
- }
- #ifndef CD_H_
- #define CD_H_
-
- class Cd //原有的数组修改为指针便于动态分配内存;
- {
- private:
- char *performers;
- char *labels;
- int selections;
- double playtime;
-
- public:
- Cd(const char *s1, const char *s2, int n, double x);
- Cd(const Cd &d);
- Cd();
- virtual ~Cd(); //虚析构函数;
- virtual void Report() const;
- Cd &operator=(const Cd &d);
- };
-
- #endif
- #include
- #include "cd.h"
- #include
-
- Cd::Cd(const char *s1, const char *s2, int n, double x) //用户定义构造函数;
- {
- performers = new char[std::strlen(s1) + 1];
- std::strcpy(performers, s1);
- labels = new char[std::strlen(s2) + 1];
- std::strcpy(labels, s2);
- selections = n;
- playtime = x;
- }
-
- Cd::Cd(const Cd &d) //用户定义构造函数;
- {
- performers = new char[std::strlen(d.performers) + 1];
- std::strcpy(performers, d.performers);
- labels = new char[std::strlen(d.labels) + 1];
- std::strcpy(labels, d.labels);
- selections = d.selections;
- playtime = d.playtime;
- }
-
- Cd::Cd() //用户定义默认构造函数;
- {
- performers = new char[1];
- performers[0] = '\0';
- labels = new char[1];
- labels[0] = '\0';
- selections = 0;
- playtime = 0.0;
- }
-
- Cd::~Cd()
- {
- delete[] performers;
- delete[] labels;
- }
-
- void Cd::Report() const
- {
- std::cout << "Performers: " << performers << std::endl;
- std::cout << "Label: " << labels << std::endl;
- std::cout << "Selections: " << selections << std::endl;
- std::cout << "Playtime: " << playtime << std::endl;
- }
-
- Cd &Cd::operator=(const Cd &d) //重载赋值运算符;
- {
- if (this == &d)
- {
- return *this;
- }
- delete[] performers; //释放原有已分配的内存空间;
- delete[] labels; //同上;
- performers = new char[std::strlen(d.performers) + 1];
- std::strcpy(performers, d.performers);
- labels = new char[std::strlen(d.labels) + 1];
- std::strcpy(labels, d.labels);
- selections = d.selections;
- playtime = d.playtime;
- return *this;
- }
- #ifndef CLASSIC_H_
- #define CLASSIC_H_
- #include "cd.h"
-
- class Classic : public Cd //公有继承派生类;
- {
- private:
- char *cdstr;
-
- public:
- Classic();
- Classic(const char *s1, const char *s2, const char *s3, int n, double x);
- Classic(const char *s, const Cd &d);
- ~Classic();
- virtual void Report() const;
- Classic &operator=(const Classic &cs);
- };
-
- #endif
- #include
- #include
- #include "classic.h"
-
- Classic::Classic() : Cd()
- {
- cdstr = new char[1];
- cdstr[0] = '\0';
- }
-
- Classic::Classic(const char *s, const char *s1, const char *s2, int n, double x) : Cd(s1, s2, n, x) //基类构造函数的成员列表初始化;
- {
- cdstr = new char[std::strlen(s) + 1];
- std::strcpy(cdstr, s);
- }
-
- Classic::Classic(const char *s, const Cd &d) : Cd(d) //基类构造函数的成员列表初始化;
- {
- cdstr = new char[std::strlen(s) + 1];
- std::strcpy(cdstr, s);
- }
-
- Classic::~Classic()
- {
- delete[] cdstr;
- }
-
- void Classic::Report() const
- {
- Cd::Report();
- std::cout << "Major article in the CD is: " << cdstr << std::endl;
- std::cout.put('\n');
- }
-
- Classic &Classic::operator=(const Classic &cs)
- {
- if (this == &cs)
- {
- return *this;
- }
- delete[] cdstr;
- Cd::operator=(cs);
- cdstr = new char[std::strlen(cs.cdstr) + 1];
- std::strcpy(cdstr, cs.cdstr);
- return *this;
- }
3.修改baseDMA-lacksDMA-hasDMA类层次,让三个类都从一个ABC派生而来,然后使用与程序清单13.10 相似的程序对结果进行测试。也就是说,它应使用ABC 指针数组并让用户决定要创建的对象类型。在类定义中添加virtual View()方法以处理数据显示。
- #include
- #include "dma.h"
- const int LEN = 3;
-
- int main()
- {
- using std::cin;
- using std::cout;
- using std::endl;
-
- DMA *temp[LEN];
- char label[50];
- int rating;
- char color[40];
- char style[50];
- char kind;
-
- cout << "Here are the process for creating 3 objects" << endl; //参考书上程序清单13.10让用户进行选择;
- for (int i = 0; i < LEN; i++)
- {
- cout << "Enter 1 for baseDMA, 2 for lacksDMA or 3 for hasDMA: ";
- while (cin >> kind && (kind != '1' && kind != '2' && kind != '3'))
- {
- cin.clear();
- while (cin.get() != '\n')
- continue;
- cout << "Please enter 1, 2 or 3: ";
- }
- if (kind == '1')
- {
- cout << "Please enter the label: ";
- cin >> label; //推荐输入书中样例Portabelly;
- cout << "Please enter the rating: ";
- cin >> rating; //推荐输入书中样例8;
- temp[i] = new baseDMA(label, rating, "baseDMA");
- }
- else if (kind == '2')
- {
- cout << "Please enter the color: ";
- cin >> color; //推荐输入书中样例red;
- temp[i] = new lacksDMA(color, "lacksDMA");
- }
- else
- {
- cout << "Please enter the style: ";
- cin >> style; //推荐输入书中样例Mercator;
- temp[i] = new hasDMA(style, "hasDMA");
- }
- }
- cout << "\nThe results after creating 3 objects" << endl;
- for (int i = 0; i < LEN; i++)
- {
- temp[i]->View();
- }
- for (int i = 0; i < LEN; i++)
- {
- delete temp[i];
- }
- cout << "Done.\n";
-
- return 0;
- }
- #ifndef DMA_H_
- #define DMA_H_
- #include
-
- class DMA
- {
- private:
- char *classname; //基类成员设为一个char指针方便分配内存空间保存派生类的类名;
-
- protected:
- const char *show_classname() const { return classname; } //抽象基类DMA的View方法不进行定义因此使用protected让派生类可以访问基类数据;
-
- public:
- DMA(const char *cn = "null"); //声明基类DMA的默认构造函数;
- DMA(const DMA &rs); //声明基类DMA的复制构造函数;
- DMA &operator=(const DMA &rs); //声明为基类DMA重载赋值运算符;
- virtual ~DMA(); //声明基类DMA的虚析构函数;
- virtual void View() const = 0; //声明基类DMA的纯虚函数;
- };
-
- class baseDMA : public DMA
- {
- private:
- char *label;
- int rating;
-
- public:
- baseDMA(const char *l = "null", int r = 0, const char *cn = "null"); //声明派生类baseDMA的默认构造函数;
- baseDMA(const char *l, int r, const DMA &rs); //声明派生类baseDMA的用户定义构造函数;
- baseDMA(const baseDMA &rs); //声明派生类baseDMA的复制构造函数;
- ~baseDMA(); //声明派生类baseDMA的析构函数;
- baseDMA &operator=(const baseDMA &rs); //声明为派生类baseDMA重载赋值运算符;
- virtual void View() const; //声明为派生类baseDMA定义基类虚函数;
- };
-
- class lacksDMA : public DMA
- {
- private:
- enum { COL_LEN = 40 };
- char color[COL_LEN];
-
- public:
- lacksDMA(const char *c = "blank", const char *cn = "null"); //声明派生类lacksDMA的默认构造函数;
- lacksDMA(const char *c, const DMA &rs); //声明派生类lacksDMA的用户定义构造函数;
- lacksDMA(const lacksDMA &rs); //声明派生类lacksDMA的复制构造函数;
- ~lacksDMA(); //声明派生类lacksDMA的析构函数;
- lacksDMA &operator=(const lacksDMA &rs); //声明为派生类lacksDMA重载赋值运算符;
- virtual void View() const; //声明为派生类lacksDMA定义基类虚函数;
- };
-
- class hasDMA : public DMA
- {
- private:
- char *style;
-
- public:
- hasDMA(const char *s = "none", const char *cn = "null"); //声明派生类hasDMA的默认构造函数;
- hasDMA(const char *s, const DMA &rs); //声明派生类hasDMA的用户定义构造函数;
- hasDMA(const hasDMA &rs); //声明派生类hasDMA的复制构造函数;
- ~hasDMA(); //声明派生类hasDMA的析构函数;
- hasDMA &operator=(const hasDMA &rs); //声明为派生类hasDMA重载赋值运算符;
- virtual void View() const; //声明为派生类hasDMA定义基类虚函数;
- };
-
- #endif
- #include "dma.h"
- #include
-
- DMA::DMA(const char *cn) //定义基类DMA的默认构造函数;
- {
- classname = new char[std::strlen(cn) + 1];
- std::strcpy(classname, cn);
- }
-
- DMA::DMA(const DMA &rs) //定义基类DMA的复制构造函数;
- {
- classname = new char[std::strlen(rs.classname) + 1];
- std::strcpy(classname, rs.classname);
- }
-
- DMA &DMA::operator=(const DMA &rs) //为基类DMA重载赋值运算符;
- {
- if (this == &rs)
- {
- return *this;
- }
- delete[] classname; //先释放后分配;
- classname = new char[std::strlen(rs.classname) + 1];
- std::strcpy(classname, rs.classname);
- return *this;
- }
-
- DMA::~DMA() //定义基类DMA的析构函数;
- {
- delete[] classname;
- }
-
- baseDMA::baseDMA(const char *l, int r, const char *cn) : DMA(cn) //定义派生类baseDMA的默认构造函数;
- {
- label = new char[std::strlen(l) + 1];
- std::strcpy(label, l);
- rating = r;
- }
-
- baseDMA::baseDMA(const char *l, int r, const DMA &rs) : DMA(rs) //定义派生类baseDMA的用户定义构造函数;
- {
- label = new char[std::strlen(l) + 1];
- std::strcpy(label, l);
- rating = r;
- }
-
- baseDMA::baseDMA(const baseDMA &rs) : DMA(rs) //定义派生类baseDMA的复制构造函数;
- {
- label = new char[std::strlen(rs.label) + 1];
- std::strcpy(label, rs.label);
- rating = rs.rating;
- }
-
- baseDMA::~baseDMA() //定义派生类baseDMA的析构函数;
- {
- delete[] label;
- }
-
- baseDMA &baseDMA::operator=(const baseDMA &rs) //为派生类baseDMA重载赋值运算符;
- {
- if (this == &rs)
- {
- return *this;
- }
- delete[] label; //先释放后分配;
- DMA::operator=(rs); //调用基类DMA的赋值运算符方法初始化派生类baseDMA的基类数据成员;
- label = new char[std::strlen(rs.label) + 1];
- std::strcpy(label, rs.label);
- rating = rs.rating;
- return *this;
- }
-
- void baseDMA::View() const //为派生类baseDMA定义基类虚函数;
- {
- std::cout << "Classname: " << show_classname() << std::endl; //调用基类protected中的show_classname方法显示基类数据成员;
- std::cout << "Label: " << label << std::endl;
- std::cout << "Rating: " << rating << std::endl;
- }
-
- lacksDMA::lacksDMA(const char *c, const char *cn) : DMA(cn) //定义派生类lacksDMA的默认构造函数;
- {
- std::strncpy(color, c, COL_LEN - 1);
- color[COL_LEN - 1] = '\0';
- }
-
- lacksDMA::lacksDMA(const char *c, const DMA &rs) : DMA(rs) //定义派生类lacksDMA的用户定义构造函数;
- {
- std::strncpy(color, c, COL_LEN - 1);
- color[COL_LEN - 1] = '\0';
- }
-
- lacksDMA::lacksDMA(const lacksDMA &rs) : DMA(rs) //定义派生类lacksDMA的复制构造函数;
- {
- std::strncpy(color, rs.color, COL_LEN - 1);
- color[COL_LEN - 1] = '\0';
- }
-
- lacksDMA::~lacksDMA()
- {
- }
-
- lacksDMA &lacksDMA::operator=(const lacksDMA &rs) //为派生类lacksDMA重载赋值运算符;
- {
- if (this == &rs)
- {
- return *this;
- }
- DMA::operator=(rs); //调用基类DMA的赋值运算符方法初始化派生类lacksDMA的基类数据成员;
- std::strncpy(color, rs.color, COL_LEN - 1);
- color[COL_LEN - 1] = '\0';
- return *this;
- }
-
- void lacksDMA::View() const //为派生类lacksDMA定义基类虚函数;
- {
- std::cout << "Classname: " << show_classname() << std::endl; //调用基类protected中的show_classname方法显示基类数据成员;
- std::cout << "Color: " << color << std::endl;
- }
-
- hasDMA::hasDMA(const char *s, const char *cn) : DMA(cn) //定义派生类hasDMA的默认构造函数;
- {
- style = new char[std::strlen(s) + 1];
- std::strcpy(style, s);
- }
-
- hasDMA::hasDMA(const char *s, const DMA &rs) : DMA(rs) //定义派生类hasDMA的用户定义构造函数;
- {
- style = new char[std::strlen(s) + 1];
- std::strcpy(style, s);
- }
-
- hasDMA::hasDMA(const hasDMA &rs) : DMA(rs) //定义派生类hasDMA的复制构造函数;
- {
- style = new char[std::strlen(rs.style) + 1];
- std::strcpy(style, rs.style);
- }
-
- hasDMA::~hasDMA() //定义派生类hasDMA的析构函数;
- {
- delete[] style;
- }
-
- hasDMA &hasDMA::operator=(const hasDMA &rs) //为派生类hasDMA重载赋值运算符;
- {
- if (this == &rs)
- {
- return *this;
- }
- delete[] style; //先释放后分配;
- DMA::operator=(rs); //调用基类DMA的赋值运算符方法初始化派生类hasDMA的基类数据成员;
- style = new char[std::strlen(rs.style) + 1];
- std::strcpy(style, rs.style);
- return *this;
- }
-
- void hasDMA::View() const //为派生类hasDMA定义基类虚函数;
- {
- std::cout << "Classname: " << show_classname() << std::endl; //调用基类protected中的show_classname方法显示基类数据成员;
- std::cout << "Style: " << style << std::endl;
- }
4.BenevolentOrderof Programmers用来维护瓶装葡萄酒箱。为描述它BPPortmaster设置了一个Port类,其声明如下:
#include'
using- namespace std;
class Port
private:
char *brand;-charkstyle(201//ieawnyrubyintagesiintbottles;-public.飞+A
Port(const char *br"none",~const char:*st "none, int b 0)
Port(const Port 5 p);
copy constructor
virtual.-Port(- delete ll brand;
Port &~operator=(const.Port 5 p):.Port & operator+=(int b) ,
Port 5 operator-=(int bj;available
// addsb-tobottles// subtracts b from bottles, if
--
int'BottleCount() const-f return bottles;virtual void showl const:
friend ostream & operatores(ostream & os,const Port & p);
show()方法按下面的格式显示信息
Brand:GalFo
Kind: tawnyBottles:20
operator<<()函数按下面的格式显示信息(末尾没有换行符):Gal1o,tawnyr20-
PortMaster 完成了-Port类的方法定义后派生了VintagePort类然后被解职-因为不小心将一瓶45度Cockbum泼到了正在准备烤肉调料的人身上VintagePort类如下所示:
class VintagePort :publicPort// style-necessarily=vintage
-ai
private:
ehar.*.nickname;
int year!
public:
VintagePortl:
// 1.e,r ."TheNoble".or "oldvelvet",etc ..vintageyear
VintagePort(const char* ,br;int b,const char *inn,int y);;+yintagePortqconstVintagePort-&vp)------_.-
VintagePort(E-delete l] nicknamerVintageport 5 operator=(const VintagePort vpF;void Show(yconst:....-..
friend ostream operator<<(ostream os,const Vintageport & vp);
您被指定负责完成 VintagePorta
a.第一个任务是重新创建Port 方法定义,因为前任被开除时销毁了方法定义
b.第二个任务是解释为什么有的方法重新定义了,而有些没有重新定义。
c.第三个任务是解释为何没有operator-)和operator<<()声明为虚的。
d.第四个任务是提供VintagePort 中各个方法的定义。
- #include "port.h"
- #include "vintageport.h"
-
- int main()
- {
- Port wine1("Gallo", "tawny", 20); //构造Port基类对象;
- VintagePort wine2("Lafei", 10, "strong wine", 1876); //构造VintagePort基类对象;
- VintagePort wine3("Merlot", 50, "middle wine", 1976); //构造VintagePort基类对象;
-
- cout << "Here is the Port object:\n";
- wine1.Show(); //调用基类Show方法;
- cout << wine1 << endl; //调用基类重载输出流运算符;
-
- cout << "\nHere are the VintagePort objects:\n";
- wine2.Show(); //调用派生类Show方法;
- cout << wine2 << endl; //调用派生类重载输出流运算符;
- wine3.Show(); //调用派生类Show方法;
- cout << wine3 << endl; //调用派生类重载输出流运算符;
-
- cout << "\nGallo add 20 bottles:\n";
- wine1 += 20; //调用基类重载+=运算符;
- wine1.Show();
-
- cout << "\nLafei add 10 bottles:\n";
- wine2 += 10; //调用派生类重载+=运算符;
- wine2.Show();
-
- cout << "\nMerlot minus 10 bottles:\n";
- wine3 -= 10; //调用派生类重载-=运算符;
- wine3.Show();
-
- VintagePort wine4(wine2); //调用派生类复制构造函数;
- cout << "\nResult of VintagePort copy:\n";
- wine4.Show();
-
- VintagePort wine5;
- wine5 = wine3; //调用派生类重载赋值运算符;
- cout << "\nResult of VintagePort assignment:\n";
- wine5.Show();
-
- return 0;
- }
- #ifndef PORT_H_
- #define PORT_H_
- #include
- 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);
- Port &operator-=(int b);
- int BottleCount() const { return bottles; }
- virtual void Show() const;
- friend ostream &operator<<(ostream &os, const Port &p);
- };
-
- #endif
- #include "port.h"
- #include
-
- Port::Port(const char *br, const char *st, int b)
- {
- brand = new char[std::strlen(br) + 1]; //new分配内存;
- std::strcpy(brand, br);
- std::strncpy(style, st, 20);
- style[19] = '\0'; //保证字符串是有效的;
- bottles = b;
- }
-
- Port::Port(const Port &p)
- {
- brand = new char[std::strlen(p.brand) + 1];
- std::strcpy(brand, p.brand);
- std::strncpy(style, p.style, 20);
- style[19] = '\0';
- bottles = p.bottles;
- }
-
- Port &Port::operator=(const Port &p)
- {
- if (this == &p)
- {
- return *this;
- }
- delete[] brand;
- brand = new char[std::strlen(p.brand) + 1];
- std::strcpy(brand, p.brand);
- std::strncpy(style, p.style, 20);
- style[19] = '\0';
- 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 << "Kind: " << style << endl;
- cout << "Bottles: " << bottles << endl;
- }
-
- ostream &operator<<(ostream &os, const Port &p)
- {
- os << p.brand << ", " << p.style << ", " << p.bottles;
- return os;
- }
- #ifndef VINTAGEPORT_H_
- #define VINTAGEPORT_H_
-
- 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);
- };
-
- #endif
- #include
- #include "port.h"
- #include "vintageport.h"
-
- VintagePort::VintagePort() : Port("none", "vintage", 0) //成员列表初始化派生类的基类对象数据;
- {
- nickname = new char[1];
- nickname[0] = '\0';
- year = 0;
- }
-
- VintagePort::VintagePort(const char *br, int b, const char *nn, int y) : Port(br, "vintage", b) //成员列表初始化派生类的基类对象数据;
- {
- nickname = new char[std::strlen(nn) + 1];
- std::strcpy(nickname, nn);
- year = y;
- }
-
- VintagePort::VintagePort(const VintagePort &vp) : Port(vp) //调用基类复制构造函数初始化派生类的基类对象数据;
- {
- nickname = new char[std::strlen(vp.nickname) + 1];
- std::strcpy(nickname, vp.nickname);
- year = vp.year;
- }
-
- VintagePort &VintagePort::operator=(const VintagePort &vp)
- {
- if (this == &vp)
- {
- return *this;
- }
- delete[] nickname;
- Port::operator=(vp); //调用基类赋值运算符修改派生类的基类数据成员;
- nickname = new char[std::strlen(vp.nickname) + 1];
- std::strcpy(nickname, vp.nickname);
- year = vp.year;
- return *this;
- }
-
- void VintagePort::Show() const
- {
- Port::Show();
- cout << "Nickname: " << nickname << endl;
- cout << "Year: " << year << endl;
- }
-
- ostream &operator<<(ostream &os, const VintagePort &vp)
- {
- os << (const Port &)vp; //强制类型转换调用Port基类的重载输出流运算符方法;
- os << ", " << vp.nickname << ", " << vp.year;
- return os;
- }