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

原创 2015年11月21日 11:55:09

今天下午体测 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.name);
	return *this;
}



//第二题
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;
	char *label;
	int selections;
	double playtime;
public:
	Cd(char *s1, char *s2, int n, double x);
	Cd(const Cd &d);
	Cd() {};
	~Cd() { delete[] performers; delete[] label; };

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

class Classic : public Cd
{
private:
	char *name;
public:
	Classic(char *na, char *s1, char *s2, int n, double x) : Cd(s1, s2, n, x) { name = new char[std::strlen(na) + 1]; std::strcpy(name, na); }
	Classic(char *na, const Cd &d) : Cd(d) { name = new char[std::strlen(na) + 1]; std::strcpy(name, na); }
	Classic(const Classic &c);
	Classic() {}
	~Classic() { delete[] name; }

	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)
{
	performers = new char[std::strlen(s1) + 1];
	label = new char[std::strlen(s2) + 1];
	std::strcpy(performers, s1);
	std::strcpy(label, s2);
	selections = n;
	playtime = x;
}

Cd::Cd(const Cd & d)
{
	performers = new char[std::strlen(d.performers) + 1];
	label = new char[std::strlen(d.label) + 1];
	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;
	delete[] performers;
	delete[] label;
	performers = new char[std::strlen(d.performers) + 1];
	label = new char[std::strlen(d.label) + 1];
	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)
{
	name = new char[std::strlen(c.name) + 1];
	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);
	delete[] name;
	name = new char[std::strlen(c.name) + 1];
	std::strcpy(name, c.name);
	return *this;
}


//第三题
//main.cpp


#include "DMA.h"

const int LEN = 2;

int main()
{
	DMA *p[LEN];
	for (int i = 0; i < LEN; ++i)
	{
		int flag;
		std::cout << "输入要创建的对象,1:lacksDMA 2:hasDMA : ";
		std::cin >> flag;
		if (1 == flag)
		{
			lacksDMA l("Tree", 100, "BLACK");
			p[i] = &l;
			p[i]->View();
		}
		else
		{
			hasDMA h("Tree", 100, "OMG");
			p[i] = &h;
			p[i]->View();
		}
	}
	return 0;
}

//DMA.h
#define _CRT_SECURE_NO_WARNINGS
#ifndef DMA_H_
#define DMA_H_

#include <iostream>

class DMA
{
private:
	char *label;
	int rating;
public:
	DMA() { label = NULL; rating = 0; }
	DMA(const char *l, const int r);
	virtual ~DMA() { delete[] label; label = NULL; }

	char *GetLabel() const { return label; }
	int GetRating() const { return rating; }
	DMA &operator=(const DMA &rs);
	virtual void View() const = 0;
};

class baseDMA : public DMA
{
public:
	baseDMA() : DMA() {}
	baseDMA(const char *l, const int r) : DMA(l, r) {}

	baseDMA &operator=(const baseDMA &rs);
	virtual void View() const;
};

class lacksDMA : public DMA
{
private:
	char color[40];
public:
	lacksDMA() : DMA() { std::strcpy(color, "NULL"); }
	lacksDMA(const char *l, const int r, const char *c) : DMA(l, r) { std::strcpy(color, c); }

	lacksDMA &operator=(const lacksDMA &rs);
	virtual void View() const;
};

class hasDMA : public DMA
{
private:
	char *style;
public:
	hasDMA() : DMA() { style = NULL; }
	hasDMA(const char *l, const int r, const char *s);
	~hasDMA() { delete[] style; style = NULL; }

	hasDMA &operator=(const hasDMA &res);
	virtual void View() const;
};

#endif

//DMA.cpp
#include "DMA.h"

DMA::DMA(const char *l, const int r)
{
	label = new char[std::strlen(l) + 1];
	std::strcpy(label, l);
	rating = r;
}

DMA &DMA::operator=(const DMA &rs)
{
	if (this == &rs)
		return *this;
	delete[] label;
	label = new char[std::strlen(rs.label) + 1];
	std::strcpy(label, rs.label);
	rating = rs.rating;
	return *this;
}

baseDMA &baseDMA::operator=(const baseDMA &rs)
{
	if (this == &rs)
		return *this;
	DMA::operator=(rs);
	return *this;
}

void baseDMA::View() const
{
	std::cout << "label: " << GetLabel() << std::endl;
	std::cout << "rating: " << GetRating() << std::endl;
}

lacksDMA &lacksDMA::operator=(const lacksDMA &rs)
{
	if (this == &rs)
		return *this;
	DMA::operator=(rs);
	std::strcpy(color, rs.color);
	return *this;
}

void lacksDMA::View() const
{
	std::cout << "label: " << GetLabel() << std::endl;
	std::cout << "rating: " << GetRating() << std::endl;
	std::cout << "color: " << color << std::endl;
}

hasDMA::hasDMA(const char *l, const int r, const char *s) : DMA(l, r)
{
	style = new char[std::strlen(s) + 1];
	std::strcpy(style, s);
}

hasDMA &hasDMA::operator=(const hasDMA &rs)
{
	if (this == &rs)
		return *this;
	DMA::operator=(rs);
	delete[]style;
	style = new char[std::strlen(rs.style) + 1];
	std::strcpy(style, rs.style);
	return *this;
}

void hasDMA::View() const
{
	std::cout << "label" << GetLabel() << std::endl;
	std::cout << "rating: " << GetRating() << std::endl;
	std::cout << "style: " << style << std::endl;
}


//第四题
//main.cpp
#include "port.h"

int main()
{
	Port *ptr;
	Port p("Gallo", "tawny", 20);
	ptr = &p;
	ptr->Show();
	std::cout << p << std::endl;
	VintagePort v("Gallo", 20, "tttt", 20);
	ptr = &v;
	ptr->Show();
	std::cout << v << std::endl;
	return 0;
}

//port.h
#define _CRT_SECURE_NO_WARNINGS
#ifndef PORT_H_
#define PORT_H_

#include <iostream>

class Port
{
private:
	char *brand;
	char style[20];
	int bottles;
public:
	Port(const char *br = "none", const char *st = "none", int b = 0);
	Port(const Port &p);
	virtual ~Port() { delete[] brand; }
	Port &operator=(const Port &p);
	Port &operator+=(int b);
	Port &operator-=(int b);
	int BottleCount() const { return bottles; }
	virtual void Show() const;
	friend std::ostream &operator<<(std::ostream &os, const Port &p);
};

class VintagePort : public Port
{
private:
	char *nickname;
	int year;
public:
	VintagePort();
	VintagePort(const char *br, int b, const char *nn, int y);
	VintagePort(const VintagePort &vp);
	~VintagePort() { delete[] nickname; }
	VintagePort &operator=(const VintagePort &vp);
	void Show() const;
	friend std::ostream &operator<<(std::ostream &os, const VintagePort &vp);
};
#endif

//port.cpp
#include "port.h"

Port::Port(const char * br, const char * st, int b)
{
	brand = new char[std::strlen(br) + 1];
	std::strcpy(brand, br);
	std::strcpy(style, st);
	bottles = b;
}

Port::Port(const Port & p)
{
	brand = new char[std::strlen(p.brand) + 1];
	std::strcpy(brand, p.brand);
	std::strcpy(style, p.style);
	bottles = p.bottles;
}

Port & Port::operator=(const Port & p)
{
	if (this == &p)
		return *this;
	delete[] brand;
	brand = new char[std::strlen(p.brand) + 1];
	std::strcpy(brand, p.brand);
	std::strcpy(style, p.style);
	bottles = p.bottles;
	return *this;
}

Port & Port::operator+=(int b)
{
	bottles += b;
	return *this;
}

Port & Port::operator-=(int b)
{
	bottles -= b;
	return *this;
}

void Port::Show() const
{
	std::cout << "Brand: " << brand << std::endl;
	std::cout << "Kind: " << style << std::endl;
	std::cout << "Bottles: " << bottles << std::endl;
}

std::ostream &operator<<(std::ostream &os, const Port &p)
{
	os << p.brand << ", " << p.style << ", " << p.bottles;
	return os;
}

VintagePort::VintagePort() : Port()
{
	nickname = NULL;
	year = 0;
}

VintagePort::VintagePort(const char * br, int b, const char * nn, int y) : Port(br, "Vintage", b)
{
	nickname = new char[std::strlen(nn) + 1];
	std::strcpy(nickname, nn);
	year = y;
}

VintagePort::VintagePort(const VintagePort & vp) : Port(vp)
{
	nickname = new char[std::strlen(vp.nickname) + 1];
	std::strcpy(nickname, vp.nickname);
	year = vp.year;
}

VintagePort & VintagePort::operator=(const VintagePort & vp)
{
	if (this == &vp)
		return *this;
	delete[] nickname;
	Port::operator=(vp);
	nickname = new char[std::strlen(vp.nickname) + 1];
	std::strcpy(nickname, vp.nickname);
	year = vp.year;
	return *this;
}

void VintagePort::Show() const
{
	Port::Show();
	std::cout << "nick name: " << nickname << std::endl;
	std::cout << "year: " << year << std::endl;
}

std::ostream &operator<<(std::ostream &os, const VintagePort &vp)
{
	os << (Port &)vp;
	std::cout << ", " << vp.nickname << ", " << vp.year;
	return os;
}


相关文章推荐

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

c++ primer plus(第6版)中文版 第十三章编程练习答案

第十三章编程练习答案 13.1根据Cd基类,完成派生出一个Classic类,并测试 //13.1根据Cd基类,完成派生出一个Classic类,并测试 #include #include usi...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

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

//第一题 //main.cpp #include "TV.h" int main() { Tv s42; std::cout

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

C++primer plus第六版课后编程题答案12.6

myQueue,cpp不变 main126.cpp #include #include "myQueue.cpp" #include #include using namespace std...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C++ Primer Plus第六版 第十三章 编程练习答案
举报原因:
原因补充:

(最多只允许输入30个字)