What is class template | Sololearn: Learn to code for FREE!

+2

What is class template

class template

c++

4/1/2017 4:43:44 AM

Bhushan Mahire

4 Answers

New Answer

+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....

+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

+2

template allows you use different data types.

0

any simpler explaination