ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage   interview questions urls   External Links  Contact Us     Login  |  Sign Up                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C++  >>  C++ General
 
 


 

 
 STL interview questions  STL Interview Questions
 OOPS interview questions  OOPS Interview Questions
 C++ General interview questions  C++ General Interview Questions
Question
Why would you make a destructor virtual?
 Question Submitted By :: Seeker
I also faced this Question!!     Rank Answer Posted By  
 
  Re: Why would you make a destructor virtual?
Answer
# 1
we make destructure as virtual because when we derived a 
class from base class then destructor called from derived 
to base so for this happening correctly we haveto make base 
class destructor as avirtual.
 
Is This Answer Correct ?    0 Yes 0 No
Sunny Bogawat
 
  Re: Why would you make a destructor virtual?
Answer
# 2
Vitual destructor is used 

(1) whenever the base class object pointer points to 
derived class object,and is being created using new.
i.e class base - base class
in main - base bptr;
class derived - derived class
in main - bptr = new derived();

Then whenever we use "delete bptr" at runtime always the 
memory of bptr is freed but not derived because here the 
pointer is of type base and not of derived.

Note: The destructor that gets invoked is the one that 
associated with the type of the object.

Simple rule of thumb: Make your destructor virtual if your 
class has any virtual functions.

Example: 
#include <iostream.h>
class Base
{
       public:
          Base(){ cout<<"Constructor: Base"<<endl;}
          virtual ~Base(){ cout<<"Destructor : Base"<<endl;}
};
class Derived: public Base
{
     //Doing a lot of jobs by extending the functionality
       public:
           Derived(){ cout<<"Constructor: Derived"<<endl;}
           ~Derived(){ cout<<"Destructor : Derived"<<endl;}
};
void main()
{
        Base *Var = new Derived();
        delete Var;
}


 
Is This Answer Correct ?    1 Yes 0 No
Chandra
 
 
 
  Re: Why would you make a destructor virtual?
Answer
# 3
what chandra said is correct.but it is not 100% correct.for 
that answer i want to add one point.

whenever 
1)there is a pointer in the base class and we allocated 
with the help of new in base class and anthoer pointer in 
derived class and agin we allocated with the help of new.
2)whenever the base class object pointer points to 
derived class object,and is being created using new.
i.e class base - base class
in main - base bptr;
class derived - derived class
in main - bptr = new derived();

this will be illustrated with an example given below


#include <iostream.h>
class Base
{      protected:
           int *ptr;
       public:
          Base()
          {     ptr = new  int[10];
               cout<<"Constructor: Base"<<endl;
          }
          virtual ~Base()
          { 
                cout<<"Destructor : Base"<<endl;
                delete []ptr;
           }
};
class Derived: public Base
{
       protected:
            int *ptr1;
       public:
           Derived()
           {          
                      ptr1=new int[10]; 
                      cout<<"Constructor: Derived"<<endl;
           }
           ~Derived()
           { 
                delete []ptr1;    
                cout<<"Destructor : Derived"<<endl;
           }
};
void main()
{
        Base *Var = new Derived;
        delete Var;
}

if we are not placing virtual in base  class, only base 
class destructor will be called because "var is of type 
Base".so,
if we are not placing virtual compiler will see only "type 
of the pointer,not the object which it is holding".so,if we 
place virtual it will see the object it is holding.

now memory allocated for ptr,ptr1 in heap was destroyed by 
destructors of the  respective class.
 
Is This Answer Correct ?    1 Yes 0 No
Chandra Sekhar
 

 
 
 
Other C++ General Interview Questions
 
  Question Asked @ Answers
 
How to implement flags? Symphony1
How is an Abstract Base Class(ABC) related to an "Abstract Data Type" (ADT)  2
Is there something that we can do in C and not in C++? Patni5
Have you used MSVC? What do you think of it? Google2
In C++ cout is: a) object b) class c) something else Lehman-Brothers10
How can you find the nodes with repetetive data in a linked list? Lucent1
When volatile can be used? Symphony2
What is Object Oriented programming.what is the difference between C++ and C? Infosys5
What is "mutable" keyword?  2
1. What does the following do: void afunction(int *x) { x=new int; *x=12; } int main() { int v=10; afunction(&v); cout<<v; } a) Outputs 12 b) Outputs 10 c) Outputs the address of v Quark3
What is the difference between a copy constructor and an overloaded assignment operator? Microsoft3
How do you know that your class needs a virtual destructor? Lucent3
How to avoid changing constant values? Symphony1
What is the Difference between "vector" and "array"? TCS6
What is the output of: String a1 = "Hello"; String a2 = "world!"; String* s1 = &a2; String& s2 = a1; s1 = &a1; s2 = a2; std::cout << *s1 << " " << s2 << std::endl; Lehman-Brothers4
Evaluate: int fn(int v) { if(v==1 || v==0) return 1; if(v%2==0) return fn(v/2)+2; else return fn(v-1)+3; } for fn(7); a) 10 b) 11 c) 1 Quark1
Write a program to reverse a linked list? Catalytic-Software2
whats the size of class EXP on 32 bit processor? class EXP { char c1; char c2; int i1; int i2; char *ptr; static int mem; }; Huawei4
class Foo { const int x; protected: Foo(int f); ~Foo(); }; Foo f; Referring to the sample code above, why will the class declaration not compile? a) The variable x is const. b) The destructor is protected. c) The destructor is not public. d) The constructor is protected. e) There is no default constructor. Quark4
What is the difference between operator new and the new operator? Wipro1
 
For more C++ General Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com