6.2 CONSTRUCTORS
To initialize an object, C++ uses a special method called the constructor. We should declare this method in the class itself. Please note that in Program 6.1, we have not defined any constructor. The compiler supplied it on its own. Therefore, it is called as system supplied constructor. This constructor can also be termed as do-nothing constructor. Such constructors do not initialize data members. Therefore, values of these members are arbitrary. Since such a situation is not acceptable, (in general) we have to define constructors ourselves.
There are two restrictions regarding constructors. Firstly, the name of the constructor has to be the name of the class. Secondly, it cannot have any return type, not even the void.
Let ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access