CoolInterview.com - World's Largest Collection of Interview Questions
 Interview Questions  
 Our Services  

Get 9,000 Interview Questions & Answers in an eBook.


  • 9500+ Pages
  • 9000 Question & Answers
  • All Tech. Categories
  • 14 MB Content

    Get it now !!


    Send your Resume to 6000 Companies


  • INTERVIEW QUESTIONS LANGUAGES C++ DETAILS
    Question :
    What Is The Use of VirtualDestructer?
    Contact   Contact
    Category C++ Interview Questions
    Rating (4.5) By 4 users
    Added on 9/12/2007
    Views 6452
    Rate it!
    Answers:

    Virtual destructor is used for the polymorphic decomposition of allocated resources.



     Posted by: Vishal Singh    

    Contact Vishal Singh  Contact Vishal Singh

    in the case of multiple inheritance same member function in both and derived class exist then the two copies of childs will be created. so there is an ambiguity between which function to execute so virtual functions are introduced.we can accept the derived and based class methods with help of pointers.if pointer points to base method then base method will be executed.if it points to derive then derive will be executed so in order to delete these pointers we go for virtual desstructors



     Posted by: krishna    

    Contact krishna  Contact krishna

    virtual destructer is used so that while deleting the pointer to base class but pointing to base class invokes the derived class destructor first,then the base class destruct hence preventing memory leak...



     Posted by: praveen    

    Contact praveen  Contact praveen

    First pls get clear with virtual function conceptss...
    suppose we have a class hierachy
    Empl-->Manager-->CEO
    now we are creating
    Empl* e1;
    CEO c1;
    e1= & c1;
    Now when ever my e1 goes out of scope because e1 is of type Empl only the destructor of e1 is called & memory is freed for attributes of e1 only. ? for Manager & CEO attributes so we have the memory leakage problem

    so if we are making Destructor as a virtual so exceutin of destructor will follow
    ~CEO-->~Manager-->~Empl path so Memory for all the data is freed.....

    so to avoid these Memory Leakage problem we have to make destructor as a virtual destructor.....

    Now if u have understood the explanation pls tell me
    Can we have virtual constructor???




     Posted by: Mihir P Trivedi    

    Contact Mihir P Trivedi  Contact Mihir P Trivedi

    virtual destructors are used to deallocate the memory in prper order if the memory is allocated through new
    for example
    base *p;
    p=new derive;
    firstly the base class constructor would be called followd by the derive class
    if we now say delete p then destruction should proceed from derive to base.
    this can be ensured by declaring base class destructor virual



     Posted by: Rohit Adhlakha    

    Contact Rohit Adhlakha  Contact Rohit Adhlakha


    If you have the better answer, then send it to us. We will display your answer after the approval.
    Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification  Enter the above shown code:*
    Inform me about updated answers to this question

       
    Related Questions
    View Answer
    Can the structures contain the constructor?
    If yes, then how?
    View Answer
    What is difference between copy constructor and constructor?

    View Answer
    What is importance of const. pointer in copy constructor?

    View Answer
    Why can't we overload the sizeof, :?, :: ., .* operators in c++?

    View Answer
    What happens to the member pointers when an exception occurs in constructor, while allocating memory?How can we over come this ?

    View Answer
    Is there any way to write a class such that no class can be inherited from it. Please include code

    View Answer
    What is virtual constructors/destructors?


    View Answer
    What do you mean by pure virtual functions?


    View Answer
    What is namespace?


    View Answer

    Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

    Notify me when better answer is posted!
    Email:

    View ALL C++ Interview Questions

    User Options
    Sponsored Links


    Copyright ©2003-2009 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions
    Page URL: http://www.coolinterview.com/interview/11333/default.asp?cachecommand=bypass


    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Dedicated Servers | Joke of the Day

    0.45