C++ Premier Plus 6th edition - Programming excercise - Chapter13 - 1&2

Note:

  1. In base class, even if not explicitly defined copy constructor and overloaded op=, derived class still is able to revolk defaut ones. (condition: no dynamic memory used in base class)
  2. Need to explicitly use methods of base class,
    eg. Cd::Cd(d);//copy constructor of base class
    Cd::operator=();//overloaed op= of base class

classic.h

#ifndef CD_H_
#define CD_H_

// base class
class Cd   // represents a CD disk
{
private:
    char performers[50];
    char label[20];
    int selections; // number of selections
    double playtime; // playing time in minutes
public:
    Cd(const char * s1, const char * s2, int n, double x);
    Cd();

    virtual ~Cd() {};// keep it or not, both are OK,it do nothing
    virtual void Report() const; // reports all CD data
    /*
    use defaut copy construtor and op=
    virtual Cd & operator=(const Cd & d);
    */
};

class Classic :public Cd
{
private:
    // place constructor in private to level up safety,is not right
    // dirived class is not able to revolk

    char* ps;// use dynamic memory
public:
    Classic();
    Classic(const char*pt, const char * s1, const char * s2, int n, double x);
    Classic(const Classic& d);
    virtual ~Classic()
    {
        delete[]ps;
    }; // inline function
    virtual void Report() const; // reports all CD data
    virtual Classic & operator=(const Classic & d);
};

#endif

// newly added data-member,if use dynamic memory,shall keep copy constructor and op=
// if use char array[],not necessary to keep

classic.cpp

#include<iostream>
#include"classic.h"
using std::cout;
using std::endl;

// *******implementations for Cd*******
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;
}

// ignore copy constructor, use default copy constructor to shallow copy is enough

Cd::Cd()
{
    performers[0] = '\0';
    label[0] = '\0';
    selections = 0;
    playtime = 0.0;
}

void Cd::Report() const
{
    cout << "\nThe disk details: \n"
         << "Performers: ";
    for (int i = 0; i < 50 && performers[i] != '\0'; i++)
    {
        cout << performers[i];
    }
    cout << "\nSelections: " << selections << endl;
    cout << "Playtime: " << playtime << endl;
}

// *******implementations for Classic*******
Classic::Classic():Cd()
{
    ps = nullptr;
}

Classic::Classic(const char* m, const char * s1, const char * s2, int n, double x):Cd(s1,s2,n,x)
{
    int len = strlen(m) + 1;
    ps = new char[len];
    strcpy_s(ps, len, m);
}

// revolk default copy constructor
Classic::Classic(const Classic& d)
{
    Cd::Cd(d);
    int len = strlen(d.ps) + 1;
    ps = new char[len];
    strcpy_s(ps, len, d.ps);
}

void Classic::Report() const
{
    // never re-do what base class funcions alreay has done
    // explicit revolk
    Cd::Report();
    for (unsigned int i = 0; i < strlen(ps) && ps[i] != '\0'; i++)
        cout << ps[i];
    cout << endl << endl;
}
// auto revolk defaut op=?
Classic & Classic::operator=(const Classic & d)
{
    Cd::operator=(d);
    int len = strlen(d.ps) + 1;
    ps = new char[len];
    strcpy_s(ps, len, d.ps);
    return *this;
}

main

#include <iostream>
using namespace std;
#include "classic.h" // which will contain #include cd.hProgramming Exercises 781
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: \n";
    Classic copy;
    copy = c2;
    copy.Report();
    cin.get();
    return 0;
}
void Bravo(const Cd & disk)
{
    disk.Report();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 格外实用的c语言编程书籍&mdash;&mdash;premier电子版。它是由Prata编写的,是一本&lt;classic&gt;级别的大部头教材,对c语言的各个方面进行了细致地讲解。该书在全球范围内广受好评,被誉为c语言编程经典入门读物。相较于传统的纸质版,电子版更具备便携性和交互性两大特点。在电子版上,你可以轻松地进行文字搜索、章节跳转、笔记记录等操作,在阅读体验上更为人性化,使你更好地掌握知识。 此书内容详尽、深入浅出,对想从事c语言编程的软件工程师具有广泛的适用性,从初学者到进阶者都能够从中获益良多。全书共计1100多页,涵盖了c语言的基础知识和高级应用,包括数据类型、流控制语句、结构体、函数、指针、内存管理等等。同时,该书中还有大量的示例代码和练习题,非常适合用来辅助学习和巩固所学知识。 总之,c语言是计算机编程语言中必不可少的一门,而premier电子版则是学习c语言的必备工具书。不管是从事软件开发的新手,还是追求技术深度的老手,都能够从这本书中汲取到宝贵的知识,帮助他们更好地编写优秀的程序。 ### 回答2: c premier是一种用于C语言编程的电子版本集成开发环境(IDE),它可以提供一个完整的开发环境,包括编辑器、编译器和调试器。使用c premier电子版可以方便地编写和管理C语言程序,加速开发流程,减少开发错误。c premier具有先进的功能,如语法突出显示、自动完成和脚本编写等,可用于各种类型的C语言程序开发包括嵌入式系统、桌面应用程序等。此外,c premier还具有强大的调试器,可以帮助开发人员查找和修复程序中的错误。c premier电子版的优势之一是它可以在不同平台上运行,包括Windows、Mac和Linux系统。总之,c premier电子版是一种非常实用的C语言开发环境,可以轻松编写高质量的C语言程序。 ### 回答3: Premier电子版是一种数字化的电子期刊,主要用于在线发布、订阅和阅读相关的经济、商业、金融和管理等领域的文章和研究。该电子版的优势在于其能够提供便利快捷的服务,用户无需购买实体版本,通过在线订阅即可随时随地浏览该刊物,方便用户学习和参考。此外,Premier电子版可以保证内容质量和权威性,其收录的文章和报告均来自于经过严格审稿的学术或专业期刊,保证了内容的可信度和专业性。另外,Premier电子版拥有较完善的检索和分类系统,用户可以按照自己的需求和兴趣对文章进行检索和筛选,从而节省时间和精力。总的来说,Premier电子版为学术研究者、商业从业者和爱好者提供了一个高质量、可信、便捷和多样的学习和借鉴资源,有助于推动相关领域的发展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值