C++ Class & Function Templates
C++ Class & Function Templates
In this tutorial, we will learn about class templates in C++ with the help of examples.
Templates are powerful features of C++ which allows us to write generic programs. There are
two ways we can implement templates:
• Function Templates
• Class Templates
Similar to function templates, we can use class templates to create a single class to work with
different data types.
Class templates come in handy as they can make our code shorter and more manageable.
In the above declaration, T is the template argument which is a placeholder for the data type
used, and class is a keyword.
Inside the class body, a member variable var and a member function functionName() are both of
type T .
className<dataType> classObject;
For example,
className<int> classObject;
className<float> classObject;
className<string> classObject;
Example 1: C++ Class Templates
// C++ program to demonstrate the use of class templates
#include <iostream>
using namespace std;
// Class template
template <class T>
class Number {
private:
// Variable of type T
T num;
public:
Number(T n) : num(n) {} // constructor
T getNum() {
return num;
}
};
int main() {
return 0;
}
Output
int Number = 7
double Number = 7.7
In this program. we have created a class template Number with the code
public:
Number(T n) : num(n) {}
T getNum() { return num; }
};
Notice that the variable num , the constructor argument n , and the function getNum() are of
type T , or have a return type T . That means that they can be of any type.
In main() , we have implemented the class template by creating its objects
Number<int> numberInt(7);
Number<double> numberDouble(7.7);
//Error
Number numberInt(7);
Number numberDouble(7.7);
// Function definition
template <class T>
returnType ClassName<T>::functionName() {
// code
}
Notice that the code template <class T> is repeated while defining the function outside of the
class. This is necessary and is part of the syntax.
If we look at the code in Example 1, we have a function getNum() that is defined inside the
class template Number .
// Function definition
template <class T>
T Number<T>::getNum() {
return num;
}
Example 2: Simple Calculator Using Class Templates
This program uses a class template to perform addition, subtraction, multiplication and
division of two variables num1 and num2 .
The variables can be of any type, though we have only used int and float types in this example.
#include <iostream>
using namespace std;
public:
Calculator(T n1, T n2) {
num1 = n1;
num2 = n2;
}
void displayResult() {
cout << "Numbers: " << num1 << " and " << num2 << "." << endl;
cout << num1 << " + " << num2 << " = " << add() << endl;
cout << num1 << " - " << num2 << " = " << subtract() << endl;
cout << num1 << " * " << num2 << " = " << multiply() << endl;
cout << num1 << " / " << num2 << " = " << divide() << endl;
}
int main() {
Calculator<int> intCalc(2, 1);
Calculator<float> floatCalc(2.4, 1.2);
return 0;
}
Output
Int results:
Numbers: 2 and 1.
2+1=3
2-1=1
2*1=2
2/1=2
Float results:
Numbers: 2.4 and 1.2.
2.4 + 1.2 = 3.6
2.4 - 1.2 = 1.2
2.4 * 1.2 = 2.88
2.4 / 1.2 = 2
The class contains two private members of type T : num1 & num2 , and a constructor to initialize
the members.
We also have add() , subtract() , multiply() , and divide() functions that have the return type T . We
also have a void function displayResult() that prints out the results of the other functions.
In main() , we have created two objects of Calculator : one for int data type and another
for float data type.
This prompts the compiler to create two class definitions for the respective data types during
compilation.
public:
ClassTemplate(T v1, U v2, V v3) : var1(v1), var2(v2), var3(v3) {} // constructor
void printVar() {
cout << "var1 = " << var1 << endl;
cout << "var2 = " << var2 << endl;
cout << "var3 = " << var3 << endl;
}
};
int main() {
// create object with int, double and char types
ClassTemplate<int, double> obj1(7, 7.7, 'c');
cout << "obj1 values: " << endl;
obj1.printVar();
return 0;
}
Output
obj1 values:
var1 = 7
var2 = 7.7
var3 = c
obj2 values:
var1 = 8.8
var2 = a
var3 = 0
In this program, we have created a class template, named ClassTemplate , with three parameters,
with one of them being a default parameter.
Notice the code class V = char . This means that V is a default parameter whose default type
is char .
Inside ClassTemplate , we declare 3 variables var1 , var2 and var3 , each corresponding to one of the
template parameters.
class ClassTemplate {
private:
T var1;
U var2;
V var3;
... .. ...
... .. ...
};
Here,
Object T U V
In the above code, T is a template argument that accepts different data types ( int , float , etc.),
and typename is a keyword.
When an argument of a data type is passed to functionName() , the compiler generates a new
version of functionName() for the given data type.
Calling a Function Template
Once we've declared and defined a function template, we can call it in other functions or
templates (such as the main() function) with the following syntax
functionName<dataType>(parameter1, parameter2,...);
We can then call it in the main() function to add int and double numbers.
int main() {
int result1;
double result2;
// calling with int parameters
result1 = add<int>(2, 3);
cout << result1 << endl;
return 0;
}
int main() {
int result1;
double result2;
// calling with int parameters
result1 = add<int>(2, 3);
cout << "2 + 3 = " << result1 << endl;
return 0;
}
Output
2 + 3 = 5
2.2 + 3.3 = 5.5