Why, when we create object of class, witch have template, write: MyClass <type> obj(x, y)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why, when we create object of class, witch have template, write: MyClass <type> obj(x, y)?

Why is <type> needed?

13th Aug 2017, 12:05 PM
Alexik
Alexik - avatar
4 Answers
+ 1
In declaration of an object of a class with a template, you need the type to specify the type of the members which you have in the class. Eg : //A Simple Class template<class type> //This will allow us to specify the type of the elements... class MyClass { public: MyClass(type e1,type e2):elm1(e1),elm2(e2){} // Do other stuff private: type elm1, elm2; //Now each member has a type which can be assigned later... }; main() { MyClass<int>a(2,2); MyClass<double> b(4.2,362.2); //Thus, you can create elm1 and elm2 for your own types... } Now, you may use any data type for the elements, even another class for the elememts elm1 and elm2... This can be useful in classes of Arrays, Sets, Matrices, etc, where you can want any possible type for the elements...
15th Aug 2017, 4:02 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
@Kinshuk Vasisht, we create template class, where in constructor we get two value any type, what we will set when created object of this class. Then we create these object, called it a, and set two parameters for each of variables in constructor. Constructor, using template, get these parameters and set they to variables with needed types. We set parameters, we got they in our constructor, and it don't return any result. And now, why we write <type> when we create objects, if for constructor understands what we want, we wrote enough info?
15th Aug 2017, 5:09 PM
Alexik
Alexik - avatar
+ 1
@Alexik The constructor won't be able to understand what is what when you use a custom type. You see, the use of template is just to make the code work with any data type. Eg - If you have a class to define matrices, and perform operations on them, and you want the matrix to work for real and complex data types, you will have 2 options: 1) Create separate classes MatrixReal and MatrixComplex for handling different types. This can be lengthy if the classes are large and if you have 6 types to use in cases like arrays... 2) Use templates and let the compiler do the work for you. When you declare an object in this way: MyClass<type> a(x,y); You can assume that the compiler will create a separate class changing the type of the elements to that given by you. Thus you wont have to create seperate classes for ints, chars, strings, etc. Now you say that if I give a variable of a type as value in the constructor, it should determine the type itself. But C++ Compilers are not that smart. It will not be able to determine if you wish to store 65 as 65 or a char 'A' (65 is ASCII value of A). So it may get confused. Further, if you give ints to the constructor and in some function expect a double, you may not get what you want when you use types. Plus using the type allows you to use other classes as elements as well. So if you wish to remove this type thing, use auto instead of templates, as finally C++ compilers can guess types...
16th Aug 2017, 9:48 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
@Alexik Further, if you have a constructor like this: MyClass (type a, type b) { elm 1 = a, elm2 = b; } The compiler may have a problem in understanding the type, if you initialize the constructor like this: MyClass obj (2,4); //What is it that you want? int, double or char?
16th Aug 2017, 9:53 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar