第十三章编程练习(2)

来源:互联网 发布:抽风解说 知乎 编辑:程序博客网 时间:2024/05/29 23:21

Classic.h

#pragma once#ifndef Classic_H_#define Classic_H_class Cd {private:char *  performers;char * label;int selections;double playtime;public:Cd(char * s1="blank", char * s2="null", int n=0, double x=0.0<span style="color:#000000;">);//注意默认构造函数的初始化</span>explicit Cd(const Cd & d);//使用explicit关键字禁止隐式转换,可以让错误出现在编译过程而不是运行过程virtual ~Cd();//为了先调用派生类的析构函数再调用基类的,基类析构函数必须声明为虚virtual void Report()const; Cd & operator = (const Cd & cd);};class Classic :public Cd {private:char * name;public:Classic(char * s1="blank", char *s2="null", char *s3="none", int n=0, double x=0.0);~Classic();explicit Classic(const Classic & c);void Report()const;Classic &operator=(const Classic & c);};#endif

Classic.cpp

#include "Classic.h"#include <iostream>Cd::Cd(char * s1, char * s2, int n, double x){performers = new char[std::strlen(s1) + 1];std::strcpy(performers, s1);label = new char[std::strlen(s2) + 1];std::strcpy(label, s2);selections = n;playtime = x;}Cd::Cd(const Cd & d){performers = new char[std::strlen(d.performers) + 1];std::strcpy(performers, d.performers);label = new char[std::strlen(d.label) + 1];std::strcpy(label, d.label);selections = d.selections;playtime = d.playtime;}Cd::~Cd(){delete[]performers;delete[]label;}void Cd::Report() const{using std::cout;using std::endl;cout << "Performers: " << performers << endl<< "Label: " << label << endl<< "Selections: " << selections << endl<< "Playtime: " << playtime << endl;}Cd & Cd::operator=(const Cd & d){if (this == &d)return *this;delete[]performers;delete[]label;performers = new char[std::strlen(d.performers) + 1];std::strcpy(performers, d.performers);label = new char[std::strlen(d.label) + 1];std::strcpy(label, d.label);selections = d.selections;playtime = d.playtime;return *this;}Classic::Classic(char * s1, char * s2, char * s3, int n, double x) :Cd(s1, s2, n, x){name = new char[std::strlen(s3) + 1];std::strcpy(name, s3);}Classic::~Classic(){delete[]name;}Classic::Classic(const Classic & c):Cd(c){name = new char[std::strlen(c.name) + 1];std::strcpy(name, c.name);}void Classic::Report() const{std::cout << "Name: " << name << std::endl;Cd::Report();}Classic & Classic::operator=(const Classic & c){if (this == &c)return *this;Cd::operator=(c);delete[] name;name = new char[std::strlen(c.name) + 1];std::strcpy(name, c.name);return *this;}

main.cpp

#include <iostream>#include "Classic.h"void Bravo(const Cd & disk);using namespace std;int main(){Cd c1("Beatles", "Capitol", 14, 35.5);//Classic c2 = Classic("Piano Sonata in B flat, Fantasia in C",//"Alfred Brendel", "Philips", 2, 7.17); # 因为使用了explicit关键字,所以这段代码不能匹配到复制构造函数
Classic c2("Piano Sonata in B flat, Fantasia in C",  "Alfred Brendel", "Philips", 2, 7.17);Cd * pcd = &c1;cout << "Using object directly:\n";c1.Report();cout << endl;c2.Report();cout << endl;cout << "Using type cd * pointer to object:\n";pcd->Report();cout << endl;pcd = &c2;pcd->Report();cout << "\nCalling 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();std::cout << endl;}



0 0
原创粉丝点击