+ 2

What is class template

class template

1st Apr 2017, 4:43 AM
Bhushan Mahire
Bhushan Mahire - avatar
4 Answers
+ 3
A class template allows you to use the class accordingly for different types. It is just an extension of function templates to class methods... Eg - template<class T> class Ex{ private: T arg1,arg2 public: Ex(T a,T b):arg1(a),arg2(b){}} //in main() Ex<double>a(2,2),b(4,5); // Saved a,b as objects of type double // Can also call Ex<int>c(2,2); Such classes prove useful in Matrices, Complex Numbers, Comparisions, STL Containers like Vectors, Maps, Sets, etc....
1st Apr 2017, 5:01 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Basically, a Template allows you to use a particular function for more than one data type. Thus now if you use templates in a max() function, it will return the greater of strings, integers, doubles and chars, and even any new classes you create... Templates thus are an adavncement to function overloading, just done in a single statement as compared to the earlier way... Similarly, using a template in a class will allow you to use methods inside it with different data types... Like the max method in a class for doubles, strings, integers, other classes, etc
1st Apr 2017, 6:21 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
template allows you use different data types.
1st Apr 2017, 5:46 AM
chris
chris - avatar
0
any simpler explaination
1st Apr 2017, 5:28 AM
Akira Hayashi
Akira Hayashi - avatar