Program Demonstrates Constructor With No Parameter In Base Class And Parametrized Constructor In Derived Class In C++

LEVEL: Beginner

Whats in this post?

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

Code


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


Background and Explanation of the code


In this program, the statement derv d( 10, 15 ); in the main ( ) first invokes the constructor with two parameters derv (int, int) of the derived class which in turn implicitly invokes the base’s no parameter constructor base ( ) which is then executed.Then the derived constructor is executed which initialize passed values to its data members.

OUTPUT

Base’s no parameter constructor

Derive’s constructor with two parameters