C++ calling base class constructors

The short answer for this is, “because that’s what the C++ standard specifies”.

Note that you can always specify a constructor that’s different from the default, like so:

class Shape  {

  Shape()  {...} //default constructor
  Shape(int h, int w) {....} //some custom constructor


};

class Rectangle : public Shape {
  Rectangle(int h, int w) : Shape(h, w) {...} //you can specify which base class constructor to call

}

The default constructor of the base class is called only if you don’t specify which one to call.

Leave a Comment