c++primer plus 第13章 编程题第2题

#pragma once
#ifndef CD_H_
#define CD_H_
//base class

class Cd
{
private:
    char * performers;
    char * label;
    int selection;    //number of selection
    double playtime;
public:
    Cd();
    Cd(const Cd & d);
    Cd(char * s1, char * s2, int n, double x);
    virtual ~Cd();
    virtual void Report() const;    //reports all cd data
    Cd & operator= (const Cd & d);
};
#endif // !CD_H_
#pragma once

CD.h

#pragma once
#ifndef CLASSIC_H_
#define CLASSIC_H_
#include"CD.h"

class Classic : public Cd
{
private:
    char * mainprod;
public:

    Classic();
    Classic(char * s1, char * s2, char * s3, int n, double x);
    Classic(const Classic & a);
    ~Classic();
    Classic & operator=(const Classic & a);
    void Report() const;
};

#endif // !CLASSIC_H_

classic.h

#include"classic.h"
#include<cstring>
#include<iostream>
using std::strcpy;
using std::strlen;
using std::cout;
using std::endl;
//base cd

Cd::Cd()
{
    performers = new char[1];
    performers[0] = ‘\0‘;
    label = new char[1];
    label[0] = ‘\0‘;    //构造函数保持统一格式对应析构函数
    selection = 0;
    playtime = 0.0;
}

Cd::Cd(const Cd & a)
{
    int len;
    len = strlen(a.performers);
    performers = new char[len + 1];
    strcpy(performers, a.performers);
    len = strlen(a.label);
    label = new char[len + 1];
    strcpy(label, a.label);
    selection = a.selection;
    playtime = a.playtime;
}

Cd & Cd::operator=(const Cd & a)
{
    //记住第一步要先delete掉以前的,节省内存
    delete[]performers;
    delete[]label;
    int len;
    len = strlen(a.performers);
    performers = new char[len + 1];
    strcpy(performers, a.performers);
    len = strlen(a.label);
    label = new char[len + 1];
    strcpy(label, a.label);
    selection = a.selection;
    playtime = a.playtime;
    return *this;
}

Cd::Cd(char * s1, char * s2, int n, double x)
{
    int len;
    len = strlen(s1);
    performers = new char[len + 1];    //记得要加一,strlen不算‘\0’
    strcpy(performers, s1);
    len = strlen(s2);
    label = new char[len + 1];
    strcpy(label, s2);
    selection = n;
    playtime = x;
}

Cd::~Cd()
{
    delete[]performers;
    delete[]label;
}

void Cd::Report() const
{
    cout << "performers: " << performers << endl;
    cout << " label: " << label << endl;
    cout << " selection: " << selection << endl;
    cout << " playtime: " << playtime << endl;
}

Classic::~Classic()
{
    //Cd::~Cd;//这句不用,写了报错,重复删除
    delete[]mainprod;//派生析构只用删除派生类里的新成员就好
}

Classic::Classic() : Cd()
{
    mainprod = new char[1];
    mainprod[0] = ‘\0‘;    //构造函数要统一型式以兼容析构函数
}

Classic::Classic(char * s1, char * s2, char * s3, int a, double x) : Cd(s1, s2, a, x)
{
    int len;
    len = strlen(s3);
    mainprod = new char[len + 1];
    strcpy(mainprod, s3);
}

Classic::Classic(const Classic & a) : Cd(a)
{
    int len;
    len = strlen(a.mainprod);
    mainprod = new char[len + 1];
    strcpy(mainprod, a.mainprod);
}

Classic & Classic::operator=(const Classic & a)
{
    //先要用基类的重载= 给基类部分赋值
    Cd::operator=(a);
    delete[]mainprod;
    int len;
    len = strlen(a.mainprod);
    mainprod = new char[len + 1];
    strcpy(mainprod, a.mainprod);
    //别忘记要返回值
    return *this;
}

void Classic::Report() const
{
    Cd::Report();
    cout << " mainproduction: " << mainprod << endl;
}

method.cpp

#include<iostream>
using namespace std;
#include"classic.h"    //will contain #include cd.h
void Bravo(const Cd & disk);
//记住要自己修改cd.h文件
int main()
{
    Cd c1("Beatles", "Capitol", 14, 35.5);
    Classic  c2 = Classic("Piano Sonata in B flat, Fantasia in C", "Alfred Brendel", "Philios", 2, 57.17);

    Cd * pcd = &c1;

    cout << "using object directly:\n";
    c1.Report();
    c2.Report();

    //Cd sd;

    cout << "using type cd * pointer to object:\n";
    pcd->Report();
    pcd = &c2;
    pcd->Report();

    cout << "Calling a function with a Cd reference argument:\n";
    Bravo(c1);
    Bravo(c2);

    cout << "testing assignment" << endl;
    Classic copy;
    copy = c2;
    copy.Report();
    return 0;
}

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

test.cpp

  先调用基类构造,再调用派生类构造,析构函数则相反,先调用派生类析构,再调用基类的析构函数。

  派生类的析构只用删除派生里的新成员,而不用管基类部分,因为派生类析构函数会自动调用基类的析构函数,所以他自身的职责是对派生类的构造函数执行工作的清理

原文地址:https://www.cnblogs.com/syne-cllf/p/9266589.html

时间: 2024-08-30 02:15:48

c++primer plus 第13章 编程题第2题的相关文章

c++primer plus 第11章 编程题第7题

#pragma once #ifndef COMPLEX0_H_ #define COMPLEX0_H_ #include<iostream> class Complex { private: double real; double imgn; public: Complex(); Complex(double a, double b); ~Complex(); Complex operator+(const Complex & a) const; Complex operator-(

c++primer plus 第12章 编程题第1题

一开始的程序里 类不能连续赋值,内存会报错.debug了一整晚发现是new里面的数组n++出错.经过测试++n和n+1都能使程序正确运行. n++会报错是因为先使用n,再对n加1.而我们的期望是开辟一个n+1长度的数组而不是n,所以会报错. 在单语句中,自增运算和普通的加法没有区别,但是在这种复合语句中千万小心,尽量不使用n++和++n #pragma once #ifndef COW_H_ #define COW_H_ class Cow { private: //class default

c primer plus 第六章编程练习2

使用嵌套循环产生下列图案:$$$$$$$$$$$$$$$ #include<stdio.h> int main(void){ int i,j; for(i=0;i<5;i++){ for(j=0;j<=i;j++){ printf("$"); } printf("\n"); } return 0; }

java第四章编程题(初学篇)

代码: 1 /* 2 test.java 3 */ 4 package test; 5 public class test { 6 public static void main(String args[] ) 7 { 8 CPU ccp= new CPU(); 9 HardDisk hhd=new HardDisk(); 10 PC pc =new PC(); 11 ccp.setSpeed(2200); 12 hhd.setAmount(200); 13 pc.setCPU(ccp); 14

java学习之第五章编程题示例(初学篇)

1 /* 2 Animal.java 3 */ 4 package animal; 5 6 public abstract class Animal { 7 public abstract void cry(); 8 public abstract String getanimalName(); 9 } 1 //Dog.java 2 package animal; 3 4 public class Dog extends Animal 5 { 6 7 String aa="旺旺"; 8

c++ primer plus(第6版)中文版 第十三章编程练习答案

第十三章编程练习答案 13.1根据Cd基类,完成派生出一个Classic类,并测试 //13.1根据Cd基类,完成派生出一个Classic类,并测试 #include <iostream> #include <cstring> using namespace std; // base class class Cd { char performers[50]; char label[20]; int selections; // number of selections double

c++ primer plus(第6版)中文版 第九章编程练习答案

首先,说明下环境: linux:fedora14: IDE:eclipse: python:python2.7 python框架:django web服务器:apache web服务器的python模块:mod_wsgi 写在前面: 之前用的windows下面的xampp,写的php后台,现在想转向linux下面的python,跟以前一样,选择apache和eclipse作为自己的开发工具. eclipse的python配置, 参见之前的博客:http://blog.csdn.net/zy416

C和指针第13章第4题

题目:编写一个函数,它用于对一个任何类型的数组进行排序. 算法核心代码sort函数实现 /** 功能说明:sort函数可以对不同类型的数据进行排序 参数: 1.一个指向需要排序的数组的第一个值的指针. 2.数组中元素的个数. 3.每个数组元素的长度. 4.一个指向比较回调函数的指针 **/ void sort(void *array,int size,int element_size,int (*cmp)(void const *a,void const *b)) { char *pc = (c

c++ primer plus(第6版)中文版 第十二章编程练习答案

第十二章编程练习答案 12.1根据以下类声明,完成类,并编小程序使用它 //12.1根据以下类声明,完成类,并编小程序使用它 #include <iostream> #include <cstring> using namespace std; class Cow{ char name[20]; char * hobby; double weight; public: Cow(); Cow(const char * nm, const char * ho, double wt);