• C++Primer Plus习题记录-Chapter13


    13-1

    #pragma once
    #ifndef CD_H_
    #define CD_H_
    class Cd {
    private:
        char performers[50];
        char label[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);
    };
    class Classic :public Cd {
    private:
        char name[20];
    public:
        Classic(const char* s1, const char* s2, const char* s3, int n, double x);
        Classic(const Classic& d, char* c);
        Classic(const Classic& d);
        Classic();
        virtual void Report() const;
        Classic& operator=(const Classic& d);
    };
    #endif // !CD_H_
    #include "pch.h"
    #include <iostream>
    #include <cstring>
    #include "cd.h"
    using namespace std;
    Cd::Cd(const char* s1, const char* s2, int n, double x) {
        strcpy_s(performers, s1);
        strcpy_s(label, s2);
        selections = n;
        playtime = x;
    }
    Cd::Cd(const Cd& d) {
        strcpy_s(performers, d.performers);
        strcpy_s(label, d.label);
        selections = d.selections;
        playtime = d.playtime;
    }
    Cd::Cd() {
        performers[0] = '';
        label[0] = '';
        selections = 0;
        playtime = 0;
    }
    Cd::~Cd() {
    
    }
    void Cd::Report() const {
        cout << "performers: " << performers << endl;
        cout << "label: " << label << endl;
        cout << "number of selections: " << selections << endl;
        cout << "playing time in minutes: " << playtime << endl;
        cout << endl;
    }
    Cd& Cd::operator=(const Cd& d) {
        if (this == &d)
            return *this;
        strcpy_s(performers, d.performers);
        strcpy_s(label, d.label);
        selections = d.selections;
        playtime = d.playtime;
        return *this;
    }
    Classic::Classic(const char* s1, const char* s2, const char* s3, int n, double x) :Cd(s1, s2, n, x) {
        strcpy_s(name, s3);
    }
    Classic::Classic(const Classic& d, char* c) : Cd(d) {
        strcpy_s(name, c);
    }
    Classic::Classic() {
    
    }
    void Classic::Report() const {
        cout << "name: " << name << endl;
        Cd::Report();
    }
    Classic& Classic::operator=(const Classic& d) {
        if (this == &d)
            return *this;
        Cd::operator=(d);
        strcpy_s(name, d.name);
        return *this;
    }
    #include "pch.h"
    #include <iostream>
    #include <string>
    #include <cstring>
    #include "cd.h"
    using namespace std;
    void Bravo(const Cd& disk);
    int main(void) {
        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:
    ";
        c1.Report();
        c2.Report();
    
        cout << "Using type cd * pointer to objects:
    ";
        pcd->Report();
        pcd = &c2;
        pcd->Report();
    
        cout << "Calling a function with a Cd reference argument:
    ";
        Bravo(c1);
        Bravo(c2);
    
        cout << "Testing assignment: ";
        Classic copy;
        copy = c2;
        copy.Report();
        return 0;
    }
    void Bravo(const Cd& disk) {
        disk.Report();
    }

     13-2

    #pragma once
    #ifndef CD_H_
    #define CD_H_
    class Cd {
    private:
        char* performers;
        char* label;
        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);
    };
    class Classic :public Cd {
    private:
        char* name;
    public:
        Classic(const char* s1, const char* s2, const char* s3, int n, double x);
        Classic(const Classic& d, char* c);
        Classic(const Classic& d);
        Classic();
        ~Classic();
        virtual void Report() const;
        Classic& operator=(const Classic& d);
    };
    #endif // !CD_H_
    #include "pch.h"
    #include <iostream>
    #include <cstring>
    #include "cd.h"
    using namespace std;
    Cd::Cd(const char* s1, const char* s2, int n, double x) {
        int size1 = strlen(s1);
        int size2 = strlen(s2);
        performers = new char[size1 + 1];
        label = new char[size2 + 1];
        strcpy_s(performers, size1 + 1, s1);
        strcpy_s(label, size2 + 1, s2);
        selections = n;
        playtime = x;
    }
    Cd::Cd(const Cd& d) {
        int size1 = strlen(d.performers);
        int size2 = strlen(d.label);
        performers = new char[size1 + 1];
        label = new char[size2 + 1];
        strcpy_s(performers, size1 + 1, d.performers);
        strcpy_s(label, size2 + 1, d.label);
        selections = d.selections;
        playtime = d.playtime;
    }
    Cd::Cd() {
        performers = new char[1];
        performers[0] = '';
        label = new char[1];
        label[0] = '';
        selections = 0;
        playtime = 0;
    }
    Cd::~Cd() {
        delete[] performers;
        delete[] label;
    }
    void Cd::Report() const {
        cout << "performers: " << performers << endl;
        cout << "label: " << label << endl;
        cout << "number of selections: " << selections << endl;
        cout << "playing time in minutes: " << playtime << endl;
        cout << endl;
    }
    Cd& Cd::operator=(const Cd& d) {
        if (this == &d)
            return *this;
        delete[] label;
        delete[] performers;
        int size1 = strlen(d.performers);
        int size2 = strlen(d.label);
        performers = new char[size1 + 1];
        label = new char[size2 + 1];
        strcpy_s(performers, size1 + 1, d.performers);
        strcpy_s(label, size2 + 1, d.label);
        selections = d.selections;
        playtime = d.playtime;
        return *this;
    }
    Classic::Classic(const char* s1, const char* s2, const char* s3, int n, double x) :Cd(s1, s2, n, x) {
        name = new char[strlen(s3) + 1];
        strcpy_s(name,strlen(s3)+1, s3);
    }
    Classic::Classic(const Classic& d, char* c) : Cd(d) {
        name = new char[strlen(c) + 1];
        strcpy_s(name, strlen(c) + 1, c);
    }
    Classic::Classic() {
        name = new char[1];
        name[0] = '';
    }
    Classic::~Classic() {
        delete[] name;
    }
    void Classic::Report() const {
        cout << "name: " << name << endl;
        Cd::Report();
    }
    Classic& Classic::operator=(const Classic& d) {
        if (this == &d)
            return *this;
        Cd::operator=(d);
        delete[] name;
        name = new char[strlen(d.name) + 1];
        strcpy_s(name,strlen(d.name)+1, d.name);
        return *this;
    }

    13-3

    #pragma once
    #ifndef ABC_H_
    #define ABC_H_
    #include <iostream>
    using namespace std;
    class Abc {
    private:
        char* label;
        int rating;
    public:
        Abc(const char* l = "null", int r = 0);
        Abc(const Abc& rs);
        Abc& operator=(const Abc& rs);
        virtual ~Abc();
        virtual void View() = 0;
        friend ostream& operator<<(ostream& os, const Abc& rs);
        char* getLabel() { return label; }
        int getRating() { return rating; }
    };
    class baseDMA :public Abc {
    public:
        baseDMA(const char* l = "null", int r = 0);
        baseDMA(const baseDMA& rs);
        virtual void View();
    };
    class lacksDMA :public Abc {
    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 Abc& rs);
        friend ostream& operator<<(ostream& os, const lacksDMA& ls);
        virtual void View();
    };
    class hasDMA : public Abc
    {
    private:
        char * style;
    public:
        hasDMA(const char * s = "none", const char * l = "null", int r = 0);
        hasDMA(const char * s, const Abc & rs);
        hasDMA(const hasDMA & hs);
        ~hasDMA();
    
        hasDMA & operator=(const hasDMA & hs);
        friend std::ostream& operator<<(std::ostream& os, const hasDMA& hs);
        virtual void View();
    };
    #endif // !ABC_H_
    #include "pch.h"
    #include "abc.h"
    #include <iostream>
    #include <cstring>
    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& rs) {
        label = new char[strlen(rs.label) + 1];
        strcpy_s(label, strlen(rs.label) + 1, rs.label);
        rating = rs.rating;
    }
    Abc& Abc::operator=(const Abc& rs) {
        if (this == &rs)
            return *this;
        delete[] label;
        label = new char[strlen(rs.label) + 1];
        strcpy_s(label, strlen(rs.label) + 1, rs.label);
        rating = rs.rating;
        return *this;
    }
    Abc::~Abc() {
        delete[] label;
    }
    void Abc::View() {
        cout << "Label: " << label << endl;
        cout << "Rating: " << rating << endl;
    }
    ostream& operator<<(ostream& os, const Abc& rs) {
        os << "Label: " << rs.label << endl;
        os << "Rating: " << rs.rating << endl;
        return os;
    }
    baseDMA::baseDMA(const char * l, int r) : Abc(l, r)
    {
    }
    
    baseDMA::baseDMA(const baseDMA & rs) : Abc(rs)
    {
    }
    
    void baseDMA::View()
    {
        Abc::View();
    }
    lacksDMA::lacksDMA(const char * c, const char * l, int r) : Abc(l, r)
    {
        strcpy_s(color, c);
        color[COL_LEN - 1] = '';
    }
    
    lacksDMA::lacksDMA(const char * c, const Abc & rs) : Abc(rs)
    {
        strcpy_s(color, c);
        color[COL_LEN - 1] = '';
    
    }
    
    std::ostream& operator<<(std::ostream& os, const lacksDMA & ls)
    {
        os << (const Abc&)ls;
        os << "Color: " << ls.color << endl;
        return os;
    }
    
    void lacksDMA::View()
    {
        Abc::View();
        cout << "Color: " << color << endl;
    }
    
    //hasbase类  
    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 char * s, const Abc & rs) : Abc(rs)
    {
        style = new char[strlen(s) + 1];
        strcpy_s(style,strlen(s)+1, s);
    }
    
    hasDMA::~hasDMA()
    {
        delete[] style;
    }
    
    void hasDMA::View()
    {
        Abc::View();
        cout << "Style: " << style << endl;
    }
    
    hasDMA & hasDMA::operator=(const hasDMA & hs)
    {
        if (this == &hs)
            return *this;
        delete[] style;
        Abc::operator=(hs);
        style = new char[strlen(hs.style) + 1];
        strcpy_s(style, strlen(hs.style) + 1, hs.style);
        return *this;
    }
    
    std::ostream& operator<<(std::ostream& os, const hasDMA& hs)
    {
        os << (const Abc&)hs;
        os << "Style: " << hs.style << endl;
        return os;
    }
    #include "pch.h"
    #include <iostream>
    #include <string>
    #include <cstring>
    #include "abc.h"
    const int BASE = 3;
    int main(void) {
        Abc * p_base[BASE];
        char temp[40];
        char tcolor[40];
        char tstyle[40];
        int tempnum;
        int kind;
    
        for (int i = 0; i < BASE; i++)
        {
            cout << "Enter base's label: " << endl;
            cin.getline(temp, 40);
            cout << "Enter base's rating: " << endl;
            cin >> tempnum;
            cout << "Enter 1 for base, 2 for lacksbase or 3 for hasbase: " << endl;
            while (cin >> kind && (kind != 1 && kind != 2 && kind != 3))
            {
                cout << "Enter either 1, 2 or 3: " << endl;
            }
            //混合输入数字和字符时记得注意,
            //数字输入后留换行符在输入队列中,并没去掉、 
            cin.get();//去掉换行符 
            if (kind == 1)
            {
                p_base[i] = new baseDMA(temp, tempnum);
            }
            else if (kind == 2)
            {
    
                cout << "Enter the color: " << endl;
                cin.get(tcolor, 40);
                p_base[i] = new lacksDMA(tcolor, temp, tempnum);
            }
            else if (kind == 3)
            {
                cout << "Enter the style: " << endl;
                cin.getline(tstyle, 40);
                p_base[i] = new hasDMA(tstyle, temp, tempnum);
            }
            while (cin.get() != '
    ')
                continue;
        }
        cout << endl;
        for (int i = 0; i < BASE; i++)
        {
            p_base[i]->View();
            cout << endl;
        }
    
        for (int i = 0; i < BASE; i++)
        {
            delete p_base[i];//释放对象 
        }
        cout << "Done.
    ";
        return 0;
    }

    13-4

    #pragma once
    #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);
    };
    class VintagePort :public Port {
    private:
        char* nickname;
        int year;
    public:
        VintagePort();
        VintagePort(const char* br, const char* st, 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 // !PORT_H_
    #include "pch.h"
    #include "port.h"
    #include <cstring>
    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, 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, p.style);
        bottles = p.bottles;
    }
    
    Port & Port::operator=(const Port & p)
    {
        if (&p == this)
            return (*this);
        delete[] brand;
        brand = new char[strlen(p.brand) + 1];
        strcpy_s(brand, strlen(p.brand) + 1, p.brand);
        strcpy_s(style, p.style);
        bottles = p.bottles;
    }
    
    Port & Port::operator+=(int b)
    {
        bottles += b;
        return *this;//记得返回值    
    }
    
    
    Port & Port::operator-=(int b)
    {
        if (bottles >= b)
            bottles -= b;
        if (bottles < b)
            std::cout << "You don't have enough bottles.";
        return *this;//记得返回值
    }
    
    void Port::Show() const
    {
        std::cout << "Brand: " << brand << std::endl;
        std::cout << "Kind: " << style << std::endl;
        std::cout << "Bottles: " << bottles << std::endl;
    }
    
    ostream & operator<<(ostream & os, const Port & p)
    {
        os << p.brand << ", " << p.style << ", " << p.bottles;
        return os;
    }
    
    //vintageport类
    VintagePort::VintagePort() : Port()
    {
        nickname = new char[1];
        nickname[0] = '';
        year = 0;
    }
    
    VintagePort::VintagePort(const char * br, const char * st, int b, const char * nn, int y) : Port(br, st, b)
    {
        nickname = new char[strlen(nn) + 1];
        strcpy_s(nickname, strlen(nn) + 1, nn);
        year = y;
    }
    
    VintagePort::VintagePort(const VintagePort & vp) : Port(vp)
    {
        nickname = new char[strlen(vp.nickname) + 1];
        strcpy_s(nickname, strlen(vp.nickname) + 1, vp.nickname);
        year = vp.year;
    }
    
    VintagePort & VintagePort::operator=(const VintagePort & vp)
    {
        if (&vp == this)
            return (*this);
        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();
        std::cout << "Nickname: " << nickname << std::endl;
        std::cout << "Year: " << year << std::endl;
    }
    
    ostream & operator<<(ostream & os, const VintagePort & vp)
    {
        os << (const Port&)vp;
        os << ", " << vp.nickname << ", " << vp.year;
        return os;
    }
    #include "pch.h"
    #include <iostream>
    #include <string>
    #include <cstring>
    #include "port.h"
    const int BASE = 3;
    int main(void) {
        Port port1("gallo", "tawny", 20);
        cout << port1 << endl << endl;
        VintagePort vp("gallo", "vintage", 24, "Old Velvet", 16);
        VintagePort vp2(vp);
        cout << vp2 << endl << endl;
        VintagePort vp3;
        vp3 = vp;
        cout << vp3 << endl << endl;
    
        Port * p_port;
        p_port = &port1;
        p_port->Show();
        cout << endl;
        p_port = &vp;
        p_port->Show();
        cout << endl;    return 0;
    }
  • 相关阅读:
    速达开发小组面试题分享下
    NC V6 nchome文件目录及其作用介绍
    GIL 线程池 进程池 同步 异步 阻塞 非阻塞
    线程
    守护进程进程安全
    并发
    22个所见即所得在线 Web 编辑器
    asp.net操纵Oracle存储过程
    JQuery 中each的使用方法
    JQuery实现简单的服务器轮询效果
  • 原文地址:https://www.cnblogs.com/lightmonster/p/10516134.html
Copyright © 2020-2023  润新知