C++ Programming - Constructors and Destructors - Discussion
Discussion Forum : Constructors and Destructors - Programs (Q.No. 14)
14.
What will be the output of the following program?
#include<iostream.h>
class BixBase
{
public:
BixBase()
{
cout<< "Base OK. ";
}
virtual ~BixBase()
{
cout<< "Base DEL. ";
}
};
class BixDerived: public BixBase
{
public:
BixDerived()
{
cout<< "Derived OK. ";
}
~BixDerived()
{
cout<< "Derived DEL. ";
}
};
int main()
{
BixBase *basePtr = new BixDerived();
delete basePtr;
return 0;
}
Discussion:
6 comments Page 1 of 1.
Prateek Janaj said:
5 years ago
Constructor and destructor are called default while compile-time, even though didn't declare in the class. In this example, both were declared, therefore no need to call destructor separately, it will be called default.
Rohan said:
8 years ago
Anyone explain in detail?
Alka said:
8 years ago
But we have not used object of derived class. We are calling constructor of derived class using a pointer. Don't we need to call destructor explicitly?
(1)
Mikeross said:
9 years ago
Virtual keyword is used to override the function. Here we are using it to prevent memory leak,
If we don't use virtual then only base class destructor is called not derived class.
But when we use virtual destructor in base class then both the destructors are called in REVERSE ORDER.
If we don't use virtual then only base class destructor is called not derived class.
But when we use virtual destructor in base class then both the destructors are called in REVERSE ORDER.
Nandhini said:
1 decade ago
What is meant by virtual? Please one reply proper answer as soon as possible.
Krunal said:
1 decade ago
If destructor in Base class is NON-VIRTUAL then compiler does NOT call derived class destructor, calls ONLY Base class destructor.
If destructor in Base class is VIRTUAL then compiler calls derived class destructor and then Base class destructor.
Thats it!
If destructor in Base class is VIRTUAL then compiler calls derived class destructor and then Base class destructor.
Thats it!
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers