c++ plus 13

来源:互联网 发布:redis 大量数据排序 编辑:程序博客网 时间:2024/06/15 04:55

以下只是我自己写的习题代码,可能会和期望的答案差很多,但是基本能满足习题所提出的要求:

#ifndef CLASSIC_H_
#define CLASSIC_H_
class Cd
{
private:
 char *performers;
 char *label;
 int selections;
 double playtime;
public:
 Cd();
 Cd(char * s1,char *s2,int n,double x);
 Cd(const Cd &);
 Cd &operator = (const Cd &c);
 virtual ~Cd();
 virtual void Report()const;
};
class Classic:public Cd
{
private:
 char *main1;
public:
 Classic();
 Classic(char *m,char * s1,char *s2,int n,double x);
 Classic(const Classic &);
 Classic &operator=(const Classic &c);
 virtual ~Classic();
 virtual void Report()const;
};
#endif
************************************************************************************************
#include "classic2.h"
#include <iostream>
using namespace std;
Cd::Cd(char *s1, char *s2, int n, double x)
{
 int len=0;
 len=strlen(s1)+1;
 performers=new char[len];
 strcpy(performers,s1);
 len=strlen(s2)+1;
 label= new char [len];
 strcpy(label,s2);
 selections=n;
 playtime=x;
};
Cd::Cd()
{
 performers=new char [1];
 performers[0]='0';
 label=new char [1];
 label[0]='/0';
 selections=0;
 playtime=0;
};
Cd::Cd(const Cd &c)
{
 performers=new char [strlen(c.performers)+1];
 strcpy(performers,c.performers);
 label=new char [strlen(c.label)+1];
 strcpy(label,c.performers);
 selections=c.selections;
 playtime=c.playtime;
}
Cd &Cd::operator = (const Cd &c)
{
 if(&c==this)
  return *this;
 delete [] performers;
 delete [] label;
 performers=new char [strlen(c.performers)+1];
 strcpy(performers,c.performers);
 label=new char [strlen(c.label)+1];
 strcpy(label,c.label);
 selections=c.selections;
 playtime=c.playtime;
 return *this;
}
void Cd::Report()const
{
 cout<<"Performers: "<<performers<<endl;
 cout<<"Label: "<<label<<endl;
 cout<<"selections: "<<selections<<endl;
 cout<<"playtime: "<<playtime<<endl;
};
Cd::~Cd()
{
 delete [] performers;
 delete [] label;
}
Classic::Classic():Cd()
{
 main1=new char [1];
 main1[0]='/0';
}
Classic::Classic(char *m, char *s1, char *s2, int n, double x):Cd(s1,s2,n,x)
{
 main1=new char [strlen(m)+1];
 strcpy(main1,m);
};
Classic::Classic(const Classic &a):Cd(a)
{
 main1=new char [strlen(a.main1)+1];
 strcpy(main1,a.main1);
}
Classic &Classic::operator=(const Classic &c)
{
 if(&c==this)
  return *this;
 Cd::operator =(c);
 delete [] main1;
 main1=new char [strlen(c.main1)+1];
 strcpy(main1,c.main1);
 return *this;
};
void Classic::Report()const
{
 cout<<"MainName: "<<main1<<endl;
 Cd::Report();
};

Classic::~Classic()
{
 delete [] main1;
}
*******************************************************************************
#include <iostream>
#include "classic2.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;
 cout<<"Using object dirctly: /n";
 c1.Report();
 c2.Report();
 cout<<"Using type cd * pointer to objects: /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.Report();
 system("pause");
 return 0;
}
void Bravo(const Cd & disk)
{
 disk.Report();
}

 

如有错误,希望路过的朋友多多指点啊,并且很乐意结交正在学习C++的朋友,大家一起共同努力,谢谢!

原创粉丝点击