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

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.