Here we gave easiest understanding program to you.
All programs easy to learn every person.
And all subjects are compulsory in BCA.
Powered by Blogger.

Followers

Thursday, 12 September 2019

Virtual Destructor in C++

0 comments

* Virtual Destructor:-

Deleting a derived class object using a pointer to a base class that has a non-virtual destructor results in undefined behavior. To correct this situation, the base class should be defined with a virtual destructor. For example, following program results in undefined behavior.



// CPP program without virtual destructor 
// causing undefined behavior 
#include<iostream> 
using namespace std; 
class base { 
public: 
 base()  
 { cout<<"Constructing base \n"; } 
 ~base() 
 { cout<<"Destructing base \n"; }  
}; 
class derived: public base { 
public: 
 derived()  
 { cout<<"Constructing derived \n"; } 
 ~derived() 
 { cout<<"Destructing derived \n"; } 
}; 
int main(void) 
derived *d = new derived(); 
base *b = d; 
delete b; 
getchar(); 
return 0; 
input/output:-

Constructing base
Constructing derived
Destructing base



Making base class destructor virtual guarantees that the object of derived class is destructed properly, i.e., both base class and derived class destructors are called. For example,


// A program with virtual destructor 
#include<iostream> 
using namespace std; 

class base { 

public: 
 base()  
 { cout<<"Constructing base \n"; } 
 virtual ~base() 
 { cout<<"Destructing base \n"; }  
}; 

class derived: public base { 

public: 
 derived()  
 { cout<<"Constructing derived \n"; } 
 ~derived() 
 { cout<<"Destructing derived \n"; } 
}; 

int main(void) 


derived *d = new derived(); 
base *b = d; 
delete b; 
getchar(); 
return 0; 



* Input/output:-


Constructing base
Constructing derived
Destructing derived
Destructing base











Pure virtual destructor in C++

* Can a destructor be pure virtual in C++?

Yes, it is possible to have pure virtual destructor. Pure virtual destructors are legal in standard C++ and one of the most important things to remember is that if a class contains a pure virtual destructor, it must provide a function body for the pure virtual destructor. You may be wondering why a pure virtual function requires a function body. The reason is because destructors (unlike other functions) are not actually ‘overridden’, rather they are always called in the reverse order of the class derivation. This means that a derived class’ destructor will be invoked first, then base class destructor will be called. If the definition of the pure virtual destructor is not provided, then what function body will be called during object destruction? Therefore the compiler and linker enforce the existence of a function body for pure virtual destructors.
Consider the following program:
#include <iostream> 
class Base 
public: 
 virtual ~Base()=0; // Pure virtual destructor 
}; 
class Derived : public Base 
public: 
 ~Derived() 
 
  std::cout << "~Derived() is executed"; 
 
}; 
int main() 
 Base *b=new Derived(); 
 delete b; 
 return 0; 
}
* Input/output:-


The linker will produce error in the above program.


Now if the definition for the pure virtual destructor is provided, then the program compiles & runs fine.



#include <iostream> 

class Base 


public: 

 virtual ~Base()=0; // Pure virtual destructor 

}; 

Base::~Base() 
 std::cout << "Pure virtual destructor is called"; 


class Derived : public Base 
public: 
 ~Derived() 
 
  std::cout << "~Derived() is executed\n"; 
 
}; 


int main() 
 Base *b = new Derived(); 
 delete b; 
 return 0; 
}


* Input/output:-



~ Derived() is executed

Pure virtual destructor is called

No comments:

Post a Comment