Cd2.cpp
#include <iostream> #include <cctype> using namespace std; class Cd{ private: char *performers; char *label; int selections; double playtime; public: char *perfor(){return performers;}; char *lab(){return label;}; int sel(){return selections;}; double play(){return playtime;}; //为了Classic的= void setPer(char *p){ int len=strlen(p); delete[]performers;//要先释放原来的 performers=new char[len+1]; strcpy(performers,p); }; void setlab(char *l){ int len=strlen(l); delete[]label; label=new char[len+1]; strcpy(label,l); }; void setsel(int num){selections=num;}; void setplay(double s){playtime=s;}; // Cd(char *s1,char *s2,int n,double x) { int lens1=strlen(s1); int lens2=strlen(s2); performers=new char[lens1+1]; strcpy(performers,s1); label=new char[lens2+1]; strcpy(label,s2); selections=n; playtime=x; }/* Cd(const Cd &d) { strcpy(performers,d.performers); strcpy(label,d.label); selections=d.selections; playtime=d.playtime; }*/ Cd(){} virtual ~Cd(){ delete[]performers; delete[]label; }//构造函数应为虚函数 virtual void Report()const { cout<<"performers:"<<performers<<endl; cout<<"label:"<<label<<endl; cout<<"selection:"<<selections<<" playtime:"<<playtime<<endl; //cout<<endl; } virtual Cd&operator=(const Cd &d) { return Cd(d); } }; class Classic:public Cd { private: char *mainLabel; public: Classic(char *s1,char *s2,char *s3,int n,double x):Cd(s1,s2,n,x) { int len=strlen(s3); mainLabel=new char[len+1]; strcpy(mainLabel,s3); } Classic(Classic &c):Cd(c.perfor(),c.lab(),c.sel(),c.play())//不能使用const { int len=strlen(c.mainLabel); mainLabel=new char[len+1]; strcpy(mainLabel,c.mainLabel); } Classic(){ } virtual ~Classic(){ delete[]mainLabel; } virtual void Report()const { Cd::Report();//原来是这样子调用父类的方法的啊 cout<<"mainLabel:"<<mainLabel; cout<<endl; } /* Classic &operator=(Classic &c)//不能使用const { return Classic(c); }*/ void operator=(Classic &c)//不能使用const { //strcpy(performers,c.perfor()); Cd::setPer(c.perfor()); Cd::setlab(c.lab()); Cd::setsel(c.sel()); Cd::setplay(c.play()); //strcpy(mainLabel,c.mainLabel); int len=strlen(c.mainLabel); delete []mainLabel; mainLabel=new char[len+1]; strcpy(mainLabel,c.mainLabel); } };
main132.cpp
#include <iostream> #include "Cd2.cpp" using namespace std; void Bravo(const Cd &d) { d.Report(); }; void main132() { Cd c1("Beates","Capitol",14,35.5); Classic c2=Classic("Piano Sonata in B flag,Fantasia in C","Alfred Brendel","Philips",2,59.19); Cd *pcd=&c1; cout<<"Using object directly:"<<endl; c1.Report(); cout<<endl; c2.Report(); cout<<"Using type cd * pointer to object:"<<endl; pcd->Report(); cout<<endl; pcd=&c2;//重定位pcd pcd->Report(); cout<<"Calling a funciton with a Cd reference argument:"<<endl; Bravo(c1); Bravo(c2); cout<<"Testing assignment:"<<endl; //Classic copy=c2;//这样也是可以的 //Classic copy(c2);//这样就没问题,那就是赋值出了问题 Classic copy; //因为这里已经初始化了,所以copy已经是利用Classic()初始化了 copy=c2;//这里有问题、//这样的话,这样赋值就不能返回一个Classic的值了 copy.Report(); cin.get(); }
——————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:https://www.royalchen.com/
author:royalchen
Email:royalchen@royalchen.com
———————————————————————————————————————————————————