6.7 CONSTRUCTORS WITH DEFAULT ARGUMENTS

It is possible to have a constructor with default arguments.. It means that if the constructor is defined with n parameters, we can invoke it with less than n arguments specified in the call.

If we define constructor for class Anyname as

Anyname ( para1,para2,para3,para4 =val4, … paran = valn);

where para1, para2 are symbolic representations of parameters, then we can invoke this constructor as Anyname(para1,para2,para3);

Let us take a concrete example.

Date(int d , int m , int y = 2002);

This construct has three parameters. Parameter number three is initialized with a value 2002. Now we can call the constructor in two possible ways.

  1. Date (15, 8, 1947);
  2. Date (15, 8);

The first constructor will initialize ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.