C++ Primer Plus(第六版)第十三章课后编程答案

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值