C++Primer Plus笔记——第十四章 C++中的代码重用课后编程练习答案

编程练习答案

习题1     习题2     习题3    习题4    习题5


习题1

//winec.h
#ifndef WINEC_H_
#define WINEC_H_
#include <iostream>
#include <string>
#include <valarray>
using namespace std;
template<class T1, class T2>
class Pair
{
private:
	T1 year;
	T2 bottles;
public:
	Pair(const T1 &yr, const T2 &bt) :year(yr), bottles(bt) {}
	Pair() {}
	void Set(const T1 &yr, const T2 &bt);
	int Sum()const;
	void Show(int y)const;
};
template<class T1, class T2>
void Pair<T1, T2>::Set(const T1 &yr, const T2 &bt)
{
	year = yr;
	bottles = bt;
}
template<class T1, class T2>
int Pair<T1, T2>::Sum()const
{
	return bottles.sum();
}
template<class T1, class T2>
void Pair<T1, T2>::Show(int y)const
{
	for (inti = 0; i < y; i++)
		cout << "\t" << year[i] << "\t" << bottles[i] << endl;
}
typedef valarray<int>ArrayInt;
typedef Pair<ArrayInt, ArrayInt>PairArray;
class Wine
{
private:
	PairArray yb;
	string fullname;
	int yrs;
public:
	Wine() {}
	Wine(const char *l, int y, const int yr[], const int bot[]);
	Wine(const char *l, int y);
	voidGetBottles();
	string&Label();
	void Show()const;
	int sum()const;
};
#endif
//winec.cpp
#include "winec.h"
Wine::Wine(const char *l, int y, const int yr[], const int bot[])
{
	fullname = l;
	yrs = y;
	yb.Set(ArrayInt(yr, yrs), ArrayInt(bot, yrs));
}
Wine::Wine(const char *l, int y)
{
	fullname = l;
	yrs = y;
}
void Wine::GetBottles()
{
	ArrayInt yr(yrs), bt(yrs);
	for (int i = 0; i < yrs; i++)
	{
		cout << "Enter the year: ";
		cin >> yr[i];
		cout << "Enter the bottles: ";
		cin >> bt[i];
	}
	while (cin.get() != '\n')
		continue;
	yb.Set(yr, bt);
}
string&Wine::Label()
{
	return fullname;
}
void Wine::Show()const
{
	cout << "Wine: " << fullname << endl;
	cout << "\tYear\tBottles\n";
	yb.Show(yrs);
}
int Wine::sum()const
{
	return yb.Sum();
}
//main.cpp
#include "winec.h"
int main(void)
{
	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() << endl;
	cout << "Bye\n";
	system("pause");
	return 0;
}

习题2

//winec.h
#ifndef WINEC_H_
#define WINEC_H_
#include <iostream>
#include <string>
#include <valarray>
using namespace std;
template<class T1, class T2>
class Pair
{
private:
	T1 year;
	T2 bottles;
public:
	Pair(const T1 &yr, const T2 &bt) :year(yr), bottles(bt) {}
	Pair() {}
	void Set(const T1 &yr, const T2 &bt);
	int Sum()const;
	void Show(int y)const;
};
template<class T1, class T2>
void Pair<T1, T2>::Set(const T1 &yr, const T2 &bt)
{
	year = yr;
	bottles = bt;
}
template<class T1, class T2>
int Pair<T1, T2>::Sum()const
{
	return bottles.sum();
}
template<class T1, class T2>
void Pair<T1, T2>::Show(int y)const
{
	for (int i = 0; i < y; i++)
		cout << "\t" << year[i] << "\t" << bottles[i] << endl;
}
typedef valarray<int>ArrayInt;
typedef Pair<ArrayInt, ArrayInt>PairArray;
class Wine :private PairArray, private string
{
private:
	int yrs;
public:
	Wine() {}
	Wine(const char *l, int y, const int yr[], const int bot[]);
	Wine(const char *l, int y);
	voidGetBottles();
	string&Label();
	void Show()const;
	int sum()const;
};
#endif
//winec.cpp
#include "winec.h"
Wine::Wine(const char *l, int y, const int yr[], const int bot[]) :string(l), yrs(y), PairArray(ArrayInt(yr,
	y), ArrayInt(bot, y))
{
}
Wine::Wine(const char *l, int y) : string(l), yrs(y)
{
}
void Wine::GetBottles()
{
	ArrayInt yr(yrs), bt(yrs);
	for (int i = 0; i < yrs; i++)
	{
		cout << "Enter the year: ";
		cin >> yr[i];
		cout << "Enter the bottles: ";
		cin >> bt[i];
	}
	while (cin.get() != '\n')
		continue;
	PairArray::Set(yr, bt);
}
string&Wine::Label()
{
	return (string &)(*this);
}
void Wine::Show()const
{
	cout << "Wine: " << (string &)(*this) << endl;
	cout << "\tYear\tBottles\n";
	PairArray::Show(yrs);
}
int Wine::sum()const
{
	return PairArray::Sum();
}
//main.cpp
#include "winec.h"
int main(void)
{
	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() << endl;
	cout << "Bye\n";
	system("pause");
	return 0;
}

习题3

//queuetp.h
#ifndef QUEUETP_H_
#define QUEUETP_H_
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
template<typename T>
class QueueTp
{
private:
    struct Node { T item; struct Node *next; };
    Node *front;
    Node *rear;
    int items;
    const int qsize;
    QueueTp(constQueueTp&q) :qsize(0) {}
    QueueTp&operator=(const QueueTp&q) { return *this; }
public:
    QueueTp(int qs = 10);
    ~QueueTp();
    bool isempty()const;
    bool isfull()const;
    int queuecount()const;
    bool enqueue(const T &item);
    bool dequeue(T &item);
};
template<typename T>
QueueTp<T>::QueueTp(int qs) :qsize(qs)
{
	front = rear = NULL;
	items = 0;
}
template<typename T>
QueueTp<T>::~QueueTp()
{
	Node *temp;
	while (front != NULL)
	{
		temp = front;
		front = front->next;
		delete temp;
	}
}
template<typename T>
bool QueueTp<T>::isempty()const
{
	return items == 0;
}
template<typename T>
bool QueueTp<T>::isfull()const
{
	return items == qsize;
}
template<typename T>
int QueueTp<T>::queuecount()const
{
	return items;
}
template<typename T>
bool QueueTp<T>::enqueue(const T &item)
{
	if (isfull())
		return false;
	Node *add = new Node;
	add->item = item;
	add->next = NULL;
	items++;
	if (front == NULL)
		front = add;
	else
		rear->next = add;
	rear = add;
	return true;
}
template<typename T>
bool QueueTp<T>::dequeue(T &item)
{
	if (front == NULL)
		return false;
	item = front->item;
	items--;
	Node *temp = front;
	front = front->next;
	delete temp;
	if (items == 0)
		rear = NULL;
	return true;
}
class Worker
{
private:
	string fullname;
	long id;
public:
	Worker() :fullname("no one"), id(0L) {}
	Worker(const string &s, long n) :fullname(s), id(n) {}
	~Worker();
	void Set();
	void Show()const;
};
#endif
//workermi.cpp
#include "queuetp.h"
Worker::~Worker() {}
void Worker::Show()const
{
	cout << "Name: " << fullname << endl;
	cout << "Employee ID: " << id << endl;
}
void Worker::Set()
{
	cout << "Enter worker's name: ";
	getline(cin, fullname);
	cout << "Enter worker's ID: ";
	cin >> id;
	while (cin.get() != '\n')
		continue;
}


//main.cpp
#include "queuetp.h"
const int Size = 5;
int main()
{
	QueueTp<Worker *>lolas(Size);
	Worker *temp;
	int ct;
	for (ct = 0; ct < Size; ct++)
	{
		char ch;
		cout << "Enter the command:\n"
			<< "A or a enter queue, "
			<< "P or p delete queue, "
			<< "Q or q quit.\n";
		cin >> ch;
		while (strchr("apq", ch) == NULL)
		{
			cout << "Please enter a p or q: ";
			cin >> ch;
		}
		if (ch == 'q')
			break;
		switch (ch)
		{
		case'a':
			temp = new Worker;
			cin.get();
			temp->Set();
			if (lolas.isfull())
				cout << "Queue already full\n";
			else
				lolas.enqueue(temp);
			break;
		case'p':
			if (lolas.isempty())
				cout << "Queue already empty\n";
			else
				lolas.dequeue(temp);
			break;
		}
	}
	cout << "\nHere the total count: ";
	cout << lolas.queuecount();
	cout << "Done.\n";
	system("pause");
	return 0;
}

习题4

//person.h
#ifndef PERSON_H_
#define PERSON_H_
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
using namespace std;
class Person
{
private:
	string firstname;
	string lastname;
protected:
	virtual void Data()const;
	virtual void Get();
public:
	Person() :firstname("no one"), lastname("no one") {}
	Person(const string &f, const string &l) :firstname(f), lastname(l) {}
	Person(const Person &p) :Person(p) {}
	virtual ~Person() = 0;
	virtual void Set() = 0;
	virtual void Show()const = 0;
};

class Gunslinger :virtual public Person
{
private:
	int numsk;
protected:
	void Data()const;
	void Get();
public:
	Gunslinger() :numsk(0), Person() {}
	Gunslinger(int nk, const string &f, const string &l) :numsk(nk), Person(f, l) {}
	Gunslinger(int nk, const Person &p) :numsk(nk), Person(p) {}
	void Show()const;
	void Set();
	double Draw()const;
};

class PokerPlayer :virtual public Person
{
protected:
	void Data()const;
public:
	PokerPlayer() :Person() {}
	PokerPlayer(const string &f, const string &l) : Person(f, l) {}
	PokerPlayer(const Person &p) :Person(p) {}
	int Draw()const;
	void Show()const;
	void Set() { Person::Set(); }
};

class BadDude :public Gunslinger, public PokerPlayer
{
protected:
	void Data()const;
	void Get();
public:
	BadDude() {}
	BadDude(int nk, const string &f, const string &l) :Person(f, l), Gunslinger(nk, f, l), PokerPlayer(f, l) {}
	BadDude(int nk, const Person &p) :Person(p), Gunslinger(nk, p), PokerPlayer(p) {}
	BadDude(const Gunslinger &g) :Person(g), Gunslinger(g), PokerPlayer(g) {}
	BadDude(int nk, const PokerPlayer &po) :Person(po), Gunslinger(nk, po), PokerPlayer(po) {}
	double Gdraw()const;
	int Cdraw()const;
	void Set();
	void Show()const;
};
#endif
//person.cpp
#include "person.h"
Person::~Person() {}
void Person::Data()const
{
 cout << "First name is : " << firstname << endl;
 cout << "Last name is : " << lastname << endl;
}
void Person::Get()
 {
	  cout << "Enter first name: \n";
	  getline(cin, firstname);
	  cout << "Enter last name: \n";
	  getline(cin, lastname);
 }
 void Person::Show()const
 {
	  Data();
 }
 void Person::Set()
 {
	  Get();
 }
 void Gunslinger::Data()const
 {
	  cout << "Nick is :" << numsk << endl;
	  cout << "The time of get the gun :" << Gunslinger::Draw() << endl;
 }
 void Gunslinger::Get()
 {
	  cout << "Enter Nick: \n";
	  cin >> numsk;
 }
 void Gunslinger::Set()
  {
	  cout << "Enter Guns name: \n";
	  Person::Get();
	  Get();
  }
  void Gunslinger::Show()const
  {
	  cout << "Gunslinger: \n";
	  Person::Data();
	  Data();
  }
  double Gunslinger::Draw()const
  {
	  return rand() % 3 + 1;
  }
  intPokerPlayer::Draw()const
  {
	  return rand() % 52 + 1;
  }
  voidPokerPlayer::Data()const
  {
	  cout << "The cards :" << Draw() << endl;
  }
  voidPokerPlayer::Show()const
  {
	  cout << "PokerPlayer :\n";
	  Person::Data();
	  Data();
}
doubleBadDude::Gdraw()const
{
	  return Gunslinger::Draw();
}
intBadDude::Cdraw()const
{
	  return PokerPlayer::Draw();
}
voidBadDude::Data()const
{
	  Gunslinger::Data();
	  PokerPlayer::Data();
	  cout << "The next cards: " << Cdraw() << endl;
	  cout << "The time of BadDude get the gun: " << Gdraw() << endl;
}
voidBadDude::Get()
{
	  Gunslinger::Get();
}
voidBadDude::Set()
{
	  cout << "Enter BadDude name: \n";
	  Person::Get();
	  Get();
}
voidBadDude::Show()const
 {
	  cout << "BadDude: \n";
	  Person::Data();
	  Data();
 }
//main.cpp
#include "person.h"

const int Size = 5;
 
int main()
{
	Person *per[Size];
	int ct;
	for (ct = 0; ct < Size; ct++)
	{
		char choice;
		cout << "Enter the Person: \n"
			<< "g: gunslinger p: poker "
			<< "b: bad dude q: quit\n";
		cin >> choice;
		while (strchr("gpbq", choice) == NULL)
		{
			cout << "Please enter a p,g,o,q: ";
			cin >> choice;
		}
		if (choice == 'q')
			break;
		switch (choice)
		{
		case'g':
			per[ct] = new Gunslinger;
			break;
		case'p':
			per[ct] = new PokerPlayer;
			break;
		case'b':
			per[ct] = new BadDude;
			break;
		}
		cin.get();
		per[ct]->Set();
	}
	cout << "\nHere is your staff:\n";
	int i;
	for (i = 0; i < ct; i++)
	{
		cout << endl;
		per[i]->Show();
	}
	for (i = 0; i < ct; i++)
		delete per[i];
	cout << "Bye\n";
	system("pause");
	return 0;
}

习题5

//emp.h
#ifndef EMP_H_
#define EMP_H_
#include <iostream>
#include <string>
using namespace std;
class abstr_emp
{
private:
	string fname;
	string lname;
	string job;
public:
	abstr_emp();
	abstr_emp(const string &fn, const string &ln,
		const string &j);
	virtual void ShowAll()const;
	virtual void SetAll();
	friend ostream&operator<<(ostream&os, const abstr_emp&e);
	virtual ~abstr_emp() = 0;
};
class employee :public abstr_emp
{
public:
	employee();
	employee(const string &fn, const string &ln,
		const string &j);
	virtual void ShowAll()const;
	virtual void SetAll();
};
class manager :virtual public abstr_emp
{
private:
	int inchargeof;
protected:
	intInChargeOf()const { return inchargeof; }
	int&InChargeOf() { return inchargeof; }
public:
	manager();
	manager(const string &fn, const string &ln,
		const string &j, int ico = 0);
	manager(const abstr_emp&e, int ico = 0);
	manager(const manager &m);
	virtual void ShowAll()const;
	virtual void SetAll();
	void getInCharge() {
		cout << "Enter inchargeof: ";
		cin >> inchargeof;
	}
};
class fink :virtual public abstr_emp
{
private:
	string reportsto;
protected:
	const string ReportsTo()const { return reportsto; }
	string &ReportsTo() { return reportsto; }
public:
	fink();
	fink(const string &fn, const string &ln,
		const string &j, const string &rpo);
	fink(const abstr_emp&e, const string &rpo);
	fink(const fink &e);
	virtual void ShowAll()const;
	virtual void SetAll();
	void getReportsTo() {
		cout << "Enter reportsto: ";
		cin >> reportsto;
	}
};
class highfink :public manager, public fink
{
public:
	highfink();
	highfink(const string &fn, const string &ln,
		const string &j, const string &rpo, int ico = 0);
	highfink(const abstr_emp&e, const string &rpo, intico = 0);
	highfink(const fink &f, int ico = 0);
	highfink(const manager &m, const string &rpo);
	highfink(const highfink&h);
	virtual void ShowAll()const;
	virtual void SetAll();
};
#endif
//emp.cpp
#include "emp.h"
 abstr_emp::abstr_emp() :fname("no one"), lname("no one"), job("no job")
 {
 }
 abstr_emp::abstr_emp(const string &fn, const string &ln,
	  const string &j) : fname(fn), lname(ln), job(j)
 {
 }
 void abstr_emp::ShowAll()const
 {
	  cout << "Firstname: " << fname << endl;
	  cout << "Lastname: " << lname << endl;
	  cout << "Job is: " << job << endl;
 }
 void abstr_emp::SetAll()
 {
	  cout << "Enter firstname: ";
	  getline(cin, fname);
	  cout << "Enter lastname: ";
	  getline(cin, lname);
	  cout << "Enter position: ";
	  getline(cin, job);
 }
 ostream&operator<<(ostream&os, const abstr_emp&e)
 {
	  os << e.fname << " " << e.lname << ", " << e.job << endl;
	  return os;
 }
 abstr_emp::~abstr_emp()
 {
 }
 employee::employee() :abstr_emp()
 {
 }
 employee::employee(const string &fn, const string &ln,
	  const string &j) : abstr_emp(fn, ln, j)
 {
 }
 void employee::ShowAll()const
 {
	  abstr_emp::ShowAll();
 }
 void employee::SetAll()
 {
	  abstr_emp::SetAll();
 }
 manager::manager() :abstr_emp()
 {
 }
 manager::manager(const string &fn, const string &ln,
	  const 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)
 {
 }
 void manager::ShowAll()const
 {
	  abstr_emp::ShowAll();
	  cout << "Inchargeof: " << InChargeOf() << endl;
 }
 void manager::SetAll()
 {
	  abstr_emp::SetAll();
	  cout << "Enter inchargeof: ";
	  (cin >> inchargeof).get();
 }
 fink::fink() :abstr_emp()
 {
 }
 fink::fink(const string &fn, const string &ln,
	  const string &j, const string &rpo) : abstr_emp(fn, ln, j), reportsto(rpo)
 {
 }
 fink::fink(const abstr_emp&e, const string &rpo) : abstr_emp(e), reportsto(rpo)
 {
 }
 fink::fink(const fink &e) : abstr_emp(e)
 {
 }
 void fink::ShowAll()const
 {
	  abstr_emp::ShowAll();
	  cout << "Reportsto: " << ReportsTo() << endl;
 }
 void fink::SetAll()
 {
	  abstr_emp::SetAll();
	  cout << "Enter reportsto: ";
	  cin >> reportsto;
 }
 highfink::highfink() :abstr_emp(), manager(), fink()
 {
 }
 highfink::highfink(const string &fn, const string &ln,
	  const string &j, const string &rpo, int ico) : abstr_emp(fn, ln, j), manager(fn, ln, j, ico), fink(fn,
		  ln, j, rpo)
 {
 }
 highfink::highfink(const abstr_emp&e, const string &rpo, int ico) : abstr_emp(e), manager(e, ico),
	  fink(e, rpo)
 {
 }
 highfink::highfink(const fink &f, int ico) : abstr_emp(f), manager(f, ico), fink(f)
 {
 }
 highfink::highfink(const manager &m, const string &rpo) : abstr_emp(m), manager(m), fink(m,
	  rpo)
 {
 }
 highfink::highfink(const highfink&h) : abstr_emp(h), manager(h), fink(h)
 {
 }
 void highfink::ShowAll()const
 {
	  abstr_emp::ShowAll();
	  cout << "InChargeOf: " << manager::InChargeOf() << endl;
	  cout << "ReportsTo: " << fink::ReportsTo() << endl;
 }
 void highfink::SetAll()
 {
	  abstr_emp::SetAll();
	  manager::getInCharge();
	  fink::getReportsTo();
 }
 //useemp.cpp
#include "emp.h"

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();
	 system("pause");
	 return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值