GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 22 of 203    Print  
What is virtual constructors/destructors?
Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object. 
 There is a simple solution to this problem – declare a virtual base-class destructor. This makes all derived-class destructors virtual even though they don’t have the same name as the base-class destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator to a base-class pointer to a derived-class object, the destructor for the appropriate class is called.
 
Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error.
 
Does c++ support multilevel and multiple inheritance?
Yes. 
 
What are the advantages of inheritance?
• It permits code reusability.
• Reusability saves time in program development.
• It encourages the reuse of proven and debugged high-quality software, thus reducing problem after a system becomes functional.
 
 
What is the difference between declaration and definition?
 The declaration tells the compiler that at some later point we plan to present the definition of this declaration. 
E.g.:  void stars () //function declaration
 
The definition contains the actual implementation.
E.g.:  void stars () // declarator
 {
  for(int j=10; j>=0; j--) //function body
   cout<<”*”;
  cout<<endl;
 } 
 



  
Total Answers and Comments: 4 Last Update: December 01, 2007   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: SomGollakota
 

Constructors cannot be virtual. This is a given.
Distructors, on the other hand, can be virtual and help in destroying child class objects using a base class pointer. Consider the following example.

class A {
public:
B();
~B();
}
class B : public A {
B();
~B();
}

When you create an instance (object) of class B, the way the object is created is, first Object A is created and then Object B (thus concluding the object of B - Object B contains both the parent and child objects). The destruction of the object B is in reverse order (first object B is destroyed, and then object A).
If, on the other hand, you were to have a pointer to object A and assign it a dynamic object B, when you destroy it, only object A's destructor is called and object B part of the object is never destroyed. Virtual destructors are used to solve this problem and destroy the objects in the right order.



Above answer was rated as good by the following members:
mushpert, ozkan
July 04, 2006 00:39:16   #1  
deepak        

RE: What is virtual constructors/destructor...
Constructor can't be virtual but u can have the feature of virtual construct which is also called factory pattern.
 
Is this answer useful? Yes | No
June 13, 2007 03:51:14   #2  
SomGollakota Member Since: June 2007   Contribution: 48    

RE: What is virtual constructors/destructor...

Constructors cannot be virtual. This is a given.
Distructors on the other hand can be virtual and help in destroying child class objects using a base class pointer. Consider the following example.

class A {
public:
B();
~B();
}
class B : public A {
B();
~B();
}

When you create an instance (object) of class B the way the object is created is first Object A is created and then Object B (thus concluding the object of B - Object B contains both the parent and child objects). The destruction of the object B is in reverse order (first object B is destroyed and then object A).
If on the other hand you were to have a pointer to object A and assign it a dynamic object B when you destroy it only object A's destructor is called and object B part of the object is never destroyed. Virtual destructors are used to solve this problem and destroy the objects in the right order.


 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
October 22, 2007 00:18:59   #3  
krisgroup Member Since: October 2007   Contribution: 8    

RE: What is virtual constructors/destructor...
hi

we can not have the virtual constructors the basic reason is that it turns out to be a syntax error because when you create some virtual function it must go into a VTable kind of thing that keeps track of these virtual functions since its the constructor we dont have the table created at that time so declaring a constructor virtual can throw an execption or error...

the prime reason to declare the destructors virtual is that in large scale programs we may forget to deallocate all the virtual functions we delcared and declaring the destructor virtual can ensure that all objects created get deallocated at the termination of the program

hope it makes sense

chaitanya gudipati

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
December 01, 2007 18:20:54   #4  
berezleon Member Since: December 2007   Contribution: 6    

RE: What is virtual constructors/destructors?
Virtual constructor is not build-in C++ feature but it doesn't mean its not used by devs in code and in conversations. There are many other things that doesn't exist in particular language yet people find ways around to solve it (SingleTon Virtual Constructor Double-Dispatching etc). What means by virtual ctr is a static member function that returns pointer to a heap object (whether new or existing) or it might return reference to static object declared in this function. It would be really wrong to say that virtual ctr doesn't exist --it means you are not aware of such thing and absolutely nothing else. The nice thing about virt ctr each derived virt ctr can return pointer to a type of the class it's declare in --thus we can say virtual functions have different signatures depending where they implemented:

class base {public: virtual base * virtCtr(){return new base;}
class deriv:public base {public: virtual deriv * virtCtr(){retun new deriv;}

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape