Understanding constructors
Did you notice how convenient it has been for the program examples in this chapter to have an Initialize()
member function for each class
or struct
? Certainly, it is desirable to initialize all data members for a given instance. More so, it is crucial to ensure that data members for any instance have bonafide values, as we know that memory is not provided clean or zeroed-out by C++. Accessing an uninitialized data member, and utilizing its value as if it were bonafide, is a potential pitfall awaiting the careless programmer.
Initializing each data member individually each time a class is instantiated can be tedious work. What if we simply overlook setting a value? What if the values are private
, and are therefore not directly accessible? We have seen that an Initialize()
function is beneficial because once written, it provides a means to set all data members for a given instance. The only drawback is that the programmer must now remember to call Initialize...