0

Class Templates..

Why in the slide 2/5 of class templates lesson, the first class is "private" and the second "public"?

12th Aug 2018, 7:01 AM
Baldemar Aguirre
2 Réponses
+ 3
What you see in that slide is a template class example. - template <class T> here is used to introduce a template, we use template so we can create a Pair instance containing a couple (2 items) of a similar class, by using template we can have a Pair of int, float, double, object or other things without having to create a Pair class specific for each type. template <class T> class Pair { - private: defines whatever follows are all private members. "first" and "second" member are private, they can't be accessed directly, a getter/setter method is needed to manipulate private members. private: T first, second; - public: defines whatever follows are all public members. Public members are accessible directly without a need of getter/setter. A constructor declaration is written here, where private members "first" and "second" are assigned a value, using constructor initializer, "first" is assigned "a", and "second" is assigned "b". public: Pair (T a, T b): first(a), second(b) { } };
12th Aug 2018, 8:17 AM
Ipang
+ 1
Baldemar Aguirre are you taking about this one ? https://www.sololearn.com/learn/CPlusPlus/1916/ I mean variable first and second as private ?
12th Aug 2018, 7:05 AM
Ketan Lalcheta
Ketan Lalcheta - avatar