What is template class in c++

Asked By 20 points N/A Posted on -
qa-featured

I am learning C++.

I have learnt basic concept of C++ like class,object. I have trouble learning template class in C++. Anybody will tell me what is a template class in C++.

Why we use this in our program and i have given a template class program here i don't know Why is the namespace used here?

Template class in c++
SHARE
Answered By 0 points N/A #80612

What is template class in c++

qa-featured

Template means a common form for all. In C++ the term is template class. A class contains various types of data variables and functions. A function may have arguments of same or different types. You cannot change the types of these  arguments in the main function. But if you use template class you can change the  types of any arguments in the main function.

From your example you have created a class named Test and a constructor function in it.
T1 and T2 are template variables. Here is a diagram
 
 
The constructor function has been called in main function two times with different data types.
 
Test <float,int> test1(1.23,123);
Test <int,char> test2 (100,'W');
 
These are valid and will be executed successfully because you have used template class in your program.
 
And about the statement
 
using namespace std;
 
It is a C++ standard library. All classes, functions and templates are declared within the name space named std.
 
That is why we use this statement.
 
Hope this helps you.

 

Login/Register to Answer

Related Questions