August 2001
Beginner
482 pages
10h 52m
English
Until now, you've been setting the member variables of objects in the body of the constructor. Constructors, however, are created in two stages: the initialization stage followed by the body of the constructor.
Most variables can be set in either stage, either by initializing in the initialization stage or by assigning in the body of the constructor. It is cleaner and often more efficient to initialize member variables at the initialization stage. The following example shows how to initialize member variables:
CAT(): // constructor name and parameters
itsAge(5), // initialization list
itsWeight(8)
{ } // body of constructor
After the closing parentheses on the constructor's parameter list, put a colon. Then put the name ...
Read now
Unlock full access