CopyPastor

Detecting plagiarism made easy.

Score: 0.8357121348381042; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2020-03-26
by Vishnu CS

Original Post

Original - Posted on 2013-04-09
by Tunvir Rahman Tusher



            
Present in both answers; Present only in the new answer; Present only in the old answer;

I will try to give an example of the usage of virtual destructor
#include <iostream> using namespace std; class Base { public: Base(){ cout << "Base Constructor Called\n"; } ~Base(){ cout << "Base Destructor called\n"; } }; class Derived1: public Base { public: Derived1(){ cout << "Derived constructor called\n"; } ~Derived1(){ cout << "Derived destructor called\n"; } }; int main() { Base *b = new Derived1(); delete b; }
The above code output the following:
Base Constructor Called Derived constructor called Base Destructor called
The construction of derived object follow the construction rule but when we delete the "b" pointer(base pointer) we have found that only the base destructor is called. But this must not be happen. To do the appropriate thing, we have to make the base destructor virtual. Now let see what happens in the following:
#include <iostream> using namespace std; class Base { public: Base(){ cout << "Base Constructor Called\n"; } virtual ~Base(){ cout << "Base Destructor called\n"; } }; class Derived1: public Base { public: Derived1(){ cout << "Derived constructor called\n"; } ~Derived1(){ cout << "Derived destructor called\n"; } }; int main() { Base *b = new Derived1(); delete b; }
The output changed as following:
Base Constructor Called Derived Constructor called Derived destructor called Base destructor called
So the destruction of the base pointer (which takes an allocation on derived object!) follows the destruction rule, i.e first the Derived, then the Base. On the other hand, there is nothing like a virtual constructor.
A virtual constructor is not possible but virtual destructor is possible. Let us experiment....

#include <iostream>
using namespace std;
class Base { public: Base(){ cout << "Base Constructor Called\n"; } ~Base(){ cout << "Base Destructor called\n"; } };
class Derived1: public Base { public: Derived1(){ cout << "Derived constructor called\n"; } ~Derived1(){ cout << "Derived destructor called\n"; } };
int main() { Base *b = new Derived1(); delete b; }
The above code output the following:
<!-- language: lang-none -->
Base Constructor Called Derived constructor called Base Destructor called
The construction of derived object follow the construction rule but when we delete the "b" pointer(base pointer) we have found that only the base destructor is called. But this must not be happen. To do the appropriate thing, we have to make the base destructor virtual. Now let see what happens in the following:

#include <iostream>
using namespace std;
class Base { public: Base(){ cout << "Base Constructor Called\n"; } virtual ~Base(){ cout << "Base Destructor called\n"; } };
class Derived1: public Base { public: Derived1(){ cout << "Derived constructor called\n"; } ~Derived1(){ cout << "Derived destructor called\n"; } };
int main() { Base *b = new Derived1(); delete b; }
The output changed as following:
<!-- language: lang-none -->
Base Constructor Called Derived Constructor called Derived destructor called Base destructor called
So the destruction of the base pointer (which takes an allocation on derived object!) follows the destruction rule, i.e first the Derived, then the Base. On the other hand, there is nothing like a virtual constructor.

        
Present in both answers; Present only in the new answer; Present only in the old answer;