Class Templates
Like function templates, you can also create class templates for generic class operations. When a class uses the concept of Template, then the class is known as generic class.
Sometimes, you need a class implementation that is same for all classes, only the data types used are different.
Syntax of Class Template
template <class T>
class className
{
... .. ...
public:
T var;
T someOperation(T arg);
... .. ...
};
In the above declaration, T
is the template argument which is a placeholder for the data type used.
Inside the class body, a member variable var
and a member function someOperation()
are both of type T
.
How to create a class template object
To create a class template object, you need to define the data type inside a < >
when creation.
className<dataType> classObject;
For Example:
className<int> classObject;
className<float> classObject;
className<string> classObject;
Simple calculator using Class template
/**
* Program to add, subtract, multiply and divide two numbers using class template
* File name : ClassTemplate_SimpleCalculator.cpp
**/
#include <iostream>
using namespace std;
template <class T>
class Calculator
{
private:
T num1, num2;
public:
Calculator(T n1, T n2)
{
num1 = n1;
num2 = n2;
}
void displayResult()
{
cout << "Numbers are: " << num1 << " and " << num2 << "." << endl;
cout << "Addition is: " << add() << endl;
cout << "Subtraction is: " << subtract() << endl;
cout << "Product is: " << multiply() << endl;
cout << "Division is: " << divide() << endl;
}
T add() { return num1 + num2; }
T subtract() { return num1 - num2; }
T multiply() { return num1 * num2; }
T divide() { return num1 / num2; }
};
int main()
{
Calculator<int> intCalc(2, 1);
Calculator<float> floatCalc(2.4, 1.2);
cout << "Int results:" << endl;
intCalc.displayResult();
cout << endl << "Float results:" << endl;
floatCalc.displayResult();
return 0;
}
/*
Output :
Int results:
Numbers are: 2 and 1.
Addition is: 3
Subtraction is: 1
Product is: 2
Division is: 2
Float results:
Numbers are: 2.4 and 1.2.
Addition is: 3.6
Subtraction is: 1.2
Product is: 2.88
Division is: 2
*/
In the above program, a class template Calculator
is declared.
The class contains two private members of type T
: num1
& num2
, and a constructor to initialize the members.
It also contains public member functions to calculate the addition, subtraction, multiplication and division of the numbers which return the value of data type defined by the user. Likewise, a function displayResult()
to display the final output to the screen.
In the main()
function, two different Calculator
objects intCalc
and floatCalc
are created for data types: int
and float
respectively. The values are initialized using the constructor.
Notice we use <int>
and <float>
while creating the objects. These tell the compiler the data type used for the class creation.
This creates a class definition each for int
and float
, which are then used accordingly.
Then, displayResult()
of both objects is called which performs the Calculator operations and displays the output.