C++ Primer Plus 第六版(中文版)第十三章(完美修订版)编程练习答案(类继承)

//本章程序需分多文件运行,请读者注意;

//本博主所写的代码仅为阅读者提供参考;

//若有不足之处请提出,博主会尽所能修改;

//附上课后编程练习题目;

//若是对您有用的话请点赞或分享提供给它人;


//13.11 - 1(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:\n";
    Classic copy;
    copy = c2;
    copy.Report();

    return 0;
}

void Bravo(const Cd &disk)
{
    
    
    disk.Report();
    return;
}

//13.11 - 1(cd).h

#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

//13.11 - 1(cd).cpp

#include <iostream>
#include "cd.h"
#include <cstring>

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;
    return;
}

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;
}

//13.11 - 1(classic).h

#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

//13.11 - 1(classic).cpp

扫描二维码关注公众号,回复: 12354410 查看本文章
#include <iostream>
#include <cstring>
#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');
    return;
}

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;
}

//-----------------------------------------------------------------

//-----------------------------------------------------------------
//13.11 - 2(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:\n";
    Classic copy;
    copy = c2;
    copy.Report();

    return 0;
}

void Bravo(const Cd &disk)
{
    
    
    disk.Report();
    return;
}

//13.11 - 2(cd).h

#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

//13.11 - 2(cd).cpp

#include <iostream>
#include "cd.h"
#include <cstring>

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;
    return;
}

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;
}

//13.11 - 2(classic).h

#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

//13.11 - 2(classic).cpp

#include <iostream>
#include <cstring>
#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');
    return;
}

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;
}

//-----------------------------------------------------------------

//-----------------------------------------------------------------
//13.11 - 3(main).cpp

#include <iostream>
#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;
}

//13.11 - 3(dma).h

#ifndef DMA_H_
#define DMA_H_
#include <iostream>

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

//13.11 - 3(dma).cpp

#include "dma.h"
#include <cstring>

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;
    return;
}

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;
    return;
}

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;
    return;
}

//-----------------------------------------------------------------

//-----------------------------------------------------------------
//13.11 - 4(main).cpp

#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;
}

//13.11 - 4(port).h

#ifndef PORT_H_
#define PORT_H_
#include <iostream>
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

//13.11 - 4(port).cpp

#include "port.h"
#include <cstring>

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;
}

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;
    return;
}

ostream &operator<<(ostream &os, const Port &p)
{
    
    
    os << p.brand << ", " << p.style << ", " << p.bottles;
    return os;
}

//13.11 - 4(vintageport).h

#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

//13.11 - 4(vintageport).cpp

#include <cstring>
#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;
    return;
}

ostream &operator<<(ostream &os, const VintagePort &vp)
{
    
    
    os << (const Port &)vp; //强制类型转换调用Port基类的重载输出流运算符方法;
    os << ", " << vp.nickname << ", " << vp.year;
    return os;
}

//-----------------------------------------------------------------

//------------------------------------------2021年1月7日 ----------------------------------------------;

猜你喜欢

转载自blog.csdn.net/m0_46181359/article/details/112339229
今日推荐