Sunday, April 12, 2009

Function Templates in C++

/*
template_sum.cpp: This program is about creating template function for adding two numbers.
Date: 13/04/2009.
Author: Afiz ******
*/

#include // including header files.
using namespace std; //

class math{ // main class.

public: // access modifier.
template myType sum(myType a, myType b) // template function.
{
return (a+b);}
};// end of the math class.
int main() // main class.
{
math obj; // creating object in math class.
float a, b;
string type="int";
cout << "Please Enter the two values\n";
cin >>a>>b;

cout <<> (a,b)<< endl;
// calling template function. we can pass any type here
system("pause"); // system pause function.
return 1; // return statement.
}