In Chapter 9, you saw how to define a class-object
variable in one statement and to initialize the corresponding
class object's member variables in others. The following illustrates
class-object declaration and initialization for a tank_car
variable, t
:
tank_car t; t.radius = 3.5; t.length = 40.0;
For many classes, member variables have predictable values, at least initially. Accordingly, C++ allows you to define a special member function, the default constructor, which is called automatically whenever a new class object is created. Such constructors enable you to initialize the member variables in new class objects.