C++ primer第六版 13章课后题解

本文展示了一个使用C++实现的CD类及其派生类Classic的示例代码,重点介绍了如何利用动态内存分配来处理字符串数据,同时展示了构造函数、析构函数、赋值运算符重载等关键特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

12.1 以下面的类声明为基础:
头文件

//
//  classic.h
//  classic
//
//  Created by apple on 2018/8/15.
//  Copyright © 2018年 apple. All rights reserved.
//

#ifndef classic_h
#define classic_h

class Cd{
private:
    char performers[50];
    char label[20];
    int selections; //number of selections
    double playtime;
public:
    Cd(char *s1, char* s2, int n, double x);
    Cd(const Cd &d);
    Cd();
    ~Cd();
    virtual void Report() const;//reports all CD data
    Cd & operator=(const Cd &d);
};

class Classic:public Cd{
public:
    Classic(char *s1, char* s2, char *s3, int select, int play);
    Classic();
    ~Classic();
    virtual void Report() const;
    Classic& operator = (const Classic &c);
private:
    char name[50];
};

#endif /* classic_h */

头文件实现文件

//
//  classic.cpp
//  classic
//
//  Created by apple on 2018/8/15.
//  Copyright © 2018年 apple. All rights reserved.
//

#include <iostream>
#include "classic.h"
using namespace std;

Cd::Cd(char *s1, char* s2, int n, double x){
    strcpy(performers, s1);
    strcpy(label, s2);
    selections = n;
    playtime = x;
}

Cd::Cd(const Cd &d){
    strcpy(performers, d.performers);
    strcpy(label, d.label);
    selections = d.selections;
    playtime = d.playtime;
}
Cd::Cd(){

}
Cd::~Cd(){

}
void Cd::Report() const{
    cout<<"in CD class, Performers = "<<performers<<" label=  "<<label<<" ,selection = "<<selections<<" ,playtime = "<<playtime<<endl;

}
Cd & Cd::operator=(const Cd &d){
    if(this == &d)
        return *this;
    strcpy(performers, d.performers);
    strcpy(label, d.label);
    selections = d.selections;
    playtime = d.playtime;
    return *this;
}

Classic::Classic(){

}
Classic::~Classic(){

}

Classic::Classic(char *s1, char* s2, char *s3, int select, int play):Cd(s1,s2,select,play){
    strcpy(name, s3);
}

void Classic::Report() const{
    Cd::Report();
    cout<<"name = "<<name<<endl;
}

Classic& Classic::operator = (const Classic &c){
    if(this == &c)
        return *this;
    Cd::operator=(c);
    strcpy(name, c.name);
    return *this;
}

测试文件

//
//  main.cpp
//  classic
//
//  Created by apple on 2018/8/15.
//  Copyright © 2018年 apple. All rights reserved.
//

#include <iostream>
#include "classic.h"
using namespace std;

void Bravo(const Cd& disk);

int main(int argc, const char * argv[]) {
    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: ";
    Classic copy;
    copy = c2;
    copy.Report();

    return 0;
}

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

2.完成练习1,但让两个类使用动态内存分配而不是长度固定的数组来记录字符串
头文件

//
//  classic.h
//  classic
//
//  Created by apple on 2018/8/15.
//  Copyright © 2018年 apple. All rights reserved.
//

#ifndef classic_h
#define classic_h

class Cd{
private:
  //  char performers[50];
    char* performers;
//    char label[20];
    char* label;
    int selections; //number of selections
    double playtime;
public:
    Cd(char *s1, char* s2, int n, double x);
    Cd(const Cd &d);
    Cd();
    virtual ~Cd();
    virtual void Report() const;//reports all CD data
    Cd & operator=(const Cd &d);
};

class Classic:public Cd{
public:
    Classic(char *s1, char* s2, char *s3, int select, int play);
    Classic();
    virtual ~Classic();
    virtual void Report() const;
    Classic& operator = (const Classic &c);
private:
  //  char name[50];
    char* name;
};

#endif /* classic_h */

头文件实现文件

//
//  classic.cpp
//  classic
//
//  Created by apple on 2018/8/15.
//  Copyright © 2018年 apple. All rights reserved.
//

#include <iostream>
#include "classic.h"
using namespace std;

Cd::Cd(char *s1, char* s2, int n, double x){
    int len1 = strlen(s1);
    performers = new char[len1+1];
    strcpy(performers, s1);
    int len2 = strlen(s2);
    label = new char[len2+1];
    strcpy(label, s2);
    selections = n;
    playtime = x;
}

Cd::Cd(const Cd &d){
    strcpy(performers, d.performers);
    strcpy(label, d.label);
    selections = d.selections;
    playtime = d.playtime;
}
Cd::Cd(){

}
Cd::~Cd(){
    cout<<"delete Cd\n";
    delete []performers;
    delete []label;
}
void Cd::Report() const{
    cout<<"in CD class, Performers = "<<performers<<" label=  "<<label<<" ,selection = "<<selections<<" ,playtime = "<<playtime<<endl;

}
Cd & Cd::operator=(const Cd &d){
    if(this == &d)
        return *this;
    performers = new char[strlen(d.performers)+1];
    strcpy(performers, d.performers);
    label = new char[strlen(d.label)+1];
    strcpy(label, d.label);
    selections = d.selections;
    playtime = d.playtime;
    return *this;
}

Classic::Classic(){


}
Classic::~Classic(){
    cout<<"Delete Classic\n";
    delete []name;
}

Classic::Classic(char *s1, char* s2, char *s3, int select, int play):Cd(s1,s2,select,play){
    int len = strlen(s3);
    name = new char[len+1];
    strcpy(name, s3);
}

void Classic::Report() const{
    Cd::Report();
    cout<<"name = "<<name<<endl;
}

Classic& Classic::operator = (const Classic &c){
    if(this == &c)
        return *this;
    Cd::operator=(c);
    name = new char[strlen(c.name)+1];
    strcpy(name, c.name);
    return *this;
}

测试文件

//
//  main.cpp
//  classic
//
//  Created by apple on 2018/8/15.
//  Copyright © 2018年 apple. All rights reserved.
//

#include <iostream>
#include "classic.h"
using namespace std;

void Bravo(const Cd& disk);

int main(int argc, const char * argv[]) {
    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: ";
    Classic copy;
    copy = c2;
    copy.Report();

    return 0;
}

void Bravo(const Cd & disk){
    disk.Report();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值