12.1 以下面的类声明为基础:
头文件
//
// classic.h
// classic
//
// Created by apple on 2018/8/15.
// Copyright © 2018年 apple. All rights reserved.
//
#ifndef classic_h
#define classic_h
class Cd{
private:
char performers[50];
char label[20];
int selections; //number of selections
double playtime;
public:
Cd(char *s1, char* s2, int n, double x);
Cd(const Cd &d);
Cd();
~Cd();
virtual void Report() const;//reports all CD data
Cd & operator=(const Cd &d);
};
class Classic:public Cd{
public:
Classic(char *s1, char* s2, char *s3, int select, int play);
Classic();
~Classic();
virtual void Report() const;
Classic& operator = (const Classic &c);
private:
char name[50];
};
#endif /* classic_h */
头文件实现文件
//
// classic.cpp
// classic
//
// Created by apple on 2018/8/15.
// Copyright © 2018年 apple. All rights reserved.
//
#include <iostream>
#include "classic.h"
using namespace std;
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(){
}
Cd::~Cd(){
}
void Cd::Report() const{
cout<<"in CD class, Performers = "<<performers<<" label= "<<label<<" ,selection = "<<selections<<" ,playtime = "<<playtime<<endl;
}
Cd & Cd::operator=(const Cd &d){
if(this == &d)
return *this;
strcpy(performers, d.performers);
strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
return *this;
}
Classic::Classic(){
}
Classic::~Classic(){
}
Classic::Classic(char *s1, char* s2, char *s3, int select, int play):Cd(s1,s2,select,play){
strcpy(name, s3);
}
void Classic::Report() const{
Cd::Report();
cout<<"name = "<<name<<endl;
}
Classic& Classic::operator = (const Classic &c){
if(this == &c)
return *this;
Cd::operator=(c);
strcpy(name, c.name);
return *this;
}
测试文件
//
// main.cpp
// classic
//
// Created by apple on 2018/8/15.
// Copyright © 2018年 apple. All rights reserved.
//
#include <iostream>
#include "classic.h"
using namespace std;
void Bravo(const Cd& disk);
int main(int argc, const char * argv[]) {
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);
cout<<"Testing assignment: ";
Classic copy;
copy = c2;
copy.Report();
return 0;
}
void Bravo(const Cd & disk){
disk.Report();
}
2.完成练习1,但让两个类使用动态内存分配而不是长度固定的数组来记录字符串
头文件
//
// classic.h
// classic
//
// Created by apple on 2018/8/15.
// Copyright © 2018年 apple. All rights reserved.
//
#ifndef classic_h
#define classic_h
class Cd{
private:
// char performers[50];
char* performers;
// char label[20];
char* label;
int selections; //number of selections
double playtime;
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{
public:
Classic(char *s1, char* s2, char *s3, int select, int play);
Classic();
virtual ~Classic();
virtual void Report() const;
Classic& operator = (const Classic &c);
private:
// char name[50];
char* name;
};
#endif /* classic_h */
头文件实现文件
//
// classic.cpp
// classic
//
// Created by apple on 2018/8/15.
// Copyright © 2018年 apple. All rights reserved.
//
#include <iostream>
#include "classic.h"
using namespace std;
Cd::Cd(char *s1, char* s2, int n, double x){
int len1 = strlen(s1);
performers = new char[len1+1];
strcpy(performers, s1);
int len2 = strlen(s2);
label = new char[len2+1];
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(){
}
Cd::~Cd(){
cout<<"delete Cd\n";
delete []performers;
delete []label;
}
void Cd::Report() const{
cout<<"in CD class, Performers = "<<performers<<" label= "<<label<<" ,selection = "<<selections<<" ,playtime = "<<playtime<<endl;
}
Cd & Cd::operator=(const Cd &d){
if(this == &d)
return *this;
performers = new char[strlen(d.performers)+1];
strcpy(performers, d.performers);
label = new char[strlen(d.label)+1];
strcpy(label, d.label);
selections = d.selections;
playtime = d.playtime;
return *this;
}
Classic::Classic(){
}
Classic::~Classic(){
cout<<"Delete Classic\n";
delete []name;
}
Classic::Classic(char *s1, char* s2, char *s3, int select, int play):Cd(s1,s2,select,play){
int len = strlen(s3);
name = new char[len+1];
strcpy(name, s3);
}
void Classic::Report() const{
Cd::Report();
cout<<"name = "<<name<<endl;
}
Classic& Classic::operator = (const Classic &c){
if(this == &c)
return *this;
Cd::operator=(c);
name = new char[strlen(c.name)+1];
strcpy(name, c.name);
return *this;
}
测试文件
//
// main.cpp
// classic
//
// Created by apple on 2018/8/15.
// Copyright © 2018年 apple. All rights reserved.
//
#include <iostream>
#include "classic.h"
using namespace std;
void Bravo(const Cd& disk);
int main(int argc, const char * argv[]) {
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);
cout<<"Testing assignment: ";
Classic copy;
copy = c2;
copy.Report();
return 0;
}
void Bravo(const Cd & disk){
disk.Report();
}