The C++ function template with the overloaded types C++ code sample


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Printing some type values using function template with overloaded types in C++ programming

To show: How to use the function template with overloaded types which is the basic of standard template library (STL) in C++ programming




// using function template and the overloaded types

#include <iostream>

using namespace std;


// template declaration

template <class ANY_TYPE>



return (a > b) ? a : b;



// main program

int main(void)


int x = 10, y = -9;

double real = 3.1415;

char ch = 'C';

const char * str ="Test string";


cout<<"integer: maximum("<<x<<", "<<y<<") = "<<maximum(x, y)<<endl;

cout<<"integer: maximum(-47, "<<y<<") = "<<maximum(-47,y)<<endl;

cout<<"double: maximum("<<real<<", "<<double(y)<<") = "<<maximum(real,double(y))<<endl;

cout<<"double: maximum("<<real<<", "<<double(x)<<") = "<<maximum(real,double(x))<<endl;

cout<<"char: maximum("<<ch<<", "<<'A'<<") = "<<maximum(ch, 'A')<<endl;

cout<<"string: maximum("<<str<<", "<<"Test"<<") = "<<maximum(str, "Test")<<endl;


return 0;



Output example:


integer: maximum(10, -9) = 10

integer: maximum(-47, -9) = -9

double: maximum(3.1415, -9) = 3.1415

double: maximum(3.1415, 10) = 10

char: maximum(C, A) = C

string: maximum(Test string, Test) = Test string

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index