64,675
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include<string>
#include<cstring>
const int SIZE = 20;
using namespace std;
class Cd
{
private:
char* performers;
char* label;
int selections; //曲目数
double playtime; //播放时间
public:
Cd(const char * s1, const char * s2, int n, double x);
~Cd(){};
virtual void Report() const; // 显示CD所有数据
};
Cd::Cd(const char * s1, const char * s2 , int n=0, double x=0.0)
{
performers = new char[SIZE];
label = new char[SIZE];
strcpy(performers,s1);
strcpy(label,s2);
selections = n;
playtime = x;
}
void Cd::Report() const
{
cout << performers << ", " << label << ", " << selections << ", " << playtime << endl;
}
/**************************************************************/
class Classic :public Cd
{
private:
char* symple;
public:
Classic(const char * s1, const char * s2, const char * s3, int n, double x);
virtual void Report() const;
char primarywork();
};
Classic::Classic(const char * s1 , const char * s2 , const char * s3, int n=0, double x=0.0):Cd(s1,s2,n,x)
{
symple=new char[SIZE];
strcpy(symple, s3);
}
void Classic::Report() const
{
this->Cd::Report();
cout<<symple<<endl;
}
char Classic::primarywork()
{
char message;
cout << "please input the message of the CD('q' is QUIT):" << endl;
cin >> message;
while (message =='q')
{
cout << message;
}
return message;
}
/**************************************************************/
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 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);
system("pause");
return 0;
}
void Bravo(const Cd & disk)
{
disk.Report();
}