概述
1.以下面的类声明为基础:
// base class
class Cd { //represents a CD disk
private:
char performers[50];
char label[20];
int selections; //number of selections
double playtime; //playing time in minute
public:
Cd(char * s1, char * s2, int n, double x);
Cd(const Cd & d);
Cd();
~Cd();
void Report() const; //reports all CD data
Cd & operator=(const Cd & d);
};
派生出一个Classic类,并添加一组char成员,用于存储指出CD中主要作品的字符串。修改上述声明,使基类的搜有函数都是虚的。如果上述定义声明的某个方法并不需要,则请删除它。使用下面的程序测试您的产品:
#include<iostream>
using namespace std;
#include"classic.h" //which will contain #include cd.h
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(); //use Cd method
c2.Report(); //use Classic method
cout<<"Using type cd *pointer to objects:n";
pcd->Report(); //use Cd method for cd object
pcd = &c2;
pcd->Report(); //use Classic method for classic object
cout<<"Calling a function with a Cd reference argument:n";
Bravo(c1);
Bravo(c2);
cout<<"Testing assignment: ";
Classic copy;
copy = c2;
copy.Report();
return 0;
}
void Bravo(const Cd& disk)
{
disk.Report();
}
//cd.h
#ifndef CD_H_INCLUDED
#define CD_H_INCLUDED
class Cd
{
private:
char performers[50];
char label[20];
int selections;
double playtime;
public:
Cd ( char *s1, char *s2, int n, double x );
Cd ( const Cd & d );
Cd();
virtual ~Cd();
virtual void Report() const;
virtual Cd & operator= ( const Cd & d );
};
class Classic: public Cd
{
private:
char opus[100];
public:
Classic();
Classic ( const Classic & c );
Classic ( char * o, char *s1, char *s2, int n, double x );
~Classic();
virtual void Report() const;
virtual Cd & operator= ( const Classic & c );
};
#endif // CD_H_INCLUDED
//cd.cpp
#include <iostream>
#include<cstring>
#include"cd.h"
using std::strcpy;
using std::cout;
using std::cin;
using std::endl;
Cd::Cd ( char *s1, char *s2, int n, double x )
{
strcpy ( performers, s1 );
strcpy ( label, s2 );
selections = n;
playtime = x;
}
Cd::Cd ( const Cd & d )
{
strcpy ( performers, d.performers );
strcpy ( label, d.label );
selections = d.selections;
playtime = d.playtime;
}
Cd::Cd()
{
strcpy ( performers, "NULL" );
strcpy ( label, "NULL" );
selections = 0;
playtime = 0;
}
Cd::~Cd() {}
void Cd::Report() const
{
cout << "performers: " << performers << endl;
cout << "label: " << label << endl;
cout << "selections: " << selections << endl;
cout << "playtime: " << playtime << endl;
}
Cd & Cd::operator= ( const Cd & d )
{
strcpy ( performers, d.performers );
strcpy ( label, d.label );
selections = d.selections;
playtime = d.playtime;
return *this;
}
Classic::Classic() : Cd()
{
strcpy ( opus, "NULL" );
}
Classic::Classic ( const Classic & c ) : Cd ( c )
{
strcpy ( opus, c.opus );
}
Classic::Classic ( char * o, char *s1, char *s2, int n, double x ) : Cd ( s1, s2, n, x )
{
strcpy ( opus, o );
}
Classic::~Classic() {}
void Classic::Report() const
{
Cd::Report();
cout << "opus: " << opus << endl;
}
Cd & Classic::operator= ( const Classic & c )
{
if ( this == &c )
return *this;
Cd::operator= ( c );
strcpy ( opus, c.opus );
return *this;
}
//main.cpp
#include <iostream>
#include"cd.cpp"
#include"cd.h"
void Bravo ( const Cd& disk );
int main()
{
using namespace std;
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(); //use Cd method
cout << endl;
c2.Report(); //use Classic method
cout << endl;
cout << "Using type cd *pointer to objects:n";
pcd->Report(); //use Cd method for cd object
cout << endl;
pcd = &c2;
pcd->Report(); //use Classic method for classic object
cout << endl;
cout << "Calling a function with a Cd reference argument:n";
Bravo ( c1 );
cout << endl;
Bravo ( c2 );
cout << endl;
cout << "Testing assignment: ";
Classic copy;
copy = c2;
copy.Report();
cout << endl;
system ( "pause" );
return 0;
}
void Bravo ( const Cd& disk )
{
disk.Report();
}
2.完成练习1,但让两个类使用动态内存分配而不是长度固定的数组来记录字符串。
#ifndef CD_H_INCLUDED
#define CD_H_INCLUDED
class Cd
{
private:
char *performers;
char *label;
int selections;
double playtime;
public:
Cd ( const char *s1, const char *s2, int n, double x );
Cd ( const Cd & d );
Cd();
virtual ~Cd();
virtual void Report() const;
virtual Cd & operator= ( const Cd & d );
};
class Classic: public Cd
{
private:
char *opus;
public:
Classic();
Classic ( const Classic & c );
Classic ( const char * o, const char *s1, const char *s2, int n, double x );
~Classic();
virtual void Report() const;
virtual Cd & operator= ( const Classic & c );
};
#endif // CD_H_INCLUDED
#include <iostream>
#include<cstring>
#include"cd.h"
using std::strcpy;
using std::strlen;
using std::cout;
using std::cin;
using std::endl;
Cd::Cd ( const char *s1, const char *s2, int n, double x )
{
performers=new char[strlen(s1)+1];
label=new char[strlen(s2)+1];
strcpy ( performers, s1 );
strcpy ( label, s2 );
selections = n;
playtime = x;
}
Cd::Cd ( const Cd & d )
{
performers=new char[strlen(d.performers)+1];
label=new char[strlen(d.label)+1];
strcpy ( performers, d.performers );
strcpy ( label, d.label );
selections = d.selections;
playtime = d.playtime;
}
Cd::Cd()
{
performers=new char[1];
label=new char[1];
performers[0]='