【13.2】c++ primer plus 课后编程答案

来源:互联网 发布:厚黑学讲解 知乎 编辑:程序博客网 时间:2024/06/06 18:05

C++ PRIMER PLUS 课后答案 
使用IDE为window7系统下的VS2010

/*user.h*/#ifndef USERSH_H_#define USERSH_H_#include <string>using std::ostream;using std::istream;using std::string;class Cd{private:char * performers;char * lable;int selections;double playtime;public:Cd(char * s1, char * s2, int n, double x);Cd(const Cd & d);Cd();virtual ~Cd(){delete [] performers; delete [] lable;}virtual void Report() const;Cd & operator = (const Cd & d);};class Classic : public Cd{private:char * works;public:Classic(char * s3, char * s1, char * s2, int n, double x);Classic(char * s3, const Cd & d);Classic(const Classic & c);Classic();~Classic(){delete works;}virtual void Report() const;Classic & operator = (const Classic & c);};#endif

/*userfucntion.cpp*/#include "usersh.h"#include <iostream>#include <cstring>using std::cout;using std::cin;using std::endl;using std::string;using std::ostream;using std::istream;Cd::Cd(){performers = new char [1];performers[0] = '\0';lable = new char [1];lable = '\0';selections = 0;playtime = 0.0;}Cd::Cd(char * s1, char * s2, int n, double x){performers = new char [strlen(s1)+1];strcpy(performers, s1);lable = new char [strlen(s2)+1];strcpy(lable, s2);selections = n;playtime = x;}Cd::Cd(const Cd & d){performers = new char [strlen(d.performers)+1];lable = new char [strlen(d.lable)+1];strcpy(performers, d.performers);strcpy(lable, d.lable);selections = d.selections;playtime = d.playtime;}Cd & Cd::operator =(const Cd & d){if (this == &d)return *this;delete [] performers;delete [] lable;performers = new char [strlen(d.performers)+1];lable = new char [strlen(d.lable)+1];strcpy(performers, d.performers);strcpy(lable, d.lable);selections = d.selections;playtime = d.playtime;return * this;}void Cd::Report() const{cout << "Performers: " << performers << endl;cout << "Lable :" << lable << endl;cout << "Selections: " << selections << endl;cout << "Playtime: " << playtime << endl;}Classic::Classic(char * s3, char * s1, char * s2, int n, double x):Cd(s1,s2,n,x){works = new char [strlen(s3)+1];strcpy(works, s3);}Classic::Classic(char * s3, const Cd & d):Cd(d){works = new char [strlen(s3)+1];strcpy(works, s3);}Classic::Classic(const Classic & c):Cd(c){works = new char [strlen(c.works)+1];strcpy(works, c.works);}Classic::Classic():Cd(){works = new char [1];works[0] = '\0';}Classic & Classic::operator =(const Classic & c){if(this == &c)return *this;Cd::operator =(c);delete [] works;works = new char [strlen(c.works)+1];strcpy(works, c.works);}void Classic::Report() const{Cd::Report();cout << "Works: " << works <<endl;}


/*main*/#include <iostream>#include <Windows.h>#include "usersh.h"using std::cout;using std::cin;using std::endl;void Bravo(const Cd & disk);int main(){   Cd C1("Beat", "Captiol", 14, 35.5);Classic C2=Classic("Piano In B Flat, Fants in C ",                "Alfread Bren", "Phillips", 2, 57.17);Cd *pcd = &C1;cout << "Use obj dirc: \n";C1.Report();C2.Report();cout << "Use *pcd to obj:\n";pcd->Report();pcd = &C2;pcd->Report();cout << "Calling A function :\n";Bravo(C1);Bravo(C2);cout << "Testing :\n";Classic copy;copy = C2;copy.Report();system("pause");return 0;}void Bravo(const Cd & disk){disk.Report();}