Question:
What is virtual constructors/destructors?
Answer:
Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying<br>the delete operator to a base-class pointer to the object, the base-class destructor function<br>(matching the pointer type) is called on the object.<br>There is a simple solution to this problem – declare a virtual base-class destructor. This makes all<br>derived-class destructors virtual even though they don’t have the same name as the base-class<br>destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator<br>to a base-class pointer to a derived-class object, the destructor for the appropriate class is called.<br>Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is<br>a syntax error. Does c++ support multilevel and multiple inheritance?<br>Yes.<br>What are the advantages of inheritance?<br>• It permits code reusability.<br>• Reusability saves time in program development.<br>• It encourages the reuse of proven and debugged high-quality software, thus reducing problem<br>after a system becomes functional.<br>What is the difference between declaration and definition?<br>The declaration tells the compiler that at some later point we plan to present the definition of this<br>declaration.<br>E.g.: void stars () //function declaration<br>The definition contains the actual implementation.<br>E.g.: void stars () // declarator<br>{<br>for(int j=10; j>=0; j--) //function body<br>cout<<”*”;<br>cout<<endl;<br>} Source: CoolInterview.com
write an interactive program that three(3)list of number which are stored in three separate file,and criates one(1) stored list. each file should contain not more than 15 number. First you need to create a program that randomly chooses a size (<=15 numbers) for list 1 an than rendomly generates alist of number and store them in the fole 1 Repeat this Procedure to select the other two list(list 1 and list 2) and stores them in the corresponding file (list 2 and list 3)for sorting and merging them into one... Source: CoolInterview.com
Answered by: Dhaval Prajapati | Date: 11/3/2007
| Contact Dhaval Prajapati
"If an object (with a non-virtual destructor) is destroyed explicitly by applying<br>the delete operator to a base-class pointer to the object, the base-class destructor function<br>(matching the pointer type) is called on the object."<br><br>Wrong. The behavior in this case is undefined. Source: CoolInterview.com
Answered by: ihatepopups | Date: 4/27/2009
| Contact ihatepopups
A destructor is called whenever an object is deleted, there are some cases when users code doesn’t know which destructor should be called. If a base class pointer is pointing to derived class objects. when the object dies only the base class destructor is called. This could be a serious error, especially if the derived destructor is supposed to release some resources. Virtual destructors are extremely valuable when some derived classes have some specified cleanup code. If any code anywhere deletes an derived class object via a base class pointer, then the base class’s destructor needs to be virtual.<br>class CA<br>{<br>public: Virtual ~CA()<br>{<br>cout<<"destructor ca"<<endl;<br>};<br>class CB:public CA<br>{<br>public:<br>~CB()<br>{<br>cout<<"dest cb"<<endl;<br>}<br>};<br>void main()<br>{<br>CA *obj = new CB;<br>delete obj;<br>}<br><br>The virtual keyword cannot be applied to a constructor since constructor turns raw bits into a living object. <br>Objects of a derived class mature during construction. While the bas class’s constructor is executing, the object is merely a base class object. Later when the derived class’s contructor begins executing, the object matures into a derived class object.<br>If a virtual function is invoked while the object is still immature, the immature version of the virtual function is called.<br>This gives syntex error..<br> Source: CoolInterview.com
Answered by: Niranjan ambati | Date: 5/14/2009
| Contact Niranjan ambati
virtual functions (constructor and destructor are also functions) instruct the compilers to executes the latest defination of the functions.Thus they are helpful in inheritance.<br><br>For example :suppose class B is inherited from class A.<br><br>Now when we create a pointer of object a pointing to object of class B.<br>then in nomal case the constructor of class A will be called.<br>While in case of Virtual constructor the lated constructor will be called (class B constructor)<br> Source: CoolInterview.com
Answered by: Tarun Mishra | Date: 5/30/2009
| Contact Tarun Mishra
virtual functions (constructor and destructor are also functions) instruct the compilers to executes the latest defination of the functions.Thus they are helpful in inheritance.<br><br>For example :suppose class B is inherited from class A.<br><br>Now when we create a pointer of object a pointing to object of class B.<br>then in nomal case the constructor of class A will be called.<br>While in case of Virtual constructor the lated constructor will be called (class B constructor)<br> Source: CoolInterview.com
Answered by: ARADHNA SINGH | Date: 7/11/2009
| Contact ARADHNA SINGH
virtual functions (constructor and destructor are also functions) instruct the compilers to executes the latest defination of the functions.Thus they are helpful in inheritance.<br><br>For example :suppose class B is inherited from class A.<br><br>Now when we create a pointer of object a pointing to object of class B.<br>then in nomal case the constructor of class A will be called.<br>While in case of Virtual constructor the lated constructor will be called (class B constructor)<br> Source: CoolInterview.com
Answered by: varun sharma | Date: 8/1/2009
| Contact varun sharma
if the object in the hierarchy is destroyed explicitly by applying the delete operator<br>to a base-class pointer to a derived-class object, the destructor for the appropriate class is called.<br>Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is<br> Source: CoolInterview.com
Answered by: Gulab kumar | Date: 9/6/2009
| Contact Gulab kumar
if the object in the hierarchy is destroyed explicitly by applying the delete operator<br>to a base-class pointer to a derived-class object, the destructor for the appropriate class is called.<br>Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is<br> Source: CoolInterview.com
Answered by: BANDA | Date: 9/29/2009
| Contact BANDA
i want to c++ interview questions and projects,please send me Source: CoolInterview.com
Answered by: T.MAHESH | Date: 10/21/2009
| Contact T.MAHESH
without virtual destructor ,base class pointer delete as well derived class pointer. Source: CoolInterview.com
Answered by: shyam sundar | Date: 11/10/2009
| Contact shyam sundar
without virtual destructor ,base class pointer delete as well derived class pointer. Source: CoolInterview.com
Answered by: chandan | Date: 12/14/2009
| Contact chandan
Ask any programmer, he'll immediately reply saying "A destructor is a member function of a class, which gets called when the object goes out of scope". This means all clean ups and final steps of class destruction are to be done in destructor. A virtual function is something which helps a derived class in overriding the implementation of a functionality of a base class. <br>The order of execution of destructor in an inherited class during a clean up is like this.<br>1. Derived class destructor<br>2. Base class destructor<br><br><br><br><br>A difference between a destructor (of course also the constructor) and other member functions is that, if a regular member function has a body at the derived class, only the version at Derived class gets executed. Whereas in case of destructors, both derived as well as base class versions get executed.<br><br>Now turning our attention to why a destructor has to be virtual, the reason is that we, programmers are very smart. We'll do days and nights of work to inherit and extend the functionality of an existing class which is being used, and say that we don't want to change the implementation/interface just for the sake of a new entrant. Let me explain this with an example.<br><br><br>--------------------------------------------------------------------------------<br>#include <iostream.h><br>class Base<br>{<br> public:<br> Base(){ cout<<"Constructor: Base"<<endl;}<br> ~Base(){ cout<<"Destructor : Base"<<endl;}<br>};<br>class Derived: public Base<br>{<br> //Doing a lot of jobs by extending the functionality<br> public:<br> Derived(){ cout<<"Constructor: Derived"<<endl;}<br> ~Derived(){ cout<<"Destructor : Derived"<<endl;}<br>> };<br>void main()<br>{<br> Base *Var = new Derived();<br> delete Var;<br>}<br><br>Try executing this code, you'll see the difference. To our observation, the constructors are getting called in the proper order. But to the dread of a programmer of a large project, the destructor of the derived class was not called at all.<br><br>This is where the virtual mechanism comes into our rescue. By making the Base class Destructor virtual, both the destructors will be called in order. The following is the corrected sample.<br><br>#include <iostream.h><br>class Base<br>{<br> public:<br> Base(){ cout<<"Constructor: Base"<<endl;}<br> virtual ~Base(){ cout<<"Destructor : Base"<<endl;}<br>};<br>class Derived: public Base<br>{<br> //Doing a lot of jobs by extending the functionality<br> public:<br> Derived(){ cout<<"Constructor: Derived"<<endl;}<br> ~Derived(){ cout<<"Destructor : Derived"<<endl;}<br>};<br>void main()<br>{<br> Base *Var = new Derived();<br> delete Var;<br>}<br><br><br>Note:<br>There is one more point to be noted regarding virtual destructor. We can't declare pure virtual destructor. Even if a virtual destructor is declared as pure, it will have to implement an empty body (at least) for the destructor.<br> Source: CoolInterview.com
Answered by: P GOVIND RAO | Date: 1/21/2010
| Contact P GOVIND RAO
malano itna mushkil kaam kaisay ker laitay ho Source: CoolInterview.com
Answered by: umer | Date: 6/16/2010
| Contact umer
there is no need to create a virtual destructor. when the object goes out of scope then the destructor calls automatically and it clears the memory occupied by the object the created earlier. Source: CoolInterview.com
Answered by: lohith | Date: 6/25/2010
| Contact lohith
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|