Home Segments Index Top Previous Next

492: Mainline

When you want a constructor in a derived class to hand arguments to an argument-bearing constructor in a base class, you modify the derived-class constructor's definition by sandwiching base-class constructor calls between its argument list and its body, and you signal your intention to do this sandwiching with a colon.

For example, if you want the default box_car constructor to supply arguments for the three-argument box constructor, you modify the box_car default constructor as follows:

Derived-class default constructor 
 |       *----- No arguments, as before 
 |       |  *----- Colon marking presence of a call 
 |       |  |      to a base-class constructor 
 |       |  |  *----- Name of the base-class constructor 
 |       |  |  |    *----- Arguments for the base-class constructor 
 |       |  |  |    |                *----- Empty body 
 |       |  |  |    v                | 
 v       v  v  v   ---------------   v 
box_car ( ) : box (10.5, 9.5, 40.0) { } 

You should compare this constructor-calling default constructor with the previous member-variable-assigning version:

box_car ( ) { 
  height = 10.5; width = 9.2; length = 40.0; 
}