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

今天下午体测 GGGGGGGGGGGG


//第一题
//main.cpp
#include "cd.h"

void Bravo(const Cd &disk);

int main()
{
	Cd c1("Beatles", "Capitol", 14, 35.5);
	Classic c2 = Classic("Piano Sonata in Bflat, Fantasia in C", "Alfred Brendel", "Philips", 2, 57.17);
	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;
}

void Bravo(const Cd &disk)
{
	disk.Report();
}

//cd.h
#ifndef CD_H_
#define CD_H_

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

class Cd
{
private:
	char performers[50];
	char label[20];
	int selections;
	double playtime;
public:
	Cd(char *s1, char *s2, int n, double x);
	Cd(const Cd &d);
	Cd() {};
	~Cd() {};

	virtual void Report() const;
	Cd &operator=(const Cd &d);
};

class Classic : public Cd
{
private:
	char name[100];
public:
	Classic(char *na, char *s1, char *s2, int n, double x) : Cd(s1, s2, n, x) { std::strcpy(name, na); }
	Classic(char *na, const Cd &d) : Cd(d) { std::strcpy(name, na); }
	Classic(const Classic &c);
	Classic() {}

	virtual void Report() const;
	Classic &operator=(const Classic &c);
};

#endif

//cd.cpp
#include "cd.h"

Cd::Cd(char * s1, char * s2, int n, double x)
{
	std::strcpy(performers, s1);
	std::strcpy(label, s2);
	selections = n;
	playtime = x;
}

Cd::Cd(const Cd & d)
{
	std::strcpy(performers, d.performers);
	std::strcpy(label, d.label);
	selections = d.selections;
	playtime = d.playtime;
}

void Cd::Report() const
{
	std::cout << "performers: " << performers << std::endl;
	std::cout << "label: " << label << std::endl;
	std::cout << "selections: " << selections << std::endl;
	std::cout << "playtime: " << playtime << std::endl;
}

Cd & Cd::operator=(const Cd & d)
{
	if (this == &d)
		return *this;
	std::strcpy(performers, d.performers);
	std::strcpy(label, d.label);
	selections = d.selections;
	playtime = d.playtime;
	return *this;
}

Classic::Classic(const Classic & c) : Cd(c)
{
	std::strcpy(name, c.name);
}

void Classic::Report() const
{
	std::cout << "name: " << name << std::endl;
	Cd::Report();
}

Classic & Classic::operator=(const Classic & c)
{
	if (this == &c)
		return *this;
	Cd::operator=(c);
	std::strcpy(name, c.na
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值