这是一个简单的继承的程序,能帮我调试一下程序吗,谢谢拉.(本人分数不多了,所以只能给10分)

aa1298 2004-11-16 11:04:27
#include<iostream>
#include<cstring>
using namespace std;
class cd
{
private:
char performers[50];
char label[20];
int selections;
double playtime;//10
public:
cd(char *sl,char *s2,int n,double x);
cd(const cd&d);
cd();
virtual ~cd(){};
virtual void report()const;
virtual cd&operator=(const cd&d);
};//18
class classic:public cd
{
private:
char zuopin[50];
public:
classic(char *s1,char *s2,char *s3,int n,double x);
classic(const classic&d);
classic();
virtual ~classic();
virtual void peport()const;
virtual classic&operator=(const classic&d);
};
cd::cd(char *s1,char *s2,int n,double x)
{
strncpy(performers,s1,49);//33
strncpy(label,s2,19);
selections=n;
playtime=x;
}
void cd::report()const//38
{ cout<<"performers="<<performers<<"\n";
cout<<"label="<<label<<"\n";
cout<<"selections="<<selections<<"\n";
cout<<"playtime"<<playtime<<"\n";}
cd&cd::operator=(const cd&d)
{
if(this==&d)
return *this;
delete []performers;//47
performers=new char[51];
strcpy(performers,d.performers);
delete []label;
label[]=new char[51];
strcpy(label,d.label);
selections=d.selections;
playtime=d.playtime;
return *this;
}
classic::classic(char *s1,char *s2,char *s3,int n,double x):cd(s1,s2,n,x)
{ strncpy(zuopin,s3,49);//58
} //59
void classic::report()const
{ cout<<"performers="<<s1<<"\n";
cout<<"label="<<s2<<"\n";
cout<<"zuopin="<<s3<<"\n"
cout<<"selections="<<n<<"\n";
cout<<"playtime"<<x<<"\n";}
classic&classic::operator=(const classic&d)
{if (this==&d)
return *this;
delete []performers;
performers=new char[51];
strcpy(performers,d.performers);
delete []label;
label=new char[51];//73
strcpy(label,d.label);
delete []zuopin;
zuopin=new char[51];
strcpy(zuopin,d.zuopin);
selections=d.selections;
playtime=d.playtime;
return *this;
}
void bravo(const cd&disk);//82
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 obhect directly:\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;//98
copy=c2;
copy.report();
return 0;
}
void bravo (const cd&disk)
{disk.report();}
...全文
172 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
runall 2004-11-21
  • 打赏
  • 举报
回复
#include<iostream>
#include<cstring>
using namespace std;
class cd
{
protected:
char performers[50];
char label[20];
int selections;
double playtime;//10
public:
cd(char *sl,char *s2,int n,double x);
cd(const cd&d);
cd(){}
virtual ~cd(){};
virtual void report()const;
virtual cd operator=(const cd&d);
};//18
class classic:public cd
{
private:
char zuopin[50];
public:
classic(char *s1,char *s2,char *s3,int n,double x);
classic(const classic&d){}
classic(){}
virtual ~classic(){}
virtual void report()const;
virtual classic operator=(const classic&d);
};
cd::cd(char *s1,char *s2,int n,double x)
{ strncpy(performers,s1,49);//32
strncpy(label,s2,19);
selections=n;
playtime=x;
}
cd::cd(const cd&d)
{ strcpy(performers,d.performers);//38
strcpy(label,d.label);
selections=d.selections;
playtime=d.playtime;
}
void cd::report()const//43
{ cout<<"performers="<<performers<<"\n";
cout<<"label="<<label<<"\n";
cout<<"selections="<<selections<<"\n";
cout<<"playtime"<<playtime<<"\n";}
cd cd::operator=(const cd&d)
{
cd aa;
strcpy(aa.performers,d.performers);
strcpy(aa.label,d.label);
aa.selections=d.selections;
aa.playtime=d.playtime;
return aa;
}
classic::classic(char *s1,char *s2,char *s3,int n,double x):cd(s1,s2,n,x)
{ strncpy(zuopin,s3,49);//58
} //59
void classic::report()const
{ cout<<"performers="<<performers<<"\n";
cout<<"label="<<label<<"\n";
cout<<"zuopin="<<zuopin<<"\n";
cout<<"selections="<<selections<<"\n";
cout<<"playtime"<<playtime<<"\n";}
classic classic::operator=(const classic&d)
{
classic aa;
strcpy(aa.performers,d.performers);//69
strcpy(aa.label,d.label);
strcpy(zuopin,d.zuopin);
aa.selections=d.selections;
aa.playtime=d.playtime;
return aa;
}
void bravo(const cd&disk);//66
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 obhect directly:\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;//98
copy=c2;
copy.report();
return 0;
}
void bravo (const cd&disk)
{disk.report();}


可以成功编译,但是不知运行结果如何,你自己试试看吧
runall 2004-11-21
  • 打赏
  • 举报
回复
子类构造函数首先需要调用父类的构造函数
如果你没有在子类的构造函数中调用父类的构造函数,编译器会调用父类的默认无参构造函数
而你的父类cd没有无参构造函数,所以你可以加上一个无参构造函数,或者修改子类的构造函数来显示调用父类的构造函数
runall 2004-11-21
  • 打赏
  • 举报
回复
把类cd的成员变量访问级别改为protected
aa1298 2004-11-19
  • 打赏
  • 举报
回复
我又改了一下,剩下的错误实在不知道怎么改了,帮小弟看看,多谢了!!!!

#include<iostream>
#include<cstring>
using namespace std;
class cd
{
private:
char performers[50];
char label[20];
int selections;
double playtime;//10
public:
cd(char *sl,char *s2,int n,double x);
cd(const cd&d);
cd();
virtual ~cd(){};
virtual void report()const;
virtual cd operator=(const cd&d);
};//18
class classic:public cd
{
private:
char zuopin[50];
public:
classic(char *s1,char *s2,char *s3,int n,double x);
classic(const classic&d);
classic();
virtual ~classic();
virtual void report()const;
virtual classic operator=(const classic&d);
};
cd::cd(char *s1,char *s2,int n,double x)
{ strncpy(performers,s1,49);//32
strncpy(label,s2,19);
selections=n;
playtime=x;
}
cd::cd(const cd&d)
{ strcpy(performers,d.performers);//38
strcpy(label,d.label);
selections=d.selections;
playtime=d.playtime;
}
void cd::report()const//43
{ cout<<"performers="<<performers<<"\n";
cout<<"label="<<label<<"\n";
cout<<"selections="<<selections<<"\n";
cout<<"playtime"<<playtime<<"\n";}
cd cd::operator=(const cd&d)
{
cd aa;
strcpy(aa.performers,d.performers);
strcpy(aa.label,d.label);
aa.selections=d.selections;
aa.playtime=d.playtime;
return aa;
}
classic::classic(char *s1,char *s2,char *s3,int n,double x):cd(s1,s2,n,x)
{ strncpy(zuopin,s3,49);//58
} //59
void classic::report()const
{ cout<<"performers="<<performers<<"\n";
cout<<"label="<<label<<"\n";
cout<<"zuopin="<<zuopin<<"\n"
cout<<"selections="<<selections<<"\n";
cout<<"playtime"<<playtime<<"\n";}
classic classic::operator=(const classic&d)
{
classic aa;
strcpy(aa.performers,d.performers);//69
strcpy(aa.label,d.label);
strcpy(zuopin,d.zuopin);
aa.selections=d.selections;
aa.playtime=d.playtime;
return aa;
}
void bravo(const cd&disk);//66
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 obhect directly:\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;//98
copy=c2;
copy.report();
return 0;
}
void bravo (const cd&disk)
{disk.report();}
yanry 2004-11-18
  • 打赏
  • 举报
回复


#include<iostream>
#include<cstring>
using namespace std;
class cd
{
public:
char performers[50];
char label[20];
int selections;
double playtime;//10
public:
cd(char *sl,char *s2,int n,double x);
cd(const cd&d);
cd();
virtual ~cd(){};
virtual void report()const;
virtual cd&operator=(const cd&d);
};//18
class classic:public cd
{
private:
char zuopin[50];
public:
classic(char *s1,char *s2,char *s3,int n,double x);
classic(const classic&d);
classic();
virtual ~classic();
virtual void peport()const;
virtual classic&operator=(const classic&d);
virtual void report()const;
};
cd::cd(char *s1,char *s2,int n,double x)
{ strncpy(performers,s1,49);//33
strncpy(label,s2,19);
selections=n;
playtime=x;
}
cd::cd(const cd&d)
{ strcpy(performers,d.performers);//33
strcpy(label,d.label);
selections=d.selections;
playtime=d.playtime;
}
void cd::report()const//38
{ cout<<"performers="<<performers<<"\n";
cout<<"label="<<label<<"\n";
cout<<"selections="<<selections<<"\n";
cout<<"playtime"<<playtime<<"\n";}
cd&cd::operator=(const cd&d)
{
cd aa;
strcpy(aa.performers,d.performers);
strcpy(aa.label,d.label);
aa.selections=d.selections;
aa.playtime=d.playtime;
return aa;
}
classic::classic(char *s1,char *s2,char *s3,int n,double x):cd(s1,s2,n,x)
{ strncpy(zuopin,s3,49);//50
} //51
void classic::report()const
{
/* cout<<"performers="<<s1<<"\n";
cout<<"label="<<s2<<"\n";
cout<<"zuopin="<<s3<<"\n"
cout<<"selections="<<n<<"\n";
cout<<"playtime"<<x<<"\n";*/
}
classic &classic::operator=(const classic&d)
{
classic aa;
strcpy(aa.performers,d.performers);//60
strcpy(aa.label,d.label);
strcpy(zuopin,d.zuopin);
aa.selections=d.selections;
aa.playtime=d.playtime;
return aa;
}
void bravo(const cd&disk);//66
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 obhect directly:\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;//98
copy=c2;
copy.report();
return 0;
}
void bravo (const cd&disk)
{disk.report();}
aa1298 2004-11-18
  • 打赏
  • 举报
回复
to: yanry()
你的程序我在vc6.0里编译不过啊
aa1298 2004-11-17
  • 打赏
  • 举报
回复
这是我改过的程序,有几个错误实在不知道怎么改,请大虾帮忙看看,多谢了!!!


#include<iostream>
#include<cstring>
using namespace std;
class cd
{
private:
char performers[50];
char label[20];
int selections;
double playtime;//10
public:
cd(char *sl,char *s2,int n,double x);
cd(const cd&d);
cd();
virtual ~cd(){};
virtual void report()const;
virtual cd&operator=(const cd&d);
};//18
class classic:public cd
{
private:
char zuopin[50];
public:
classic(char *s1,char *s2,char *s3,int n,double x);
classic(const classic&d);
classic();
virtual ~classic();
virtual void peport()const;
virtual classic&operator=(const classic&d);
};
cd::cd(char *s1,char *s2,int n,double x)
{ strncpy(performers,s1,49);//33
strncpy(label,s2,19);
selections=n;
playtime=x;
}
cd::cd(const cd&d)
{ strcpy(performers,d.performers);//33
strcpy(label,d.label);
selections=d.selections;
playtime=d.playtime;
}
void cd::report()const//38
{ cout<<"performers="<<performers<<"\n";
cout<<"label="<<label<<"\n";
cout<<"selections="<<selections<<"\n";
cout<<"playtime"<<playtime<<"\n";}
cd&cd::operator=(const cd&d)
{
cd aa;
strcpy(aa.performers,d.performers);
strcpy(aa.label,d.label);
aa.selections=d.selections;
aa.playtime=d.playtime;
return aa;
}
classic::classic(char *s1,char *s2,char *s3,int n,double x):cd(s1,s2,n,x)
{ strncpy(zuopin,s3,49);//50
} //51
void classic::report()const
{ cout<<"performers="<<s1<<"\n";
cout<<"label="<<s2<<"\n";
cout<<"zuopin="<<s3<<"\n"
cout<<"selections="<<n<<"\n";
cout<<"playtime"<<x<<"\n";}
classic&classic::operator=(const classic&d)
{
classic aa;
strcpy(aa.performers,d.performers);//60
strcpy(aa.label,d.label);
strcpy(zuopin,d.zuopin);
aa.selections=d.selections;
aa.playtime=d.playtime;
return aa;
}
void bravo(const cd&disk);//66
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 obhect directly:\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;//98
copy=c2;
copy.report();
return 0;
}
void bravo (const cd&disk)
{disk.report();}
aa1298 2004-11-17
  • 打赏
  • 举报
回复
我希望大虾们能给我一个完整的正确的程序,谢谢拉!!!!!!!
dot99 2004-11-16
  • 打赏
  • 举报
回复
delete []performers;
performer是个array,不能delete
同样label

我没看到你的classic::classic(); 的实体
也没看到cd::cd(const cd&);的实体

33,317

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧