Function Templates - Example 10-8 - function template stored in a header file

// File:  ex10-8.h

#ifndef EX10_8_H
#define EX10_8_H

#include <iostream>

template <typename T> void Sort(T* a,int size)
{
	int i,j;
	for (i = 1; i < size; i++)
		for (j = 0; j < i; j++) if ( a[i] < a[j] ) Swap(a[i],a[j]);
	return;
}

template <typename U> void Swap(U& a,U& b)
{
	U temp;
	temp = a;
	a = b;
	b = temp;
	return;
}

template <typename V> void array_print(V* a,int size)
{
	int i;
	for (i = 0; i < size; i++) std::cout << a[i] << std::endl;
	std::cout << std::endl;
}

#endif

// File: ex10-8.cpp

#include "ex10-8.h"

#include <iostream>
using namespace std;

class fraction
{
private:
	int numer,denom;
public:
	fraction() {}
	void assign(int n, int d) { numer = n; denom = d; }
	int operator<(fraction& f);
	friend ostream& operator<<(ostream& s,fraction& f);
};

int fraction::operator<(fraction& f)
{
	return (static_cast<float>(numer)/denom < static_cast<float>(f.numer)/f.denom);
}

ostream& operator<<(ostream& s,fraction& f)
{
	s << f.numer << '/' << f.denom;
	return s;
}


char* suit_name[4] = {"clubs","diamonds","hearts","spades"};
char* pips_name[13] = {"two","three","four","five","six","seven",
"eight","nine","ten","jack","queen","king","ace"};

class card
{
protected:
	int pips,suit;
public:
	card() {}
	void assign(int n) { suit = n/13; pips = n % 13; }
	int operator<(card& c);
	friend ostream& operator<<(ostream& s,card& c);

};

int card::operator<(card& c)
{
	return (pips < c.pips);
}

ostream& operator<<(ostream& s,card& c)
{
	s << pips_name[c.pips] << " of " << suit_name[c.suit];
	return s;
}


class pinocle_card : public card
{
public:
	pinocle_card() {}
	int operator<(pinocle_card&);
	void assign(int n) { pips = n % 6 + 7; suit = n / 2 % 4; return; }
};

int pinocle_card::operator<(pinocle_card& c)
{
	if (pips != 8 && c.pips != 8) return (pips < c.pips);
	else if (pips == 8 && c.pips != 12) return 0;
	else if (c.pips == 8 && pips != 12) return 1;
	else return 0;
}

int main(void)
{
	// array of int
	int a1[5] = { 3, 5, 1, 9, 94};
	array_print(a1,5);
	Sort(a1,5);
	array_print(a1,5);

	// array of double
	double a2[4] = { 3.7, 1.5, -1.1,.9};
	array_print(a2,4);
	Sort(a2,4);
	array_print(a2,4);

	// array of char
	char a3[4] = {"hey"};
	array_print(a3,3);
	Sort(a3,3);
	array_print(a3,3);

	// array of fractions
	fraction a4[4];
	a4[0].assign(2,3);
	a4[1].assign(1,2);
	a4[2].assign(3,4);
	a4[3].assign(5,9);
	array_print(a4,4);
	Sort(a4,4);
	array_print(a4,4);

	// array of cards
	card a5[4];
	a5[0].assign(47);
	a5[1].assign(23);
	a5[2].assign(43);
	a5[3].assign(1);
	array_print(a5,4);
	Sort(a5,4);
	array_print(a5,4);

	// array of pinocle_cards
	pinocle_card a6[6];
	a6[0].assign(32);
	a6[1].assign(18);
	a6[2].assign(41);
	a6[3].assign(10);
	a6[4].assign(13);
	a6[5].assign(27);
	array_print(a6,6);
	Sort(a6,6);
	array_print(a6,6);

	return 0;

}
/****** Output ******
3
5
1
9
94

1
3
5
9
94

3.7
1.5
-1.1
0.9

-1.1
0.9
1.5
3.7

h
e
y

e
h
y

2/3
1/2
3/4
5/9

1/2
5/9
2/3
3/4

ten of spades
queen of diamonds
six of spades
three of clubs

three of clubs
six of spades
ten of spades
queen of diamonds

jack of clubs
nine of diamonds
ace of clubs
king of diamonds
ten of hearts
queen of diamonds

nine of diamonds
jack of clubs
queen of diamonds
king of diamonds
ten of hearts
ace of clubs
***********************/