8. Destructor

Your task is to define a class named Device with:

  • constructor that prints:
    Device created
  • destructor that prints:
    Device destroyed

The program will:

  1. Create a Device object.
  2. When the program ends, the destructor runs automatically.
     

Example

Output:

Device created
Device destroyed

Why this output?

  • The constructor runs when the object is created.
  • The destructor runs automatically when the object goes out of scope (end of main()).
     

Question Significance

Teaches both constructors and destructors, showing how C++ manages object lifecycles automatically.

 

Loading...

Input

Expected Output

Device created Device destroyed