107. Destructor Order Inheritance

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

You must write a C++ program that:

  • Defines a base driver class with a destructor that prints:

    Base driver destroyed
    
  • Defines a derived driver class that:
    • Inherits publicly from the base driver
    • Has a destructor that prints:

      Derived driver destroyed
      
  • The program must prove that:
    • The derived class destructor executes first
    • The base class destructor executes after

Input:

  • One integer value N

(Input is only used to keep the program structure consistent; it does not affect destruction order.)

Program Flow (Mandatory Execution Order):

  1. Read integer N
  2. Create a derived driver object
  3. Base driver constructor executes
  4. Derived driver constructor executes
  5. End of main() scope
  6. Derived driver destructor executes
  7. Base driver destructor executees

 

Example Input:

5

Example Output:

Base driver initialized
Derived driver initialized
Derived driver destroyed
Base driver destroyed

 

Constraints:

  • Use class inheritance only
  • Derived class must inherit from base class
  • Do NOT use:
    • Virtual destructors
    • Virtual functions
    • Dynamic memory allocation (new, malloc)
    • Composition (no base object as member)
  • Object must be destroyed automatically at scope exit
  • Output text and order must match exactly
  • Use only standard input and output

 

 

 

Loading...

Input

5

Expected Output

Base driver initialized Derived driver initialized Derived driver destroyed Base driver destroyed