Sponsored Links

Interview Questions



INTERVIEW QUESTIONS C++ OPERATOR OVERLOADING IN C++ DETAILS

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>}

Category Operator Overloading in C++ Interview Questions & Answers - Exam Mode / Learning Mode
Rating (0.4) By 9329 users
Added on 10/22/2009
Views 103776
Rate it!

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 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 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 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 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 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 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 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 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 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 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 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 Contact P GOVIND RAO

malano itna mushkil kaam kaisay ker laitay ho Source: CoolInterview.com

Answered by: umer | Date: 6/16/2010 | Contact umer 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 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.
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

Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

View All Operator Overloading in C++ Interview Questions & Answers - Exam Mode / Learning Mode



User Options
India News Network

Latest 20 Questions
Payment of time- barred debt is: (a) Valid (b) Void (c) Illegal (d) Voidable
Consideration is defined in the Indian Contract Act,1872 in: (a) Section 2(f) (b) Section 2(e) (c) Section 2(g) (d) Section 2(d)
Which of the following is not an exception to the rule, "No consideration, No contract": (a) Natural love and affection (b) Compensation for involuntary services (c) Completed gift (d) Agency
Consideration must move at the desire of: (a) The promisor (b) The promisee (c) The promisor or any other party (d) Both the promisor and the promisee
An offer which is open for acceptance over a period of time is: (a) Cross Offer (b) Counter Offer (c) Standing Offer (d) Implied Offer
Specific offer can be communicated to__________ (a) All the parties of contract (b) General public in universe (c) Specific person (d) None of the above
_________ amounts to rejection of the original offer. (a) Cross offer (b) Special offer (c) Standing offer (d) Counter offer
A advertises to sell his old car by advertising in a newspaper. This offer is caleed: (a) General Offer (b) Special Offer (c) Continuing Offer (d) None of the above
In case a counter offer is made, the original offer stands: (a) Rejected (b) Accepted automatically (c) Accepted subject to certain modifications and variations (d) None of the above
In case of unenforceable contract having some technical defect, parties (a) Can sue upon it (b) Cannot sue upon it (c) Should consider it to be illegal (d) None of the above
If entire specified goods is perished before entering into contract of sale, the contract is (a) Valid (b) Void (c) Voidable (d) Cancelled
______________ contracts are also caled contracts with executed consideration. (a) Unilateral (b) Completed (c) Bilateral (d) Executory
A offers B to supply books @ Rs 100 each but B accepts the same with condition of 10% discount. This is a case of (a) Counter Offer (b) Cross Offer (c) Specific Offer (d) General Offer
_____________ is a game of chance. (a) Conditional Contract (b) Contingent Contract (c) Wagering Contract (d) Quasi Contract
There is no binding contract in case of _______ as one's offer cannot be constructed as acceptance (a) Cross Offer (b) Standing Offer (c) Counter Offer (d) Special Offer
An offer is made with an intention to have negotiation from other party. This type of offer is: (a) Invitation to offer (b) Valid offer (c) Voidable (d) None of the above
When an offer is made to the world at large, it is ____________ offer. (a) Counter (b) Special (c) General (d) None of the above
Implied contract even if not in writing or express words is perfectly _______________ if all the conditions are satisfied:- (a) Void (b) Voidable (c) Valid (d) Illegal
A specific offer can be accepted by ___________. (a) Any person (b) Any friend to offeror (c) The person to whom it is made (d) Any friend of offeree
An agreement toput a fire on a person's car is a ______: (a) Legal (b) Voidable (c) Valid (d) Illegal



Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |




Copyright ©2003-2024 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions