1.
头文件:
#ifndef _HEAD_H
#define _HEAD_H
class Cd
{
private:
char performers[50];
char label[20];
int selections;
double playtime;
public:
Cd(char * s1,char *s2,int n, double p);
Cd(const Cd &d);
Cd();
virtual ~Cd();
virtual void Report()const;
Cd &operator=(const Cd &cd);
};
class Classic:public Cd
{
private:
char songs[100];
public:
Classic(char * s1,char *s2,int n, double p,char* sg);
Classic(const Classic &cl);
Classic();
~Classic();
virtual void Report()const;
Classic &operator=(const Classic &cl);
};
#endif
#include<iostream>
#include"stdio.h"
#include "head.h"
using namespace std;
Cd::Cd(char * s1,char *s2,int n, double p):selections(n),playtime(p)
{
strcpy(performers,s1);
strcpy(label,s2);
}
Cd::Cd(const Cd &d)
{
selections=d.selections;
playtime=d.playtime;
strcpy(performers,d.performers);
strcpy(label,d.label);
}
Cd::Cd()
{
selections=0;
playtime=0;
strcpy(performers,"null");
strcpy(label,"null");
}
Cd::~Cd()
{}
void Cd::Report()const
{
cout<<"performer: "<<performers<<endl<<"lable: "<<label<<endl;
}
Cd & Cd::operator=(const Cd &cd)
{
if(this==&cd)
return *this;
selections=cd.selections;
playtime=cd.playtime;
strcpy(performers,cd.performers);
strcpy(label,cd.label);
return *this;
}
类定义
Classic::Classic(char * s1,char *s2,int n, double p,char* sg):Cd(s1,s2,n,p)
{
strncpy(songs,sg,100);
}
Classic::Classic(const Classic &cl):Cd(cl)
{
strncpy(songs,cl.songs,100);
}
Classic::Classic()
{
Cd();
strcpy(songs,"null");
}
Classic::~Classic()
{}
void Classic::Report()const
{
Cd::Report();
cout<<"songs: "<<songs<<endl;
}
Classic &Classic::operator=(const Classic &cl)
{
if(this==&cl)
return *this;
Cd::operator=(cl);
strncpy(songs,cl.songs,100);
return *this;
}
主函数:
#include "stdio.h"
#include "head.h"
#include <iostream>
using namespace std;
int main()
{
Cd c1("Beatles", "Capitol", 14, 35.5);
Classic c2 = Classic( "Alfred Brendel", "Philips", 2, 57.17,"Piano Sonata in Bflat, Fantasia in C");
Cd *pcd = &c1;
std::cout << "Using object directly:\n";
c1.Report();
c2.Report();
std::cout << "Using type cd *pointer to object:\n";
pcd->Report();
pcd = &c2;
pcd->Report();
std::cout << "Testing assignment: \n";
Classic copy;
copy = c2;
copy.Report();
return 0;
}
2.
头文件:
#ifndef _HEAD_H
#define _HEAD_H
class Cd
{
private:
char *performers;
char *label;
int selections;
double playtime;
public:
Cd(char * s1,char *s2,int n, double p);
Cd(const Cd &d);
Cd();
virtual ~Cd();
virtual void Report()const;
Cd &operator=(const Cd &cd);
};
class Classic:public Cd
{
private:
char *songs;
public:
Classic(char * s1,char *s2,int n, double p,char* sg);
Classic(const Classic &cl);
Classic();
~Classic();
virtual void Report()const;
Classic &operator=(const Classic &cl);
};
#endif
类定义:
#include<iostream>
#include"stdio.h"
#include "head.h"
using namespace std;
Cd::Cd(char * s1,char *s2,int n, double p):selections(n),playtime(p)
{
performers=new char[strlen(s1)+1];
label=new char[strlen(s2)+1];
strcpy(performers,s1);
strcpy(label,s2);
}
Cd::Cd(const Cd &d)
{
selections=d.selections;
playtime=d.playtime;
performers=new char[strlen(d.performers)+1];
label=new char[strlen(d.label)+1];
strcpy(performers,d.performers);
strcpy(label,d.label);
}
Cd::Cd()
{
selections=0;
playtime=0;
performers=new char[+1];
label=new char[+1];
performers='\0';
label='\0';
}
Cd::~Cd()
{
delete [] performers;
delete []label;
}
void Cd::Report()const
{
cout<<"performer: "<<performers<<endl<<"lable: "<<label<<endl;
}
Cd & Cd::operator=(const Cd &cd)
{
if(this==&cd)
return *this;
selections=cd.selections;
playtime=cd.playtime;
delete [] performers;
delete[]label;
performers=new char[strlen(cd.performers)+1];
label=new char[strlen(cd.label)+1];
strcpy(performers,cd.performers);
strcpy(label,cd.label);
return *this;
}
Classic::Classic(char * s1,char *s2,int n, double p,char* sg):Cd(s1,s2,n,p)
{
songs=new char[strlen(sg)+1];
strcpy(songs,sg);
}
Classic::Classic(const Classic &cl):Cd(cl)
{
songs=new char[strlen(cl.songs)+1];
strcpy(songs,cl.songs);
}
Classic::Classic()
{
Cd();
songs=new char[1];
songs='\0';
}
Classic::~Classic()
{
delete []songs;
}
void Classic::Report()const
{
Cd::Report();
cout<<"songs: "<<songs<<endl;
}
Classic &Classic::operator=(const Classic &cl)
{
if(this==&cl)
return *this;
Cd::operator=(cl);
delete[]songs;
songs=new char[strlen(cl.songs)+1];
strcpy(songs,cl.songs);
return *this;
}