What is a virtual destructor? Explain the use of it.

Go down

What is a virtual destructor? Explain the use of it.

Post by Admin on Tue Feb 17, 2015 11:34 am



If the destructor in the base class is not made virtual, then an object that might have been declared of type base class and instance of child class would simply call the base class destructor without calling the derived class destructor.

Hence, by making the destructor in the base class virtual, we ensure that the derived class destructor gets called before the base class destructor.

class a
{
public:
a(){printf("\nBase Constructor\n");}
~a(){printf("\nBase Destructor\n");}
};

class b : public a
{
public:
b(){printf("\nDerived Constructor\n");}
~b(){printf("\nDerived Destructor\n");}
};
int main()
{
a* obj=new b;
delete obj;
return 0;
}

Output:
Base Constructor
Derived Constructor
Base Destructor

By Changing
~a(){printf("\nBase Destructor\n");}
to
virtual ~a(){printf("\nBase Destructor\n");}

Output:
Base Constructor
Derived Constructor
Derived Destructor
Base Destructor

Admin
Admin

Posts : 501
Join date : 2015-02-07

View user profile http://seo-expert.editboard.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum
SEO Forum,Dofollow Forum,High PR Forum,startwithclck,Submit a guest post,Best 5 Google Adsense Alternatives for bloggers,Most Common Advantages & Disadvantages of Guest Blogging,Apply These 12 Secret Techniques To Improve On page Seo Factor,Bluehost Review: Shared Hosting for WordPress,Bluehost Review,Bluehost Reviews,bluehost hosting,godaddy, godaddy hosting, godaddy hosting review,godaddy review, godaddy coupons, godaddy promo code, godaddy coupon, godaddy promo codes, godaddy coupon code, IT Company indiaIT Company SuratIT Company MumbaiIT Company PuneIT CompanyIT Companies indiaIT Companies SuratIT Companies MumbaiIT Companies PuneIT Companies SEO Company indiaSEO Company SuratSEO Company MumbaiSEO Company PuneSEO CompanySEO Companies indiaSEO Companies SuratSEO Companies MumbaiSEO Companies PuneSEO Companies AhmedabadSEO CompaniesDigital Marketing CompanyDigital Marketing Company indiaDigital Marketing Company SuratDigital Marketing Company MumbaiDigital Marketing Company PuneDigital Marketing CompaniesDigital Marketing Companies indiaAffordable Search Engine Optimization Companysearch engine optimisation companiessearch engine optimisation companySeo marketing CompanyIndian SEO CompanyCheap seo companies Best seo companies Seo marketing companiesIndian SEO companiesCheap seo CompanyBest seo CompanyWebsite Optimization CompaniesWebsite Optimization CompanySeo optimization companySeo optimization Companiesdigital marketing agencySeo consultantSeo consultantsSeo consultingSeo consultancySearch engine optimization expertsSeo agencySeo indiaInternet marketing companyTop seo companySeo ServicesAffordable Search Engine Optimization servicesSeo marketing servicesSearch engine optimization and marketingCheap seo serviceBest seo service Cheap seo services Best seo services Digital marketing serviceDigital marketing servicesSeo analysisSeo strategySearch engine optimisation servicesProfessional seo servicesService seoOrganic seo servicesGuaranteed seo servicesMonthly seo servicesServices seoSearch marketing strategyLocal seo servicesLocal seo serviceProfessional seo servicesLocal seo services companySeo company servicesTop local seo companiesTop local seoTop local seo companyLocal seo agencyLocal seo agenciesLocal seo consultantLocal search engine optimisationLocal online marketingLocal search engine marketingSEO trainingSEO training SuratSEO training NavsariSEO training ValsadSEO training AnkleshwarSEO training BharuchInternet Marketing Services Internet Marketing Services SuratInternet Marketing Services NavsariInternet Marketing Services BharuchInternet Marketing Services VadodaraInternet Marketing Services Ankleshwar