class template
class template
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.
A class template starts with the keyword template followed by template parameter(s)
inside <> which is followed by the class declaration.
class className {
private:
T var;
... .. ...
public:
T functionName(T arg);
... .. ...
};
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.
Once we've declared and defined a class template, we can create its objects in other classes
or functions (such as the main() function) with the following syntax:
className<dataType> classObject;
For example,
className<int> classObject;
className<float> classObject;
className<string> classObject;
#include <iostream>
// Class template
class Number {
private:
// Variable of type T
T num;
public:
T getNum() {
return num;
};
int main() {
Number<int> numberInt(7);
Number<double> numberDouble(7.7);
return 0;
Output
int Number = 7
double Number = 7.7
In this program. we have created a class template Number with the code;
class Number {
private:
T num;
public:
Number(T n) : num(n) {}
};
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.
Number<int> numberInt(7);
Number<double> numberDouble(7.7);
This creates a class definition each for int and float, which are then used accordingly.
It is a good practice to specify the type when declaring objects of class templates.
Otherwise, some compilers might throw an error.
//Error
Number numberInt(7);
Number numberDouble(7.7);
Suppose we need to define a function outside of the class template. We can do this with the
following code:
class ClassName {
... .. ...
// Function prototype
returnType functionName();
};
// Function definition
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.
class Number {
... .. ...
// Function prototype
T getnum();
};
// Function definition
T Number<T>::getNum() {
return num;
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;
class Calculator {
private:
T num1, num2;
public:
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() {
intCalc.displayResult();
floatCalc.displayResult();
return 0;
Output
Int results:
Numbers: 2 and 1.
2+1=3
2-1=1
2*1=2
2/1=2
Float results:
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.
C++ Class Templates With Multiple Parameters
In C++, we can use multiple template parameters and even use default arguments for those
parameters. For example,
class ClassName {
private:
T member1;
U member2;
V member3;
... .. ...
public:
... .. ...
};
#include <iostream>
class ClassTemplate {
private:
T var1;
U var2;
V var3;
public:
void printVar() {
};
int main() {
obj1.printVar();
obj2.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.
class ClassTemplate {
// code
};
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;
... .. ...
... .. ...
};
Explain