1.
#ifndef TABTENN0_H_
#define TABTENN0_H_
#include<iostream>
//基类
class Cd
{
private:
char performers[50];
char label[20];
int selections; //number of selections
double playtime; //palying time in minutes
public:
Cd(char *s1, char *s2, int n, double x);
Cd(const Cd & d);
Cd();
virtual ~Cd();
virtual void Report() const; //reports all CD data
Cd & operator=(const Cd & d);
};
//派生类
class Classic :public Cd
{
private:
char name[20];
public:
Classic(char * s1, char * s2, char * s3, int n, double x);
Classic(const Classic & d, char * c);
Classic(const Classic & d);
Classic();
virtual void Report() const;
Classic & operator=(const Classic & d);
};
#endif
#include"tabtenn0.h"
#include<cstring>
using std::cout;
using std::endl;
//基类实现方法
Cd::Cd(char *s1, char *s2, int n, double x)
{
strcpy_s(performers, s1);
strcpy_s(label, s2);
selections = n;
playtime = x;
}
Cd::Cd(const Cd & d)
{
strcpy_s(performers, d.performers);
strcpy_s(label, d.label);
selections = d.selections;
playtime = d.playtime;
}
Cd::Cd()
{
performers[0] = '\0';
label[0] = '\0';
selections = 0;
playtime = 0;
}
Cd::~Cd()
{
}
void Cd::Report() const
{
cout << "performers:" << performers << endl;
cout << "label:" << label << endl;
cout << "number of selections:" << selections << endl;
cout << "playing time in minutes:" << playtime << endl;
cout << endl;
}
Cd & Cd::operator=(const Cd & d)
{
if (this == &d)
return *this;
strcpy_s(performers, d.performers);
strcpy_s(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) //初始化成员列表
{
strcpy_s(name, s3);
}
Classic::Classic(const Classic & d, char * c):Cd(d)
{
strcpy_s(name, c);
}
Classic::Classic(const Classic & d)
{
}
Classic::Classic()
{
}
void Classic::Report() const
{
cout << "name:" << name << endl;
Cd::Report();
}
Classic & Classic::operator=(const Classic & d)
{
if (this == &d)
return *this;
Cd::operator=(d);
strcpy_s(name, d.name);
return *this;
}
#include<iostream>
#include"tabtenn0.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 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();
}
2.动态内存分配,使用new分配内存资源,析构函数使用delete删除指针
#ifndef TABTENN0_H_
#define TABTENN0_H_
#include<iostream>
//基类
class Cd
{
private:
char * performers;
char * label;
int selections; //number of selections
double playtime; //palying time in minutes
public:
Cd(char *s1, char *s2, int n, double x);
Cd(const C