Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

C++ | Destructors | Question 2

  • Difficulty Level : Medium
  • Last Updated : 12 Oct, 2021

Predict the output of following C++ program

Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.




#include <iostream>
using namespace std;
   
int i;
   
class A
{
public:
    ~A()
    {
        i=10;
    }
};
   
int foo()
{
    i=3;
    A ob;
    return i;
}
   
int main()
{
    cout << foo() << endl;
    return 0;
}

(A) 0
(B) 3
(C) 10
(D) None of the above


Answer: (B)

Explanation: While returning from a function, destructor is the last method to be executed. The destructor for the object “ob” is called after the value of i is copied to the return value of the function. So, before destructor could change the value of i to 10, the current value of i gets copied & hence the output is i = 3. See this for more details.

Quiz of this Question

My Personal Notes arrow_drop_up
Recommended Articles
Page :