C++PrimerPlus(第6版)中文版十四章实例代码

实例14.1studentc.h

#ifndef STUDENTC_H_
#define STUDENTC_H_

#include <iostream>
#include <string>
#include <valarray>

class Student 
{
private:
	typedef std::valarray<double> ArrayDb;
	std::string name;
	ArrayDb scores;
	// private method for scores output
	std::ostream& arr_out(std::ostream& os)const;
public:
	Student() : name{"Null Student"} , scores()
	{

	}
	explicit Student(const std::string &s)
		: name(s), scores()
	{

	}
	explicit Student(int n) : name("Nully"), scores(n)
	{
		
	}
	Student(const std::string &s, int n)
		: name(s), scores(n)
	{

	}
	Student(const std::string &s, const ArrayDb &a)
		: name(s), scores(a)
	{

	}
	Student(const char *str, const double *pd, int n)
		: name(str), scores(pd, n)
	{

	}
	~Student()
	{

	}
	double Average() const;
	const std::string& Name() const;
	double& operator[](int i);
	double operator[](int i) const;
	// friends
	// input
	friend std::istream& operator>>(std::istream& is, Student& stu);
	friend std::istream& getline(std::istream &is, Student& stu);
	// output
	friend std::ostream& operator<<(std::ostream& os, const Student& stu);
};

#endif

实例14.2student.cpp

#include "studentc.h"
using std::ostream;
using std::endl;
using std::istream;
using std::string;
//public methods
double Student::Average() const
{
	if(scores.size() > 0)
	{
		return scores.sum() / scores.size();
	}
	else
	{
		return 0;
	}
}

const string& Student::Name() const
{
	return name;
}

double& Student::operator[](int i)
{
	return scores[i];
}

double Student::operator[](int i) const
{
	return scores[i];
}

// private method
ostream& Student::arr_out(ostream& os) const
{
	int i;
	int lim = scores.size();
	if(lim > 0)
	{
		for (i = 0; i < lim; ++i)
		{
			os << scores[i] << " ";
			if(i % 5 == 4)
			{
				os << endl;
			}
			if(i % 5 != 0)
			{
				os << endl;
			}
		}
	}
	else
	{
		os << "empty array";
	}
	return os;
}

// friends
// use string version of operator>>()
istream& operator>>(istream& is, Student& stu)
{
	is >> stu.name;
	return is;
}

// use string friend getline(ostream&, const string&)
istream& getline(istream& is, Student& stu)
{
	getline(is, stu.name);
	return is;
}

// use string version of operator<<()
ostream& operator<<(ostream& os, const Student& stu)
{
	os << "Scores for" << stu.name << ":\n";
	stu.arr_out(os);
	return os;
}

实例14.3use_stuc.cpp

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

void set(Student &sa, int n);
const int pupils = 3;
const int quizzes = 5;

int main()
{
	Student ada[pupils] = {Student(quizzes), Student(quizzes), Student(quizzes)};

	int i;
	for(i = 0; i < pupils; ++i)
	{
		set(ada[i], quizzes);
	}
	cout << "\nStudent List:\n";
	for (i = 0; i < pupils; ++i)
	{
		cout << ada[i].Name() << endl;
	}
	cout << "\nResults:";
	for (i = 0; i < pupils; ++i)
	{
		cout << endl << ada[i];
		cout << "average: " << ada[i].Average() << endl;
	}
	cout << "Done.\n";
	return 0;
}

void set(Student &sa, int n)
{
	cout << "Please enter the student's name: ";
	getline(cin, sa);
	cout << "Please enter " << n << " quiz scores:\n";
	for (int i = 0; i < n; ++i)
	{
		cin >> sa[i];
	}
	while (cin.get() != '\n')
	{
		continue;
	}
}

实例14.4studenti.h

#ifndef STUDENTC_H_
#define STUDENTC_H_

#include <iostream>
#include <valarray>
#include <string>

class Student : private std::string, private std::valarray<double>
{
private:
	typedef std::valarray<double> ArrayDb;
	// private method for scores output
	std::ostream& arr_out(std::ostream& os) const;
public:
	Student() : std::string("Null Student"), ArrayDb()
	{

	}
	explicit Student(const std::string &s)
		: std::string(s), ArrayDb()
	{

	}
	explicit Student(int n) : std::string("Nully"), ArrayDb(n)
	{

	}
	Student(const std::string &s, int n)
		: std::string(s), ArrayDb(n)
	{

	}
	Student(const std::string &s, const ArrayDb &a)
		: std::string(s), ArrayDb(a)
	{

	}
	Student(const char *str, const double *pd, int n)
		: std::string(str), ArrayDb(pd, n)
	{

	}
	~Student()
	{

	}
	double Average() const;
	double& operator[](int i);
	double operator[](int i) const;
	const std::string& Name() const;
	// friends
	// input
	friend std::istream& operator>>(std::istream &is, Student &stu);
	friend std::istream& getline(std::istream &is, Student &stu);
	// output
	friend std::ostream& operator<<(std::ostream &os, const Student &stu);
};

#endif

实例14.5student.cpp

#include "studenti.h"
using std::ostream;
using std::endl;
using std::istream;
using std::string;
//public methods
double Student::Average() const
{
	if (scores.size() > 0)
	{
		return scores.sum() / scores.size();
	}
	else
	{
		return 0;
	}
}

const string& Student::Name() const
{
	return (const string &) *this;
}

double& Student::operator[](int i)
{
	return ArrayDb::operator[] (i);
}

double Student::operator[](int i) const
{
	return ArrayDb::operator[] (i);
}

// private method
ostream& Student::arr_out(ostream& os) const
{
	int i;
	int lim = scores.size();
	if (lim > 0)
	{
		for (i = 0; i < lim; ++i)
		{
			os << scores[i] << " ";
			if (i % 5 == 4)
			{
				os << endl;
			}
			if (i % 5 != 0)
			{
				os << endl;
			}
		}
	}
	else
	{
		os << "empty array";
	}
	return os;
}

// friends
// use string version of operator>>()
istream& operator>>(istream& is, Student& stu)
{
	is >> (string&)stu;
	return is;
}

// use string friend getline(ostream&, const string&)
istream& getline(istream& is, Student& stu)
{
	getline(is, (string&)stu);
	return is;
}

// use string version of operator<<()
ostream& operator<<(ostream& os, const Student& stu)
{
	os << "Scores for" << (const string &) << ":\n";
	stu.arr_out(os);
	return os;
}

实例14.6use_stui.cpp

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

void set(Student &sa, int n);

const int pupils = 3;
const int quizzes = 5;

int main()
{
	Student ada[pupils] = {Student(quizzes), Student(quizzes), Student(quizzes)};
	
	int i;
	for(i = 0; i < pupils; ++i)
	{
		set(ada[i], quizzes);
	}
	cout << "\nStudent List:\n";
	for(i = 0; i < pupils; ++i)
	{
		cout << ada[i].Name() << endl;
	}
	cout << "\nResults:";
	for(i = 0; i < pupils; ++i)
	{
		cout << endl << ada[i];
		cout << "average: " << ada[i].Average() << endl;
	}
	cout << "Done.\n";

	system("pause");
	return 0;
}

void set(Student &sa, int n)
{
	cout << "Please enter the student's name: ";
	getline(cin, sa);
	cout << "Please enter " << n << " quiz scores:\n";
	for(int i = 0; i < n; ++i)
	{
		cin >> sa[i];
	}
	while (cin.get() != '\n')
	{
		continue;
	}
}

实例14.7Worker0.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)
	{

	}
	virtual ~Worker() = 0;	// pure virtual destructor
	virtual void Set();
	virtual void Show() const;
};

class Waiter : public Worker
{
private:
	int panache;
public:
	Waiter() : Worker(), panache(0)
	{

	}
	Waiter(const std::string &s, long n, int p = 0)
		: Worker(s, n), panache(p)
	{

	}
	Waiter(const Worker &wk, int p = 0)
		: Worker(wk), panache(p)
	{

	}
	void Set();
	void Show() const;
};

class Singer : public Worker
{
protected:
	enum{other, alto, contralto, soprano, bass, baritone, tenor};
	enum {Vtypes = 7};
private:
	static char *pv[Vtypes];
	int voice;
public:
	Singer() : Worker(), voice(other)
	{

	}
	Singer(const std::string &s, long n, int v = other)
		: Worker(s, n), voice(v)
	{

	}
	Singer(const Worker &wk, int v = other)
		: Worker(wk), voice(v)
	{

	}
	void Set();
	void Show() const;
};

#endif

实例14.8worker0.cpp

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

// Worker methods
// must implement virtual destructor, even if pure
Worker::~Worker()
{

}

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

void Worker::Show() const
{
	cout << "Name: " << fullname << "\n";
	cout << "Employee ID: " << id << "\n";
}

// Waiter methods
void Waiter::Set()
{
	Worker::Set();
	cout << "Enter waiter's panache rating: ";
	cin >> panache;
	while (cin.get() != '\n')
	{
		continue;
	}
}

void Waiter::Show() const
{
	cout << "Category: waiter\n";
	Worker::Show();
	cout << "Panache rating: " << panache << "\n";
}

// Singer methods
char *Singer::pv[] = “{”other“, "alto", "contralto", "soprano", "bass", "baritone", "tenor"};
void Singer::Set()
{
	Worker::Set();
	cout << "Enter number for singer's vocal range:\n";
	int i;
	for(i = 0; i < Vtypes; ++i)
	{
		cout << i << ": " << pv[i] << "  ";
		if(i % 4 == 3)
		{
			cout << endl;
		}
	}
	if(i % 4 != 0)
	{
		cout << endl;
	}
	while(cin >> voice && (voice < 0 || voice >= Vtypes) )
	{
		cout << "Please enter a value >= 0 and < " << Vtypes << endl;
	}
	while (cin.get() != '\n')
	{
		continue;
	}
}

void Singer::Show() const
{
	cout << "Category: singer\n";
	Worker::Show();
	cout << "Vocal range: " << pv[voice] << endl;
}

实例14.9worktest.cpp

#include <iostream>
#include "worker0.h"
const int LIM = 4;

int main()
{
	Waiter bob("Bob Apple", 314L, 5);
	Singer bev("Beverly Hills", 522L, 3);
	Waiter w_temp;
	Singer s_temp;

	Worker *pw[LIM] = {&bob, &bev, &w_temp, &s_temp};

	int i;
	for(i = 2; i < LIM; ++i)
	{
		pw[i]->Set();
	}
	for (i = 0; i < LIM; ++i)
	{
		pw[i]->Show();
		std::cout << std::endl;
	}

	system("pause");
	return 0;
}

实例14.10workermi.h

#ifndef WORKERMI_H_
#define WORKERMI_H_

#include<string>

class Worker
{
private:
	std::string fullname;
	long id;
protected:
	virtual void Data() const;
	virtual void Get();
public:
	Worker() : fullname("no one"), id(0L)
	{

	}
	Worker(const std::string &s, long n)
		: fullname(s), id(n)
	{

	}
	virtual ~Worker() = 0;
	virtual void Set() = 0;
	virtual void Show() const = 0;
};

class Waiter : virtual public Worker
{
private:
	int panache;
protected:
	void Date() const;
	void Get();
public:
	Waiter() : Worker(), panache(0)
	{

	}
	Waiter(const std::string &s, long n, int p = 0)
		: Worker(s, n), panache(p)
	{

	}
	Waiter(const  Worker &wk, int p = 0)
		: Worker(wk), panache(p)
	{

	}
	void Set();
	void Show() const;	
};

class Singer : virtual public Worker
{
protected:
	enum {other, alto, contralto, soprano, bass, baritone, tenor};
	enum {Vtypes = 7};
	void Data() const;
	void Get();
private:
	static char *pv[Vtypes];
	int voice;
public:
	Singer() : Worker(), voice(other)
	{

	}
	Singer(const std::string &s, long n, int v = other)
		: Worker(s, n), voice(v)
	{

	}
	Singer(const Worker &wk, int v = other)
		: Worker(wk), voice(v)
	{

	}
	void Set();
	void Show() const;
};

// multiple inheritance
class SingingWaiter : public Singer, public Waiter
{
protected:
	void Data() const;
	void Get();
public:
	SingingWaiter() 
	{

	}
	SingingWaiter(const std::string &s, long n, int p = 0, int v = other)
		: Worker(s, n), Waiter(s, n, p), Singer(s, n, v)
	{

	}
	SingingWaiter(const Worker &wk, int p = 0, int v = other)
		: Worker(wk), Waiter(wk, p), Singer(wk, v)
	{

	}
	SingingWaiter(const Waiter &wt, int v = other)
		: Worker(wt), Waiter(wt), Singer(wt, v)
	{

	}
	SingingWaiter(const Singer &wt, int p = 0)
		: Worker(wt), Waiter(wt, p), Singer(wt)
	{

	}
	void Set();
	void Show() const;
};

#endif

实例14.11workermi.cpp

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

//Worker methods
Worker::Worker()
{

}

// protected methods
void Worker::Data() const
{
	cout << "Name: " << fullname << endl;
	cout << "Employee ID: " << id << endl;
}

void Worker::Get()
{
	getline(cin, fullname);
	cout << "Enter worker's ID: ";
	cin >> id;
	while(cin.get() != '\n')
	{
		continue;
	}
}

// Waiter methods
void Waiter::Set()
{
	cout << "Enter waiter's name: ";
	Worker::Get();
	Get();
}

void Waiter::Show() const
{
	cout << "Category: waiter\n";
	Worker::Data();
	Data();
}

// protected methods
void Waiter::Data() const
{
	cout << "Panache rating: " << pannache << endl;
}

void Waiter::Get()
{
	cout << "Enter waiter's panache rating: ";
	cin >> panache;
	while (cin.get() != '\n')
	{
		continue;
	}
}

// Singer methods
char *Singer::pv[Singer::Vtypes] = {"other", "alto", "contralto", "soprano", "bass", "baritone", "tenor"};

void Singer::Set()
{
	cout << "Enter singer's name: ";
	Worker::Get();
	Get();
}

void Singer::Show() const
{
	cout << "Category: singer\n";
	Worker::Data();
	Data();
}

// protected methods
void Singer::Data() const
{
	cout << "Vocal range:" << pv[voice] << endl;
}

void Singer::Get()
{
	cout << "Enter number for singer's vocal range:\n";
	int i;
	for(i = 0; i < Vtypes; ++i)
	{
		cout << i << ": " << pv[i] << "    ";
		if(i % 4 == 3)
		{
			cout << endl;
		}
	}
	if(i % 4 != 0)
	{
		cout << '\n';
	}
	cin >> voice;
	while(cin.get() != '\n')
	{
		continue;
	}
}

// SingingWaiter methods
void SingingWaiter::Data() const
{
	Singer::Data();
	Waiter::Data();
}

void SingingWaiter::Get()
{
	Waiter::Get();
	Singer::Get();
}

void SingingWaiter::Set()
{
	cout << "Enter singing waiter's name: ";
	Worker::Get();
	Get();
}

void SingingWaiter::Show() const
{
	cout << "Category: singing waiter\n";
	Worker::Data();
	Data();
}

实例14.12workmi.cpp

#include <iostream>
#include <cstring>
#include "workermi.h"
const int SIZE = 5;

int main()
{
	using std::cin;
	using std::cout;
	using std::endl;
	using std::strchr;

	Worker *lolas[SIZE];
	int ct;
	for(ct = 0; ct < SIZE; ++ct)
	{
		char choice;
		cout << "Enter the employee category:\n"
				<< "w: waiter s: singer "
				<< "t:singing waiter q:quit\n";
		cin >> choice;
		while (strchr("wstp", choice) == NULL)
		{
			cout << "Please enter a, w, s, t, or q: ";
			cin >> choice;
		}
		if(choice == 'q')
		{
			break;
		}
		switch (choice)
		{
			case 'w':
			{
				lolas[ct] = new Waiter;
				break;
			}
			case 's':
			{
				lolas[ct] = new Singer;
				break;
			}
			case 't':
			{
				lolas[ct] = new SingingWaiter;
				break;
			}
		}
		cin.get();
		lolas[ct]->Set();
	}
	
	cout << "\nHere is your staff:\n";
	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";

	system("pause");
	return 0;
}

实例14.13stacktp.h

#ifndef STACKTP_H_
#define STACKTP_H_

template <class Type>
class Stack
{
private:
	enum {MAX = 10};
	Type items[MAX];
	int top;
public:
	Stack();
	bool isempty();
	bool isfull();
	bool push(const Type& item);
	bool pop(Type &item);
};

template <class Type>
Stack<Type>::Stack()
{
	top = 0;
}

template<class Type>
bool Stack<Type>::isempty()
{
	return top == 0;
}

template<class Type>
bool Stack<Type>::isfull()
{
	return top == MAX;
}

template<class Type>
bool Stack<Type>::push(const Type &item)
{
	if(top < MAX)
	{
		items[top++] = item;
		return true;
	}
	else
	{
		return false;
	}
}

template<class Type>
bool Stack<Type>::pop(Type &item)
{
	if(top > 0)
	{
		item = items[--top];
		return true;
	}
	else
	{
		return false;
	}
}

#endif

实例14.14stacktem.cpp

#include <iostream>
#include <string>
#include <cctype>
#include "stacktp.h"
using std::cin;
using std::cout;

int main()
{
	Stack<std::string> st;
	char ch;
	std::string po;
	cout << "Please enter A to add a purchase order,\n"
			<< "p to process a PO, or Q to quit.\n";
	while (cin >> ch && std::toupper(ch) != 'Q')
	{
		while (cin.get() != '\n')
		{
			continue;
		}
		if(!std::isalpha(ch))
		{
			cout << '\a';
			continue;
		}
		switch (ch)
		{
			case 'A':
			case 'a':
			{
				cout << "Enter a PO number to add: ";
				cin >> po;
				if(st.isfull())
				{
					cout << "stack already full\n";
				}
				else
				{
					st.push(po);
				}
				break;
			}
			case 'p':
			case 'P':
			{
				if(st.isempty())
				{
					cout << "stack already empty\n";
				}
				else
				{
					st.pop(po);
					cout << "PO #" << po << " popped\n";
					break;
				}
			}
		}
		cout << "Please enter A to add a purchase order,\n"
				<< "P to process a PO, or Q to quit.\n";
	}
	cout << "Bye\n";

	system("pause");
	return 0;
}

实例14.15stcktp1.h

#ifndef STCKTP1_H_
#define STCKTP1_H_

template <class Type>
class Stack
{
private:
	enum {SIZE = 10};
	int stacksize;
	Type *items;
	int top;
public:
	explicit Stack(int ss = SIZE);
	Stack(const Stack &st);
	~Stack()
	{
		delete []items;
	}
	bool isempty()
	{
		return top == 0;
	}
	bool isfulll()
	{
		return top == statcksize;
	}
	bool push(const Type &item);
	bool pop(Type &item);
	Stack& operator=(const Stack &st);
};

template<class Type>
Stack<Type>::Stack(int ss) : stacksize(ss), top(0)
{
	items = new Type[stacksize];
}

template<class Type>
Stack<Type>::Stack(int ss) : stacksize(ss), top(0)
{
	items = new Type[stacksize];
}

template<class Type>
Stack<Type>::Stack(const Stack &st)
{
	stacksize = st.stacksize;
	top = st.top;
	items = new Type[stacksize];
	for(int i = 0; i < top; ++i)
	{
		items[i] = st.items[i];
	}
}

template<class Type>
bool Stack<Type>::push(const Type &item)
{
	if(top < stacksize)
	{
		items[top++] = item;
		return true;
	}	
	else
	{
		return false;
	}
}

template<class Type>
bool Stack<Type>::pop(Type &item)
{
	if(top > 0)
	{
		item = items[--top];
		return true;
	}
	else
	{
		return false;
	}
}

template<class Type>
Stack<Type>& Stack<Type>::operator=(const Stack<Type> &st)
{
	if(this == &st)
	{
		return *this;
	}
	delete []items;
	stacksize = st.stacksize;
	top = st.top;
	items = new Type[stacksize];
	for(int i = 0; i < top; ++i)
	{
		item[i] = st.items[i];
	}
	return *this;
}
#endif

实例14.16stkoptr1.cpp

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "stcktp1.h"

const int Num = 10;

int main()
{
	std::srand(std::time(0));
	std::cout << "Please enter stack size: ";
	int stacksize;
	std::cin >> stacksize;
	// create an empty stack with stacksize slots
	Stack<const char*> st(stacksize);

	//int basket
	const char *in[Num] = {
							" 1: Hank Gilgamesh",
							" 2: Kiki Ishtar",
							" 3: Betty Rocker",
							" 4: Ian Flagranti",
							" 5: Wolfgang Kibble",
							" 6: Portia Koop",
							" 7: Joy Almondo",
							" 8: Xaverie Paprika",
							" 9: Juan Moore",
							" 10: Misha Mache"};
	//out basket
	const char* out[Num];

	int processed = 0;
	int nextin = 0;
	while(processed < Num)
	{
		if(st.isempty())
		{
			st.push(in[nextin++]);
		}
		else if (st.isfulll())
		{
			st.pop(out[processed++]);
		}
		else if(std::rand() % 2 && nextin < Num)
		{
			st.push(in[nextin++]);
		}
		else
		{
			st.pop(out[processed++]);
		}
	}
	for(int i = 0; i < Num; ++i)
	{
		std::cout << out[i] << std::endl;
	}
	std::cout << "Bye\n";

	system("pause");
	return 0;
}

实例14.17arraytp.h

#ifndef ARRAYTP_H_
#define ARRAYTP_H_

#include <iostream>
#include <cstdlib>

template<class T, int n>
class ArrayTp
{
private:
	T ar[n];
public:
	ArrayTp()
	{

	}
	explicit ArrayTp(const T &v);
	virtual T& opeartor[] (int i);
	virtual T opeartor[] (int i) const;
};

template<class T, int n>
ArrayTp<T, n>::ArrayTp(const T &v);
{
	for(int i = 0; i < n; ++i)
	{
		ar[i] = v;
	}
}

template<class T, int n>
T& ArrayTp<T, n>::operator[](int i)
{
	if(i < 0 || i >= n)
	{
		std::cerr << "Error in array limits: " << i
			<< " is out of range\n";
		std::exit(EXIT_FAILURE);		
	}
	return ar[i];
}

template<class T, int n>
T ArrayTp<T, p>::operator[](int i) const
{
	if(i < 0 || i >= n)
	{
		std:cerr << "Error in array limits: " << i
			<< "is out of range\n";
		std::exit(EXIT_FAILURE);
	}
	return ar[i];
}
#endif

实例14.18twod.cpp

#include <iostream>
#include "arraytp.h"
int main()
{
	using std::cout;
	using std::endl;
	ArrayTp<int, 10> sums;
	ArrayTp<double, 10> aves;
	ArrayTp< ArrayTp<int, 5>, 10> twodee;

	int i, j;

	for(i = 0; i < 10; ++i)
	{
		sums[i] = 0;
		for(j = 0; j < 5; j++)
		{
			twodee[i][j] = (i + 1) * (j + 1);
			sum[i] += twodee[i][j];
		}
		aves[i] = (double) sums[i] / 10;
	}
	for(i = 0; i < 10; ++i)
	{
		for(j = 0; j < 5; ++j)
		{
			cout.width(2);
			cout << twodee[i][j] << ' ';
		}
		cout << ": sum = ";
		cout.width(3);
		cout << sums[i] << ", average = " << aves[i] << endl;
	}

	cout << "Done.\n";

	system("pause");
	return 0;
}

实例14.19pairs.cpp

#include <iostream>
#include <string>
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;
}

int main()
{
	using std::cout;
	using std::endl;
	using std::string;
	Pair<string, int> ratings[4] = 
	{
		Pair<string, int>("The Purpled Duck", 5),
		Pair<string, int>("Jaquie's Frisco AI Fresco", 4),
		Pair<string, int>("Cafe Souffle", 5),
		Pair<string, int>("Bertie's Eats", 3)
	};
	int joints = sizeof(ratings) / sizeof (Pair<string, int>);
	cout << "Rating:\t Eatery\n";
	for(int i = 0; i < joints; ++i)
	{
		cout << ratings[i].second() << ":\t"
				<< ratings[i].first() << endl;
	}
	cout << "Oops! Revised rating:\n";
	ratings[3].first() = "Bertie's Fab Eats";
	ratings[3].second{} = 6;
	cout << ratings[3].second() << ":\t"
		<< ratings[3].first() << endl;

	system("pause");
	return 0;
}

实例14.20tempmemb.cpp

#include <iostream>
using std::cout;
using std::endl;
template<typename T>
class beta
{
private:
	template<typename V>
	class hold
	{
	private:
		V val;
	public:
		hold(V v = 0) : val(v)
		{

		}
		void show() const
		{
			cout << val << endl;
		}
		V Value() const
		{
			return val;
		}
	};
public:
	beta(T t, int i) : q(t), n(i)
	{

	}
	template<typename U>
	U blab(U u, T t)
	{
		return (n.Value() + q.Value()) * u / t;
	}
	void Show() const
	{
		q.show();
		n.show();
	}
};

int main()
{
	beta<double> guy(3.5, 3);
	cout << "T was set to double\n";
	guy.Show();
	cout << "V was set to T, which is double, then V was set to int\n";
	cout << guy.blab(10, 2.3) << endl;
	cout << "U was set to int\n";
	cout << guy.blab(10.0, 2.3) << endl;
	cout << "U was set to double\n";
	cout << "Done\n";

	system("pause");
	return 0;
}

实例14.21tempparm.cpp

#include <iostream>
#include "stacktp.h"

template < template<typename T> class Thing>
class Crab
{
private:
	Thing<int> s1;
	Thing<double> s2;
public:
	Crab()
	{

	}
	// assumes the thing class has push() and pop() members
	bool push(int a, double x)
	{
		return s1.push(a) && s2.push(x);
	}
	bool pop(int &a, double &x)
	{
		return s1.pop(a) && s2.pop(x);
	}
};

int main()
{
	using std::cout;
	using std::cin;
	using std::endl;
	Crab<Stack> nebula;
	//Stack must match template <typename T> class thing
	int ni;
	double nb;
	cout << "Enter int double pairs, such as 4 3.5 (0.0 to end):\n";
	while(cin >> ni >> nb && ni > 0 && nb > 0)
	{
		if(!nebula.push(ni, nb))
		{
			break;
		}
	}
	while (nebula.pop(ni, nb))
	{
		cout << ni << ", " << nb << endl;
	}
	cout << "Done.\n";

	system("pause");
	return 0;
}

实例14.22frnd2tmp.cpp

#include <iostream>
using std::cout;
using std::endl;

template<typename T>
class HasFriend
{
private:
	T item;
	static int ct;
public:
	HasFriend(const T &i) : item(i)
	{
		++ct;
	}
	~HasFriend()
	{
		--ct;
	}
	friend void counts();
	friend void reports(HasFriend<T> &);
};

// each specialization has its own static data member
template<typename T>
int HasFriend<T>::ct = 0;

// non template friend to all HasFriden<T> classes
void counts()
{
	cout << "int count: " << HasFriend<int>::ct << "; ";
	cout << "double count" << HasFriend<double>::ct << endl;
}

// non template friend to the HasFriend<int> class
void reports(HasFriend<int> &hf)
{
	cout << "HasFriend<int>: " << hf.item << endl;
}

// non template friend to the HasFriend<double> class
void reports(HasFriend<double> &hf)
{
	cout << "HasFriend<double>: " << hf.item << endl;
}

int main()
{
	cout << "No objects declared: ";
	counts();
	HasFriend<int> hfi1(10);
	cout << "After hfi1 declared: ";
	counts();
	HasFriend<int> hfi2(20);
	cout << "After hfi2 declared: ";
	counts();
	HasFriend<double> hfdb(10.5);
	cout << "After hfdb declared: ";
	counts();
	reports(hfi1);
	reports(hfi2);
	reports(hfdb);

	system("pause");
	return 0;
}

实例14.23tmp2tmp.cpp

#include<iostream>
using std::cout;
using std::endl;
//template prototypes
template <typename T> void counts();
template <typename T> void report(T &);

//template class
template<typename TT>
class HasFriendT
{
private:
	TT item;
	static int ct;
public:
	HasFriendT(const TT &i) : item(i)
	{
		++ct;
	}
	~HasFriendT()
	{
		--ct;
	}
	friend void counts<TT>();
	friend void report<>(HasFriendT<TT> &);
};

template <typename T>
int HasFriendT<T>::ct = 0;

// template friend functions definitions
template<typename T>
void counts()
{
	cout << "template size: " << sizeof(HasFriendT<T>) << "; ";
	cout << "template counts(): " << HasFriendT<T>::ct << endl; 
}

template<typename T>
void report(T &hf)
{
	cout << hf.item << endl;
}

int main()
{
	counts<int>();
	HasFriendT<int> hfi1(10);
	HasFriendT<int> hfi2(20);
	HasFriendT<double> hfdb(10.5);
	report(hfi1);
	report(hfi2);
	report(hfdb);
	cout << "counts<int>() output:\n";
	counts<int>();
	cout << "counts<double>() output:\n";
	counts<double>();

	system("pause");
	return 0;
}

实例14.24manyfrnd.cpp

#include<iostream>
using std::cout;
using std::endl;

template<typename T>
class ManyFriend
{
private:
	T item;
public:
	ManyFriend(const T &i) : item(i)
	{

	}
	template<typename C, typename D> friend void show2(C &, D &);
};

template<typename C, typename D>void show2(C &c, D &d)
{
	cout << c.item << ", " << d.item << endl;
}

int main()
{
	ManyFriend<int> hfi1(10);
	ManyFriend<int> hfi2(20);
	ManyFriend<double> hfdb(10.5);
	cout << "hfi1, hfi2: ";
	show2(hfi1, hfi2);
	cout << "hfdb, hfi2: ";
	show2(hfdb, hfi2);
	
	system("pause");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值