Skip to main content

How to work with virtual constructors and destructors in C++

C++ allows programmers to create virtual destructors. But, it doesn’t allow virtual constructors to be created because of various reasons. To know why a virtual destructor is needed, consider the following program:

#include <iostream>

using namespace std;

class A

{

public:

A()

{

cout<<“A’s constructor”<<endl;

}

~A()

{

cout<<“A’s destructor”<<endl;

}

};

class B : public A

{

public:

B()

{

cout<<“B’s constructor”<<endl;

}

~B()

{

cout<<“B’s destructor”<<endl;

}

};

int main()

{

A *bptr = new B();

delete bptr;

return 0;

}

Output for the above program is as follows:

A‘s constructor

B’s constructor

A‘s destructor

 

From the above output you can see that derived class destructor didn’t execute. This might lead to problems like memory leakage. To avoid such problems we make destructor in base class as virtual destructor.

 

Virtual destructor instructor ensures that the derived class destructor is executed. To create a virtual destructor, precede the destructor definition with virtualkeyword. Following program demonstrates a virtual destructor:

#include <iostream>

using namespace std;

class A

{

public:

A()

{

cout<<“A’s constructor”<<endl;

}

virtual ~A()

{

cout<<“A’s destructor”<<endl;

}

};

class B : public A

{

public:

B()

{

cout<<“B’s constructor”<<endl;

}

~B()

{

cout<<“B’s destructor”<<endl;

}

};

int main()

{

A *bptr = new B();

delete bptr;

return 0;

}

Output for the above program is as follows:

A‘s constructor

B’s constructor

B‘s destructor

A’s destructor

 

Now you can see that derived class constructor is also executed.

 

Advantages and Disadvantages of Inheritance

 

Advantages of inheritance are as follows:

Inheritance promotes reusability. When a class inherits or derives another class, it can access all the functionality of inherited class.Reusability enhanced reliability. The base class code will be already tested and debugged.As the existing code is reused, it leads to less development and maintenance costs.Inheritance makes the sub classes follow a standard interface.Inheritance helps to reduce code redundancy and supports code extensibility.Inheritance facilitates creation of class libraries.

 

Disadvantages of inheritance are as follows:

Inherited functions work slower than normal function as there is indirection.Improper use of inheritance may lead to wrong solutions.Often, data members in the base class are left unused which may lead to memory wastage.Inheritance increases the coupling between base class and derived class. A change in base class will affect all the child classes.

Take your time to comment on this article.

Comments

Popular posts from this blog

Hack WiFi password (rooted or non-rooted device)

Hello guyzz!!! Today I am here to share a trick how to hack WiFi password in an android mobile.It works on rooted as well as non-rooted device depending upon you device capability.. How to install: Method One: in order to skip license verification first you must be disconnected from internet when you run this app , it will automaticly connects you or turns your wifi on Method Two: or use lucky patcher for preventing it from license verification check these options in lucky patcher auto mode, other patches, apply patch to delvick cache, back up apk file for reinstaller This app has NO advertisements Instructions: This app has NO advertisements CONTACT ME BEFORE GIVING BAD REVIEW, SO I CAN HELP YOU. The application supports two types of test: - Dictionary test - it tries passwords from predefined list one by one. Please don't be disappointed if the password willnot be found, it simply means that it was not in the dictionary. However, if somebody set his key to ...

Hack a memory card password within minutes

possible ways to break micro sd memory card password hye friends today's topic is very importent .some time we secrate the data in memory card with password but unfortunatly we forget that password which protect our dta from hackers or your friends or any other you guess. so i am here to give you best solution of breaking memory card paasword in only 5 minutes. there are lot off methode publish on internate about how to break the memory card password but many of my reader advise me to write a tutorial about breaking memory card password .becuase they believe that i am write that tutorial very friendly so they can understand very ifficiently. so now i am going to start the tutorial namly how to break memory card password.here i am publish very friendly methode break password so i give only those methode which easily understandable for everyone .so dont wasting your valuable time lets start.follow step by step procedure. solution no. 1:-- BREAKING MEMORY CARD PASSWORD FOR...

Lucky patcher(some basics)

Though this app has many more feature for rooted android, Non-rooted android users can only enjoy following features:- 1)Create modified.apk This feature allows you to modify app. You can create app with custom patch applied (if available), app with ads removed and app with changed permissions and activities and the app will be stored in sd card. Then you have to install this modified app, before this make sure you have checked Unknown sources in your device???s setting. 2)Backup App. You can backup app only(means no data as like in Titanium backup) 3)Hack in app purchases. With this app you can hack only offline games Step 01 : Download Lucky patcher for non-rooted devices from here. Step 02 : Enable the installation from unknown sources by going to settings > security and tap on Unknown sources check-box. Step 03 : Now navigate to folder where you have placed/downloaded the luckypatcher.apk file and install it. Step 04 : Now open lucky patcher app and tap on the ...