Program Demonstrates Parameterized Constructors In Both The Base And Derived Classes In C++

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code


To copy code: Click View Source “View Source” on the right of this code snippet.

Background and Explanation of the code

The statement;

derv d (10, 20);

in main ( ) invokes the constructor with two parameters of derv class which passes the values 10 and 20 to the formal arguments bb and aa respectively.The constructor with two parameters

derv (int bb, int aa) : base (aa)

passes the argument aa to its base class single parameter constructor to initialize its data member a by explicitly invoking it.After its execution , the control returns back to the derive constructor with two parameters which then initialize its own data member b with value stored in bb.Finally the values are displayed.


Base’s single parameter constructor

Derived’s two parameter constructor

a  =  20      b  =   10