C++primer plus sixth edition after class programming practice answers 13.2__linux

Source: Internet
Author: User
#ifndef cd_h_ #define Cd_h_ struct Strandlen {char *ch;
int Len;

};
	Base class class cd{//represents a Cd disk private:strandlen performers;
	Strandlen label; int Selections;//number of selections double playtime;//playing time in minutes Public:char *perfor () Const{return Perfo
	rmers.ch;}
	Char *lab () Const{return label.ch;}
	int sel () Const{return selections;}
    Double pla () Const{return playtime;  
    void Setper (char *p);
    void Setlab (char *l);  
    void SetSel (int num) {selections=num;};  

	void Setplay (double s) {playtime=s;};
	void SetChar (Strandlen &s,const char *c);

	void Delchar (Strandlen &s);
	Cd (char *s1,char *s2,int n,double x);
	CD (const CD &D); Cd () {performers.ch=null;label.ch=null;}
	The pointer passed to the delete must point to dynamically allocated memory, or a null pointer, so to prevent error from the Delete field pointer, first place the pointer as an empty virtual ~cd ();
virtual void () Const;//report all CD data virtual CD &operator= (const CD &D);

}; Class Classic:public Cd {Private:strandlen works; Public:classic (char *w,char *s1,chAR *s2,int n,double x);
	Classic (const Classic &c);
	Classic () {works.ch=null;}
	Virtual ~classic ();
	virtual void () () const;
Virtual Classic &operator= (const Classic &c);

}; #endif
#include <iostream> #include "Cd.h" using namespace std;
	CD::CD (char *s1,char *s2,int n,double x) {SetChar (PERFORMERS,S1);
	SetChar (LABEL,S2);
	Selections=n;
Playtime=x; } cd::cd (const Cd &d) {SetChar (performers,d.performers.ch);//The Constant object converts its method to a regular method, causing the match to be less than the corresponding method SetChar (label,

	d.label.ch);
	Selections=d.selections;
Playtime=d.playtime;
	} cd::~cd () {delete [] performers.ch;

	delete [] label.ch;
	Performers.ch=null;

	Label.ch=null;
	performers.len=0;

	label.len=0;

selections=playtime=0;
	} void Cd::setper (char *p) {Delchar (performers);
SetChar (PERFORMERS,P);
	} void Cd::setlab (char *l) {Delchar (label);
SetChar (label,l);
	} void Cd::setchar (Strandlen &s,const char *c) {S.len=strlen (c);
	S.ch=new char[s.len+1];
strcpy (S.CH,C); } void Cd::d elchar (Strandlen &s) {//s.ch=null;//to place the pointer first and then delete will result in a memory leak, that is, the previously new memory address can not be found, can not be released, Causes the system to not call this memory during the program run//You directly assign a value of 0, but before the contents of the pointer is still, the system is still for your program to retain the memory of the previously approved, this part of the memory you do not use, but other programs are not used, that is, memory leaks,//dynamic allocation of memory needs to release their own, Statically allocated memoryYou can delete s.ch=null without releasing the delete [] s.ch;//new;
s.len=0; } void Cd::report () const {cout<<performers.ch<< "<<label.ch<<" &LT;&LT;SELECTIONS&LT;&L t; "

"<<playtime<<" <<endl;}

	Cd & cd::operator = (const CD &d) {if (this==&d) return *this;
	Delchar (performers);
	
	Delchar (label);

	SetChar (Performers,d.perfor ());//A Constant object converts its method to a regular method, resulting in a corresponding method SetChar (Label,d.lab ()) that does not match the non-const method.
	Selections=d.selections;

	Playtime=d.playtime;
return *this;

} classic::classic (char *w,char *s1,char *s2,int n,double x): Cd (s1,s2,n,x) {SetChar (works,w);}

Classic::classic (const Classic &c): Cd (C.perfor (), C.lab (), C.sel (), C.pla ()) {SetChar (works,c.works.ch);}
	Classic::~classic () {delete [] works.ch;
	Works.ch=null;

	works.len=0;


CD::~CD ();
	} void Classic::report () const {cout<<works.ch<< ""; Cd::report ()//Call parent class method with the same name Classic & classic::operator = (const Classic &c)//Cannot delete dynamic array {if (this==&am) without newP;C) return *this;
	Delchar (works);
	SetChar (works,c.works.ch);
	Setper (C.perfor ());
	Setlab (C.lab ());
	SetSel (C.sel ());

	Setplay (C.pla ());
return *this; }

#include <iostream>
#include "Cd.h"

using namespace std;

void Bravo (const Cd &disk);

int main ()
{
	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;//Pointer PCD destructor will point to the image destructor, make this object destructor error, solution, object destructor, the pointer in the class empty, null pointer can be multiple delete

	cout<< "Using object Directly:\n ";
	C1. A ();
	C2. A ();

	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:";
	Classic copy;
	COPY=C2;
	Copy. A ();

	return 0;
}

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


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.