106. Constructor Order Inheritance

Your task is to demonstrate constructor execution order in Embedded C++ using inheritance.

You must write a C++ program that:

  1. Defines a base driver class with a constructor that prints:

    Base driver initialized
    
  2. Defines a derived driver class that:
    • Inherits publicly from the base driver
    • Has a constructor that:
      • Receives an integer value
      • Prints:

        Derived driver initialized
        
    • Stores the received integer
    • Provides a function to print:

      Driver value <value>
      
  3. In main():
    • Read one integer from standard input
    • Create one object of the derived driver
    • Call the derived driver function to print the value

The program output must prove that the base class constructor executes before the derived class constructor.

Input:

  • One integer value N

Program Flow (Mandatory Execution Order):

  1. Read integer N
  2. Create a derived driver object using N
  3. Base driver constructor prints its message
  4. Derived driver constructor prints its message
  5. Derived driver prints N

 

Example Input:

5

Example Output:

Base driver initialized
Derived driver initialized
Driver value 5 

 

Constraints:

  • Use class inheritance only
  • Derived class must inherit from base class
  • Do NOT use:
    • Virtual functions
    • Dynamic memory allocation (new, malloc)
    • Composition (no member object of base class)
  • Output text and order must match exactly
  • Use only standard input and output

 

 

Loading...

Input

5

Expected Output

Base driver initialized Derived driver initialized Driver value 5