C++ Programming - Constructors and Destructors - Discussion
Discussion Forum : Constructors and Destructors - Programs (Q.No. 8)
8.
What will be the output of the following program?
#include<iostream.h>
class BixBase
{
public:
BixBase()
{
cout<< "Base OK. ";
}
};
class BixDerived: public BixBase
{
public:
BixDerived()
{
cout<< "Derived OK. ";
}
~BixDerived()
{
cout<< "Derived DEL. ";
}
};
int main()
{
BixBase objB;
BixDerived objD;
objD.~BixDerived();
return 0;
}
Discussion:
13 comments Page 1 of 2.
Abhishek said:
1 year ago
When the object scope is all completed then the destructor is called by the compiler itself.
that means all the memory stored by the object will be released.
But when you explicitly call the destructor with that object name then the compiler again frees the memory of that object, which may corrupt the memory.
that means all the memory stored by the object will be released.
But when you explicitly call the destructor with that object name then the compiler again frees the memory of that object, which may corrupt the memory.
Rishabh said:
6 years ago
@All.
I tried to run the code in turbo C and I was getting error like: Member identifier expected.
Why, can anyone explain me?
I tried to run the code in turbo C and I was getting error like: Member identifier expected.
Why, can anyone explain me?
Achyut Gughane said:
1 decade ago
Guys answer is wrong because when we call destructor explicitly then system doesn't call destructor for that object so correct answer is B.
Unknown said:
1 decade ago
When I ran the same piece of code in Dev C++, destructor of derived class is called only once. I think answer is wrong.
Sammy14 said:
1 decade ago
In oops,when a constructor of derived class is called compiler automatically invokes base class's default constructor (However in java we can use super(a,b) to specify parameterized constructor).
Therefore the virtually the definition of derived class constructor becomes:
BixDerived()
{
BixBase();//Line added By Compiler internally
cout<< "Derived OK. ";
}
Therefore the virtually the definition of derived class constructor becomes:
BixDerived()
{
BixBase();//Line added By Compiler internally
cout<< "Derived OK. ";
}
Arvind said:
1 decade ago
1. Firstly when objD.~BixDerived() is called explicitly.
2. Secondly when the program ends...Destructor will be invoked implicitly.
2. Secondly when the program ends...Destructor will be invoked implicitly.
(1)
Anil said:
1 decade ago
Why derived del is displayed 2 times ?
Pranathi said:
1 decade ago
When base class object is called it invokes the base call constructor (Base OK) , when the derived class object is called first it will call base class constructor then it invokes derived class constructor (Base OK, Derived OK). Destructor invokes in reverse order.
Anonymous said:
1 decade ago
First is when base class constructor is called as(BixBase objB).
Second time when derived class constructor is called as(BixDerived objD) in which base class constructor called again and then derived class consructor called.
Second time when derived class constructor is called as(BixDerived objD) in which base class constructor called again and then derived class consructor called.
Bishu said:
1 decade ago
Couldn't understand. Can anyone please explain why "Base OK" gets print twice as its obj formed only once.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers