Question OOP-CTOR-0007
EasyWhat is the output of this code?
#include <iostream>
using namespace std;
class Demo {
public:
Demo() { cout << "Created "; }
~Demo() { cout << "Destroyed"; }
};
int main() {
Demo obj;
return 0;
}
- A.
Destroyed
- B.
Created
- C.
Show answer and explanation
Answer: C
The constructor runs when obj is created, and the destructor runs automatically when obj goes out of scope at the end of main.