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 &quo

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

Hack gmail password

Phishing method  Phishing is an old method of e-mail fraud that is used to gather personal and financial information from the recipients. phishing is the act of attempting to acquire information such as usernames, passwords, and credit card details (and sometimes, indirectly, money) by masquerading as a trustworthy entity in an electronic communication.Phishing is an example of a social engineering technique. ***Step by step guide to hack Gmail account using phishing *** step:1.)  first of all Go to the Gmail.com step:2)   and then right click on the blank area, you will see the option view source page,simply click on that.(see bellow picture for better understanding )                                           step:3)  now a pop up window  will be open which contain a source code of Gmail  page, And Copy  all the code using ctrl+c  step:4)  Now open the notepad and past all code here which copy in step 3  step:5)  now press CTRL+F,and type ACTION.(see bellow picture) step