Destructors
Question 1 |
Give the output
#include
using namespace
class Base1
{
public:
~Base1()
{
cout<<"Base1's destructor"<
};
class Base2
{
public:
~Base1()
{
cout<<"Base2's destructor"<
};
class Derived : public Base1,public Base2
{
public:
~Derived()
{
cout<<"Derived's destructor"<
}
int main()
{
Derived d;
return 0;
}
#include
using namespace
class Base1
{
public:
~Base1()
{
cout<<"Base1's destructor"<
class Base2
{
public:
~Base1()
{
cout<<"Base2's destructor"<
class Derived : public Base1,public Base2
{
public:
~Derived()
{
cout<<"Derived's destructor"<
int main()
{
Derived d;
return 0;
}
Base1'1 destructor Base2'2 destructor Derived Destructor | |
Derived Destructor Base2'2 destructor Base1'1 destructor | |
Derived Destructor | |
Compiler Dependent |
Question 1 Explanation:
● C++ constructor call order will be from top to down that is from base class to derived class and c++ destructor call order will be in reverse order.
● First child class and later parent class.
● First child class and later parent class.
Question 2 |
In which case it is mandatory to provide a destructor in a class?
Almost in every class | |
Class for which two or more than two objects will be created | |
Class for which copy constructor | |
Class whose objects will be created dynamically |
Question 2 Explanation:
→ Destructors are used to de-allocate the memory that has been allocated for the object by the constructor.
→ Unlike constructor a destructor neither takes any arguments nor does it returns value. And destructor can’t be overloaded.
→ Unlike constructor a destructor neither takes any arguments nor does it returns value. And destructor can’t be overloaded.
There are 2 questions to complete.