C++ PrimerPlus第六版第十三章编程练习答案

本文提供了C++ PrimerPlus第六版第十三章的编程练习解答,包括cd.h、main.cpp、DAM.h等文件的内容。讨论了类继承中哪些方法需要重定义,哪些不需要,以及为什么赋值运算符不声明为虚函数。
摘要由CSDN通过智能技术生成

1.

cd.h

#ifndef cd_H_
#define cd_H_
#include<string>
#include <iostream>
#include <cstring>
using std::cout;
class Cd {
   
private:
	char performers[50];
	char label[20];
	int selections;
	double playtime;
public:
	Cd(const char * s1, const char * s2, int n, double x);
	Cd() {
   }
	virtual~Cd() {
   }
	virtual void Report() const;
};
class Classic :public Cd
{
   
private:
	char works[50];
public:
	Classic(const char * s1, const char *s2, const char * s3, int n, double x);
	Classic() :Cd() {
   }
	virtual void Report() const;
};

Cd::Cd(const char * s1, const char * s2, int n, double x)
{
   
	strncpy_s(performers, s1, 50);
	performers[49] = '\0';
	strncpy_s(label, s2, 20);
	label[19] = '\0';
	selections = n;
	playtime = x;
}
void Cd::Report() const
{
   
	cout << "performers: " << performers << '\n';
	cout << "label: " << label << '\n';
	cout << "selections: " << selections << '\n';
	cout << "playtime: " << playtime << '\n';
}
Classic::Classic(const char * s1, const char *s2, const char * s3, int n, double x) :Cd(s2, s3, n, x)
{
   
	strncpy_s(works, s1, 50);
	works[49] = '\0';
}
void Classic::Report() const
{
   
	cout << "works: " << works << '\n';
	Cd::Report();
}
#endif

main.cpp

#include "cd.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();
	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.

main.cpp和上一题一样的

#ifndef cd_H_
#define cd_H_
#include <iostream>
#include<string>
using std::cout;
class Cd {
   
private:
	char *performers;
	char *label;
	int selections;
	double playtime;
public:
	Cd(const char * s1, const char * s2, int n, double x);
	Cd();
	virtual~Cd();
	virtual void Report() const;
	Cd & operator=(const Cd & d);
};
class Classic :public Cd
{
   
private:
	char *works;
public:
	Classic(const char * s1, const char *s2, const char * s3, int n, double x);
	Classic();
	~Classic();
	virtual void Report() const;
	Classic & operator=(const Classic & c);
};
Cd::Cd()
{
   
	performers = nullptr;
	label = nullptr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值