Function Templates - Example 10-7 - Template function with a user defined type
// File: ex10-7.cpp
#include <iostream>
using namespace std;
const char* pips_name[13] = {"two","three","four","five","six","seven","eight",
"nine","ten","jack","queen","king","ace"};
const char* suit_name[4] = {"clubs","diamonds","hearts","spades"};
class card
{
private:
int pips;
int suit;
public:
card(int n) {pips = n % 13; suit = n / 13; }
void print(void) const
{ cout << pips_name[pips] << " of " << suit_name[suit] << endl; }
int operator>(card c) { return pips > c.pips; }
};
template <typename T> T Max(T a, T b)
{
return (a>b) ? a : b;
}
int main(void)
{
cout << Max(3,4) << endl;
card c1(23), c2(9);
c1.print();
c2.print();
(Max(c1,c2)).print();
return 0;
}
/***** Output *****
4
queen of diamonds
jack of clubs
queen of diamonds
*********************/