C++ Primer Plus第五版 第十三章 编程练习答案

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值