C++primer plus 13th Chapter 2nd of Programming question

Source: Internet
Author: User

#pragmaOnce#ifndef Cd_h_#defineCd_h_//base classclasscd{Private:    Char*performers; Char*label; intSelection//Number of selection    DoublePlaytime; Public: Cd (); Cd (ConstCd &d); Cd (Char* S1,Char* S2,intNDoublex); Virtual~Cd (); Virtual voidReport ()Const;//reports all CD dataCd &operator= (ConstCd &d);};#endif // ! Cd_h_#pragmaOnce
CD.h
#pragmaOnce#ifndef Classic_h_#defineClassic_h_#include"CD.h"classClassic: Publiccd{Private:    Char*Mainprod; Public: Classic (); Classic (Char* S1,Char* S2,Char* S3,intNDoublex); Classic (ConstClassic &a); ~Classic (); Classic&operator=(ConstClassic &a); voidReport ()Const;};#endif // ! Classic_h_
classic.h
#include"classic.h"#include<cstring>#include<iostream>usingstd::strcpy;usingStd::strlen;usingstd::cout;usingStd::endl;//Base CDCD::CD () {performers=New Char[1]; performers[0] =' /'; Label=New Char[1]; label[0] =' /';//constructor maintains uniform format corresponding destructorSelection =0; Playtime=0.0;} CD::CD (ConstCd &a) {    intLen; 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=(ConstCd &a) {    //Remember the first step is to delete the previous, save memory    Delete[]performers; Delete[]label; intLen; 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,intNDoublex) {    intLen; Len=strlen (S1); Performers=New Char[Len +1];//Remember to add one, strlen does not count 'strcpy (performers, S1); Len=strlen (S2); Label=New Char[Len +1];    strcpy (label, S2); Selection=N; Playtime=x;} Cd::~Cd () {Delete[]performers; Delete[]label;}voidCd::report ()Const{cout<<"Performers:"<< Performers <<Endl; cout<<"Label:"<< label <<Endl; cout<<"Selection:"<< selection <<Endl; cout<<"Playtime:"<< Playtime <<Endl;} Classic::~Classic () {//CD::~CD;//This sentence does not need to write an error, repeat delete    Delete[]mainprod;//derived destructors are only good for deleting new members in derived classes.}classic::classic (): Cd () {Mainprod=New Char[1]; mainprod[0] =' /';//constructors are unified to form a compatible destructor}classic::classic (Char* S1,Char* S2,Char* S3,intADoublex): Cd (S1, S2, A, x) {intLen; Len=strlen (S3); Mainprod=New Char[Len +1]; strcpy (Mainprod, S3);} Classic::classic (ConstClassic &a): Cd (a) {intLen; Len=strlen (A.mainprod); Mainprod=New Char[Len +1]; strcpy (Mainprod, A.mainprod);} Classic& Classic::operator=(ConstClassic &a) {    //first, use the base class overload = Assign a value to the base class partCd::operator=(a); Delete[]mainprod; intLen; Len=strlen (A.mainprod); Mainprod=New Char[Len +1];    strcpy (Mainprod, A.mainprod); //don't forget to return the value    return* This;}voidClassic::report ()Const{cd::report (); cout<<"mainproduction:"<< Mainprod <<Endl;}
Method.cpp
#include <iostream>using namespacestd; #include"classic.h"    //Would contain #include cd.hvoidBravo (ConstCd &disk);//Remember to modify the Cd.h file yourselfintMain () {Cd C1 ("Beatles","Capitol", -,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;}voidBravo (ConstCd &disk) {disk. Report ();}
Test.cpp

Call the base class construct first, then call the derived class construct, the destructor instead, call the derived class destructor first, and then call the destructor of the base class.

The destructor of the derived class is only used to delete the new member in the derivation, not the base class part, because the derived class destructor automatically calls the destructor of the base class , so his own responsibility is to clean up the work performed on the constructor of the derived class

C++primer plus 13th Chapter 2nd of Programming question

Related Article

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.