C ++ Primer Plus (6th) Chapter 1 programming question 2, primer Chapter 2

Source: Internet
Author: User

C ++ Primer Plus (6th) Chapter 1 programming question 2, primer Chapter 2
Chapter 2 class inheritance 13th second question of programming exercises

1 header file source code: "classic. h"

 1 #ifndef CLASSIC_H_INCLUDED 2 #define CLASSIC_H_INCLUDED 3  4 class Cd 5 { 6 private: 7     char *performers; 8     char *label; 9     int selections;10     double playtime;11 public:12     Cd(char * s1, char * s2, int n, double x);13     Cd();14     Cd(const Cd & C);15     virtual ~Cd();16     virtual void Report() const;17     Cd & operator=(const Cd &d);18 };19 20 class Classic : public Cd21 {22 private:23     char *details;24 public:25     Classic(char * s0, char * s1, char * s2, int n, double x);26     Classic() {}27     Classic(const Classic & C);28     Classic(char * s0, const Cd  & d);29     ~Classic();30     virtual void Report() const;31     Classic & operator=(const Classic & C);32 33 };34 35 #endif // CLASSIC_H_INCLUDED

 

2 header file implementation:

  1 #include <iostream>  2 #include <cstring>  3 #include "classic.h"  4   5 using std::cout;  6 using std::endl;  7   8 Cd::Cd(char * s1, char * s2, int n, double x)  9 { 10     int len1 = std::strlen(s1); 11     int len2= std::strlen(s2); 12     performers = new char[len1+1]; 13     label = new char[len2+1]; 14     std::strcpy(performers, s1); 15     std::strcpy(label, s2); 16     selections = n; 17     playtime = x; 18 } 19  20 Cd::Cd() 21 { 22     performers = 0; 23     label = 0; 24     selections = 0; 25     playtime = 0.0; 26 } 27  28 Cd::Cd(const Cd & C) 29 { 30     int len1 = std::strlen(C.performers); 31     int len2= std::strlen(C.label); 32     performers = new char[len1+1]; 33     label = new char[len2+1]; 34     std::strcpy(performers, C.performers); 35     std::strcpy(label, C.label); 36     selections = C.selections; 37     playtime = C.playtime; 38 } 39 Cd::~Cd() 40 { 41     delete [] performers; 42     delete [] label; 43 } 44  45 void Cd::Report() const 46 { 47     cout << "performers: " << performers << endl; 48     cout << "label: " << label << endl; 49     cout << "selection: " << selections << endl; 50     cout << "playtime: " << playtime << endl; 51  52 } 53  54 Cd & Cd::operator=(const Cd &C) 55 { 56     if(this == &C) 57         return *this; 58     int len1 = std::strlen(C.performers); 59     int len2= std::strlen(C.label); 60     performers = new char[len1+1]; 61     label = new char[len2+1]; 62     std::strcpy(performers, C.performers); 63     std::strcpy(label, C.label); 64     selections = C.selections; 65     playtime = C.playtime; 66     return *this; 67 } 68  69  70 Classic::Classic(char * s0, char * s1, char * s2, int n, double x):Cd(s1, s2, n, x) 71 { 72     int len = std::strlen(s0); 73     details = new char[len+1]; 74     std::strcpy(details, s0); 75 } 76  77  78  79 Classic::Classic(char * s0, const Cd &C):Cd(C) 80 { 81     details = new char[std::strlen(s0)+1]; 82     std::strcpy(details, s0); 83 } 84  85 Classic::Classic(const Classic & C):Cd(C) 86 { 87     int len = std::strlen(C.details); 88     details = new char[len+1]; 89     std::strcpy(details, C.details); 90 } 91  92 Classic::~Classic() 93 { 94     delete [] details; 95 } 96  97 void Classic::Report() const 98 { 99     Cd::Report();100     cout << "details: " << details << endl;101 }102 103 Classic & Classic::operator=(const Classic & C)104 {105     if(this == &C)106         return *this;107     Cd::operator=(C);108     delete [] details;109     int len = std::strlen(C.details);110     details = new char[len+1];111     std::strcpy(details, C.details);112     return *this;113 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.