C++Primer Plus第6版第14章编程练习答案

第一题

Pair.h

#ifndef PAIR_H_
#define PAIR_H_
#include"Wine.h"
template<class T1, class T2>
class Pair
{
private:
	T1 a;
	T2 b;
public:
	T1& first();
	T2& second();
	T1 first()const { return a; }
	T2 second()const { return b; }
	Pair(const T1& aval,const T2& bval) : a(aval), b(bval) { }
	Pair();
};

template<class T1, class T2>
T1& Pair<T1, T2>::first()
{
	return a;
}

template<class T1, class T2>
T2& Pair<T1, T2>::second()
{
	return b;
}
#endif

 Wine.h

#ifndef WINE_H_
#define WINE_H_
#include<string>
#include"Pair.h"
#include<valarray>

typedef std::valarray<int>ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;

class Wine
{
private:
	std::string wine_name;
	int number_years;
	PairArray number_of_wine_vantages;
public:
	//可能会有写人写成这样number_of_wine_vantages(yr,y,bot,y)这样的话就会变成
	//将yr类型赋值给ArrayInt 实例 ArrayInt ar = yr;不可直接将yr赋值给ar 这样写可以ArrayInt ar(yr,3)这样就ok了
	Wine(const char* l, int y, const int yr[], const int bot[]) : wine_name(l), 
		number_years(y),number_of_wine_vantages(ArrayInt(yr,y),ArrayInt(bot,y)) { }
	Wine(const char* l, int y) : wine_name(l),
		number_years(y),number_of_wine_vantages(ArrayInt(y),ArrayInt(y)) { }
	void GetBottles();
	void show()const;
	const std::string& Label()const;
	int sum()const;
};

#endif

 wine.cpp

#include"Wine.h"
#include<iostream>
void Wine::show()const
{
	std::cout << "Wine: " << wine_name << std::endl;
	std::cout << "\tyear " << "\tbottles" << std::endl;

	//std::cout << "show pair_a: ";
	//范围for
	//for (auto i : number_of_wine_vantages.first())
	//	std::cout << i << " ";
	//std::cout << std::endl;
	//std::cout << "show pair_b: ";
	///*for (auto i : number_of_wine_vantages.second())
	//	std::cout << i << " ";
	//std::cout << std::endl;*/

	for (int i = 0; i < number_of_wine_vantages.first().size(); i++)
	{
		std::cout << "\t" << number_of_wine_vantages.first()[i];
		std::cout << "\t" << number_of_wine_vantages.second()[i];
		std::cout << std::endl;
	}
}

int Wine::sum()const
{
	return number_of_wine_vantages.second().sum();
}

void Wine::GetBottles()
{
	std::cout << "Enter " << wine_name << " data for "
			  << number_years << "year(s):\n";
	for (int i = 0; i < number_years; i++)
	{
		std::cout << "Enter year: ";
		std::cin >> number_of_wine_vantages.first()[i];
		std::cout << "Enter bottles for that year: ";
		std::cin >> number_of_wine_vantages.second()[i];
	}
}

const std::string& Wine::Label()const
{
	return wine_name;
}

main.cpp

#include"Wine.h"
#include<iostream>
int main()
{
	using std::cin;
	using std::cout;
	using std::endl;

	cout << "Enter name of wine: ";
	char lab[50];
	cin.getline(lab, 50);
	cout << "Enter number of years: ";
	int yrs;
	cin >> yrs;
	Wine holding(lab, yrs);
	holding.GetBottles();
	holding.show();

	const int YRS = 3;
	int y[YRS] = { 1993,1995,1998 };
	int b[YRS] = { 48,60,72 };

	Wine more("Gushing Grape Red", YRS, y, b);
	more.show();
	cout << "Total bottles for " << more.Label()
		 << ": " << more.sum() << std::endl;
	cout << "Bye\n";
	return 0;
}

输出结果

 第二题

Pair.h

#ifndef PAIR_H_
#define PAIR_H_
#include"Winec.h"
template<class T1, class T2>
class Pair
{
private:
	T1 a;
	T2 b;
public:
	T1& first();
	T2& second();
	T1 first()const { return a; }
	T2 second()const { return b; }
	Pair(const T1& aval, const T2& bval) : a(aval), b(bval) { }
	Pair() : a(),b() { }
	//Pair<T1,T2>& operator=(const Pair<T1,T2>& p)
	//{
	//	a = p.first();
	//	b = p.second();
	//	return*this;
	//}
};

template<class T1, class T2>
T1& Pair<T1, T2>::first()
{
	return a;
}

template<class T1, class T2>
T2& Pair<T1, T2>::second()
{
	return b;
}
#endif

 Wine.h

#ifndef WINE_H_
#define WINE_H_
#include<string>
#include"Pair.h"
#include<valarray>

typedef std::valarray<int>ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;

class Wine : private PairArray,std::string
{
private:
	int number_years;
public:
	Wine(const char* l, int y, const int yr[], const int bot[]) : 
		number_years(y),std::string(l),PairArray(ArrayInt(yr,y),ArrayInt(bot,y)) { }
	Wine(const char* l, int y) :
		std::string(l),number_years(y),PairArray(ArrayInt(y),ArrayInt(y)) { }
	void GetBottles();
	void show()const;
	const std::string& Label()const;
	int sum()const;
};

#endif

wine.cpp

#include"Winec.h"
#include<iostream>
void Wine::show()const
{
	std::cout << "Wine: " << (const std::string&)(*this) << std::endl;
	std::cout << "\tyear " << "\tbottles" << std::endl;

	for (int i = 0; i < PairArray::first().size(); i++)
	{
		std::cout << "\t" << PairArray::first()[i];
		std::cout << "\t" << PairArray::second()[i];
		std::cout << std::endl;
	}
}

int Wine::sum()const
{
	return PairArray::second().sum();
}

void Wine::GetBottles()
{
	std::cout << "Enter " << (const std::string&)(*this) << " data for "
			  << number_years << "year(s):\n";
	for (int i = 0; i < number_years; i++)
	{
		std::cout << "Enter year: ";
		std::cin >> PairArray::first()[i];
		std::cout << "Enter bottles for that year: ";
		std::cin >> PairArray::second()[i];
	}
}

const std::string& Wine::Label()const
{
	return (const std::string&)(*this);
}

main.cpp

#include"Winec.h"
#include<iostream>
int main()
{
	using std::cin;
	using std::cout;
	using std::endl;

	cout << "Enter name of wine: ";
	char lab[50];
	cin.getline(lab, 50);
	cout << "Enter number of years: ";
	int yrs;
	cin >> yrs;
	Wine holding(lab, yrs);
	holding.GetBottles();
	holding.show();

	const int YRS = 3;
	int y[YRS] = { 1993,1995,1998 };
	int b[YRS] = { 48,60,72 };

	Wine more("Gushing Grape Red", YRS, y, b);
	more.show();
	cout << "Total bottles for " << more.Label()
		<< ": " << more.sum() << std::endl;
	cout << "Bye\n";

	/*PairArray pair;
	pair.operator=(PairArray(ArrayInt(), ArrayInt()));
	里面的ArrayInt()也就是构造函数比如这样 ArrayInt in = ArrayInt()*/
	return 0;
}

输出结果还是一样的

第三题

 

 workermi.h

#ifndef WORKER0_H_
#define WORKER0_H_
#include<string>

class Worker
{
private:
	std::string fullname;
	long id;
public:
	Worker() : fullname("no one"), id(0l) { }
	Worker(const std::string& s, long n) : fullname(s), id(n) { }
	Worker(const Worker& wo) {*this = wo; }
	~Worker() { }
	bool Set();
	void Show()const;
};

#endif

queuetp.h

#ifndef QUEUE_H_
#define QUEUE_H_

template<class T>
class QueueTp
{
private:
	typedef struct Queue{T a; struct Queue* next;} Node;
	Node* front;
	Node* rear;
	int item;
	const int qsize;
	enum { MAXQUEUE = 10 };
public:
	QueueTp(const int n = MAXQUEUE) : 
		qsize(MAXQUEUE),front(nullptr),rear(nullptr),item(0) { }
	~QueueTp();
	bool is_empty()const;
	bool is_full()const;
	int queuecount()const;
	bool enqueue(const T& items);
	bool dequeue(T& items);
};

template<class T>
QueueTp<T>::~QueueTp()
{
	if (!is_empty())
	{
		int i = 1;
		Node* temp;
		while (front != nullptr)
		{
			std::cout << i++ << " >> 释放\n";
			temp = front;
			front = temp->next;
			delete temp;
		}
	}
}

template<class T>
bool QueueTp<T>::is_empty()const
{
	return item == 0;
}

template<class T>
bool QueueTp<T>::is_full()const
{
	return item == qsize;
}

template<class T>
int QueueTp<T>::queuecount()const
{
	return item;
}

template<class T>
bool QueueTp<T>::enqueue(const T& items)
{
	if (is_full())
		return false;
	Node* scan = new Node;
	scan->a = items;
	scan->next = nullptr;
	item++;
	if (front == nullptr)
		front = scan;
	else
		rear->next = scan;
	rear = scan;
	return true;
}

template<class T>
bool QueueTp<T>::dequeue(T& items)
{
	if (front == nullptr)
	{
		return false;
	}
	items = front->a;
	item--;
	Node* temp;
	temp = front;
	front = temp->next;
	delete temp;
	if (is_empty())
		front = rear = nullptr;
	return true;
}
#endif

queue.cpp

#include"workermi.h"
#include<iostream>
using std::cout;
using std::cin;
using std::endl;

bool Worker::Set()
{
	cout << "fullname: ";
	if (!getline(cin, fullname))
		return false;
	cout << "Enter worker's ID: ";
	if (!(cin >> id))
		return false;
	while (cin.get() != '\n')
		continue;
	return true;
}

void Worker::Show()const
{
	cout << "fullname: " << fullname;
	cout << "\nID: " << id << endl;
}

main.cpp

#include<iostream>
#include<cstring>
#include"queuetp.h"
#include"workermi.h"
int main()
{
	using std::cout;
	using std::endl;
	using std::strchr;
	using std::cin;

	QueueTp<Worker>* lolas = new QueueTp<Worker>(5);
	Worker wor;
	Worker ker;
	cout << "enqueue\n";
	for (int i = 1; i <= 6; i++)
	{
		cout << i << " >> ";
		if (!wor.Set())
			break;
		if (!lolas->enqueue(wor))
			break;
	}

	cout << "dequeue\n" << endl;
	decltype(lolas->queuecount()) count = lolas->queuecount();
	while(count-- > 0)
	{
		lolas->dequeue(ker);
		ker.Show();
	}
	lolas->~QueueTp();//这里并不会释放,因为上面已经释放完,上面注释掉再来调用就可以了cout到while循环注释
	delete lolas;
	return 0;
}

输出结果

注释掉输出结果

结束符windows为ctal + z

第4题 

Person.h

#ifndef PSRSON_H_
#define PSRSON_H_
#include<iostream>
#include<string>
using std::string;
class Person
{
private:
	string str_name;
	string str_surname;
protected:
	virtual void Get();
public:
	Person(string name = "no name", string surname = "no surname") :
			str_name(name),str_surname(surname) { }
	Person(const char* name,const char* surname) : 
			str_name(name),str_surname(surname) { }
	virtual ~Person() = 0;
	virtual void Show()const = 0;
	virtual void Set() = 0;
};

class Gunslinger : public virtual Person
{
private:
	double gun_time;
	int gun_nicks;
protected:
	virtual void Data()const;
	void Get();
public:
	Gunslinger(double dou_time = 0, int nicks = 0) : 
			Person(),gun_time(dou_time),gun_nicks(nicks) { }
	~Gunslinger() { }
	double Draw()const { return gun_time; }
	void Set();
	void Show()const;
};

class PokerPlayer : public virtual Person
{
public:
	PokerPlayer() : Person() { }
	void Show()const {Person::Show();}
	void Set() { Person::Get(); }
	int Draw()const;
	~PokerPlayer() { }
};

class BadDude : public Gunslinger, public PokerPlayer
{
private:
	int num;
protected:
	void Data()const { Gunslinger::Data();}
public:
	BadDude() : Person(), Gunslinger(), PokerPlayer() { num = PokerPlayer::Draw(); }
	~BadDude() { }
	int Gdraw()const { return Gunslinger::Draw(); }//返回下一张扑克牌值
	int Cdraw()const;//返回下一张扑克牌值
	void Show()const;//显示全部
	void Set() { Person::Get(); Gunslinger::Get();}
};
#endif

 person.cpp

#include"Person.h"
#include<cstdlib>

Person::~Person() { }

void Person::Show()const
{
	using std::cout;
	using std::endl;
	cout << "name: " << str_name << std::endl
		 << "surname: " << str_surname << std::endl;
}

void Person::Get()
{
	using std::cin;
	using std::cout;
	cout << "Enter name: ";
	getline(cin, str_name);
	cout << "Enter surname: ";
	getline(cin, str_surname);
}

void Gunslinger::Show()const
{
	Person::Show();
	using std::cout;
	using std::endl;
	Data();
}

void Gunslinger::Data()const
{
	using std::cout;
	using std::endl;
	cout << "gun_time: " << gun_time << endl
		 << "gun_nicks: " << gun_nicks << endl;
}

void Gunslinger::Get()
{
	std::cout << "Enter the time the gun was drawn: ";
	std::cin >> gun_time;
	std::cout << "Enter the number of gunners notches: ";
	std::cin >> gun_nicks;
	std::cin.get();
}

void Gunslinger::Set()
{
	Person::Get();
	Get();
}

int PokerPlayer::Draw()const
{
	srand(time(0));
	return rand() % 52 + 1;
}

void BadDude::Show()const
{
	Person::Show(); 
	Data();
	std::cout << "Poker denomination: " << num << std::endl;
}

int BadDude::Cdraw()const
{
	return num + 1 > 52 ? 1 : num + 1;
}

 main.cpp

#include"Person.h"
#include<iostream>
int main()
{
	using std::cout;
	using std::endl;
	using std::cin;
	using std::string;
	using std::strchr;
	constexpr int SIZE = 3;

	//Gunslinger gun;
	//gun.Set();
	//PokerPlayer pl;
	//pl.Set();
	//BadDude ba;
	//ba.Set();
	//cout << "This is your member:\n\n";
	//gun.Show();
	//cout << endl;
	//pl.Show();
	//cout << endl;
	//ba.Show();
	//cout << ba.Cdraw();//返回下一张

	Person* lolas[SIZE];
	int ct;

	for (ct = 0; ct < SIZE; ct++)
	{
		char choice;
		cout << "w:Gunslinger  s: PokerPlayer "
			 << "t: BadDude q: quit\n";
		cin >> choice;
		while (strchr("wstq", choice) == NULL)
		{
			cout << "Please enter a w, s, t, or q: ";
			cin >> choice;
		}
		if (choice == 'q')
			break;
		switch (choice)
		{
		case 'w':lolas[ct] = new Gunslinger; break;
		case 's':lolas[ct] = new PokerPlayer; break;
		case 't':lolas[ct] = new BadDude; break;
		}
		cin.get();
		lolas[ct]->Set();
	}

	cout << endl << endl;
	int i;
	for (i = 0; i < ct; i++)
	{
		cout << endl;
		lolas[i]->Show();
	}
	for (i = 0; i < ct; i++)
		delete lolas[i];
	cout << "Bye.\n";

	return 0;
}

输出结果

第五题

 略

emp.h

#ifndef EMP_H_
#define EMP_H_
#include<iostream>
#include<string>

class abstr_emp
{
private:
	std::string fname;//名字
	std::string lname;//姓氏
	std::string job;
public:
	abstr_emp();
	abstr_emp(const std::string& fn, const std::string& ln, const std::string l);
	virtual void ShowAll()const;//标记并显示所有数据
	virtual void SetAll();//提示用户输入值
	friend std::ostream& operator<<(std::ostream& os, const abstr_emp& e);//只显示第一个和 lase 名称
	virtual ~abstr_emp() = 0;
};

class employee : public abstr_emp
{
public:
	employee();
	employee(const std::string& fn, const std::string& ln, const std::string& j);
	virtual void ShowAll()const;
	virtual void SetAll();
};

class manager : virtual public abstr_emp
{
private:
	int inchargeof;
protected:
	int InChargeOf()const { return inchargeof; }
	int& InChargeOf() { return inchargeof; }
public:
	manager();
	manager(const std::string& fn, const std::string& ln, const std::string& j, int ico = 0);
	manager(const abstr_emp& e, int ico);
	manager(const manager& m);
	virtual void ShowAll()const;
	virtual void SetAll();
};

class fink : virtual public abstr_emp
{
private:
	std::string reportsto;
protected:
	const std::string ReportsTo()const { return reportsto; }
	std::string& ReportsTo() { return reportsto; }
public:
	fink();
	fink(const std::string& fn, const std::string& ln, const std::string& j, const std::string& rpo);
	fink(const abstr_emp& e, const std::string& rpo);
	fink(const fink& e);
	virtual void ShowAll()const;
	virtual void SetAll();
};

class highfink : public manager, public fink
{
public:
	highfink();
	highfink(const std::string& fn, const std::string& ln, const std::string& j, const std::string& rpo,int ico);
	highfink(const abstr_emp& e, const std::string& rpo, int ico);
	highfink(const fink& f, int ico);
	highfink(const manager& m, const std::string& rpo);
	highfink(const highfink& h);
	virtual void ShowAll()const;
	virtual void SetAll();

};
#endif

 emp.cpp

#include"emp.h"

abstr_emp::~abstr_emp() { }
abstr_emp::abstr_emp() : fname("no name"),lname("no name"),job("no") { }
abstr_emp::abstr_emp(const std::string& fn, const std::string& ln, 
					 const std::string l) : fname(fn),lname(ln),job(l) { }

void abstr_emp::ShowAll()const
{
	using std::cout;
	using std::endl;
	cout << "first name: " << fname << endl
		 << "lase name: " << lname << endl
		 << "job: " << job << endl;
}

void abstr_emp::SetAll()
{
	using std::cout;
	using std::cin;

	cout << "Enter first name: ";
	getline(cin,fname);
	cout << "Enter last name: ";
	getline(cin, lname);
	cout << "Enter job: ";
	getline(cin, job);
}

std::ostream& operator<<(std::ostream& os, const abstr_emp& e)
{
	os  << "first name: " << e.fname << std::endl
		<< "lase name: "  << e.lname << std::endl;
	return os;
}

employee::employee() : abstr_emp() { }
employee::employee(const std::string& fn, const std::string& ln, 
					const std::string& j) : abstr_emp(fn,ln,j) { }

void employee::ShowAll()const
{
	abstr_emp::ShowAll();
}

void employee::SetAll()
{
	abstr_emp::SetAll();
}


manager::manager() : abstr_emp(),inchargeof(0) { }
manager::manager(const std::string& fn, const std::string& ln, const std::string& j, 
				 int ico) : abstr_emp(fn,ln,j),inchargeof(ico) { }
manager::manager(const abstr_emp& e, int ico) : abstr_emp(e),inchargeof(ico) { }

manager::manager(const manager& m) : abstr_emp(m) { inchargeof = m.inchargeof; }

void manager::ShowAll()const
{
	abstr_emp::ShowAll();
	std::cout << "inchargeof: " << inchargeof << std::endl;
}

void manager::SetAll()
{
	abstr_emp::SetAll();
	std::cout << "Enter inchargeof: ";
	(std::cin >> inchargeof).get();
}

fink::fink() : abstr_emp(),reportsto("no") { }
fink::fink(const std::string& fn, const std::string& ln, const std::string& j,
			const std::string& rpo) : abstr_emp(fn,ln,j),reportsto(rpo) { }
fink::fink(const abstr_emp& e, const std::string& rpo) : abstr_emp(e),reportsto(rpo) { }
fink::fink(const fink& e) : abstr_emp(e) { reportsto = e.reportsto; }

void fink::ShowAll()const
{
	abstr_emp::ShowAll();
	std::cout << "reportsto: " << reportsto << std::endl;
}

void fink::SetAll()
{
	abstr_emp::SetAll();
	std::cout << "Enter reportsto: ";
	getline(std::cin, reportsto);
}

highfink::highfink() : abstr_emp(),manager(),fink() { }
highfink::highfink(const std::string& fn, const std::string& ln, const std::string& j, const std::string& rpo, 
					int ico) : abstr_emp(fn,ln,j),fink(fn,ln,j,rpo),manager(fn,ln,j,ico) { }
highfink::highfink(const abstr_emp& e, const std::string& rpo, int ico) : abstr_emp(e),fink(e,rpo),manager(e,ico) { }
highfink::highfink(const fink& f, int ico) : abstr_emp(f), fink(f), manager(f, ico) { }
highfink::highfink(const manager& m, const std::string& rpo) : abstr_emp(m),fink(m,rpo),manager(m) { }
highfink::highfink(const highfink& h) : abstr_emp(h),fink(h),manager(h) { }

void highfink::ShowAll()const
{
	abstr_emp::ShowAll();
	std::cout << "inchargeof: " << InChargeOf() << std::endl;
	std::cout << "reportsto: " << ReportsTo() << std::endl;
}

void highfink::SetAll()
{
	abstr_emp::SetAll();
	using std::cout;
	using std::cin;
	cout << "Enter inchargeof: ";
	cin >> InChargeOf();
	cout << "Enter reportsto: ";
	(cin >> ReportsTo()).get();
}

 main.cpp

#include"emp.h"
#include<iostream>
using namespace std;
int main(void)
{
	employee em("Trip", "Harris", "Thumper");
	cout << em << endl;
	em.ShowAll();
	manager ma("Amorphia", "Spindragon", "Nuancer", 5);
	cout << ma << endl;
	ma.ShowAll();

	fink fi("Matt", "Oggs", "Oiler", "Juno Barr");
	cout << fi << endl;
	fi.ShowAll();
	highfink hf(ma, "Curly Kew");
	hf.ShowAll();
	cout << "Press a key for next phase:\n";
	cin.get();
	highfink hf2;
	hf2.SetAll();

	cout << "Using an abstr emp * pointer:\n";
	abstr_emp* tri[4] = { &em,&fi,&hf,&hf2 };
	for (int i = 0; i < 4; i++)
		tri[i]->ShowAll();
	return 0;
}

 输出结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值