Question: What is Template Class in C++: Template Class is an important feature of C++ which enables the coder to write generic functions or classes. In

What is ‘Template Class’ in C++:
“Template Class” is an important feature of C++ which enables the coder to write generic functions or classes.
In a generic function or class, the type of data (i.e: int, float, double, etc.) upon which the function or class
operates is specified as a parameter.
Why ‘Template Class’?
By creating a templated class/ function, you can define the nature of your algorithm to be independent of any
kind of data types.
Once you have written a templated code, your compiler will automatically generate the correct code for the type
of data that is actually used when you execute the function.
Format for writing a ‘Template Class’ in C++
Remember the simple DynamicArray class we discussed in our class. If we convert that simple class into a
templated class, then that class object will be able to hold any valid type of numeric values (int, float, double).
Now, the format for writing a template function in C++ (in the source .cpp file) is as follows:
template
return-type Class_Name::functionName(parameters)
{
// your code goes here
}
Modify the header file and the source file given below so that they now work as template class (the
array elements in the dynamically allocated memory can be any type as the user defines).

" v:shapes="Picture_x0020_39">




Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!