Loading practice questions
What will be the output of the following code?
class A
{
int i;
public:
A(int n)
{
i=n; cout<<"inside constructor ";
}
~A()
{
cout<<"destroying "<<i;
}
void seti(int n)
{
i=n;
}
int geti()
{
return i;
}
};
void t(A ob)
{
cout<<"something ";
}
int main()
{
A a(1);
t(a);
cout<<"this is i in main ";
cout<<a.geti();
}
Correct Answer: D — inside constructor something destroying 2this is i in main destroying 1
Explanation:
Although the object constructor is called only ones, the destructor will be called twice, because of destroying the copy of the object that is temporarily created. This is the concept of how the object should be passed and manipulated.