GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  Programming  >  C++

 Print  |  
Question:  
What is virtual constructors/destructors?


Answer: 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;
 } 
 


July 07, 2006 00:39:16 #1
 deepak   Member Since: Visitor    Total Comments: N/A 

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.
     

 

Back To Question