//* simple tests for function templates *

#include <iostream> using std::cin; using std::cout; using std::endl; // note: namespace std already has swap that is identical to ours // so we cannot simply import the whole namespace // and at the same time have our function named swap template <typename T> void swap (T &a, T &b) { T c=a; a=b; b=c; } template <typename T> T maximum(T a, T b) { if (a>=b) return(a); else return(b); } // note: this one will not work with string! template <typename T> T square(T x) { return(x*x); } int main(){ // integer test cout << "int test - enter two values:" << endl; int a, b; cin >> a >> b; cout << maximum(a,b) << endl; cout << square(a) << endl; swap(a,b); cout << a << " " << b << endl; // char test cout << "char test - enter two values:" << endl; char c, d; cin >> c >> d; cout << maximum(c,d) << endl;; // cout << square(c) << endl; /* will compile but it will calculate a strange square using integer numbers that are ASCII code of the passed character IT will not work for string for sure though :-) */ swap(c,d); cout << c << " " << d << endl; // double test cout << "double test - enter two values:" << endl; double e, f; cin >> e >> f; cout << maximum(e,f) << endl; cout << square(e) << endl; swap(e,f); cout << e << " " << f << endl; return(0); } // TEMPLATES SHOULD BE DECLARED BEFORE THEY ARE USED!!! // other templates that could be used for exercising: // max(x,x), min(x,x,x), max(x,x,x), swap(x,x,x), ... // at this time (given the data types you know about) // you may (but do not have to) test: // - int, unsigned int, long int, unsigned long int // - short int, unsigned short int // - char, singend char // - double, float, long double // - string (you need to add the string library to this program)