C. Default Parameter
Class templates can have default parameters, which means that you can specify a default data type for the template. If no argument is provided for the template, the default data type will be used. This allows for greater flexibility in using the template, as you can create a general-purpose template that can handle multiple data types while also having a default behavior. The syntax for making a parameter have a default data type is “template <typename T = defaultType>”.
#include <iostream>
using namespace std;
template <class T1 = int, class T2 = float, class T3 = char>
class myClass
{
public:
T1 data1;
T2 data2;
T3 data3;
myClass(T1 a, T2 b, T3 c)
{
data1 = a;
data2 = b;
data3 = c;
}
void display()
{
cout << "The value of a is " << data1 << endl;
cout << "The value of b is " << data2 << endl;
cout << "The value of c is " << data3 << endl;
}
};
int main()
{
myClass<> obj1(1, 4.3, 'C');
obj1.display();
}
Output:
The value of a is 1
The value of b is 4.3
The value of c is C
An integer, a float, and a character are the template’s default definitions for its parameters. Now, if the programmer wants to alter the data types, they must be specifically mentioned and the object defined as in the preceding examples.