Sponsored Links

Interview Questions



INTERVIEW QUESTIONS C++ INHERITANCE IN C++ DETAILS

Question: What is the difference between composition and inheritance?

Answer: Composition is the technique of combining the simpler data types with the complex data types e.g. function calls within the calling function.<br>Inheritance is one, in which one can acquire the property of other..

Category Inheritance in C++ Interview Questions & Answers - Exam Mode / Learning Mode
Rating (0.2) By 9242 users
Added on 11/20/2012
Views 74695
Rate it!

Question: What is the difference between composition and inheritance?

Answer:

Composition is the technique of combining the simpler data types with the complex data types e.g. function calls within the calling function.<br>Inheritance is one, in which one can acquire the property of other.. Source: CoolInterview.com

Answered by: vinod rajput | Date: 12/14/2007 | Contact vinod rajput Contact vinod rajput

Composition & inheritance is totally different from each other composition means compose the properties by code of same class in composition we can refer some other class who hold same properties but we have to code we can not directly use that code ,we have to rewrite .<br><br>But inheritance i.e inherit the properties of other class .in it method of class to whom we inherit can directly call by the child class object.<br><br>if we think as real life example if we inspired by someone we adope the properties of that person we have to add that quality of person ourself ,that is composition. but some quality of our parent automatically in us there is no need to strive ,that is inheritance Source: CoolInterview.com

Answered by: Deepak jain | Date: 12/19/2007 | Contact Deepak jain Contact Deepak jain

Let me be a bit elaborate. There are two kinds of inheritances. One if of the kind "IS A" and the other is of the kind "HAS A". "IS A" type of inheritance is like the following<br><br>class car:public vehicle<br>{<br>}<br>where it is said CAR "IS A [KIND OF ]" VEHICLE.<br><br>There are two kinds of "HAS A" inheritance implementation. Aggregation and Composition. <br><br>Now consider the following<br><br>class Heart<br>{<br>}<br><br>class HumanBeing<br>{<br>private :<br>Heart _heart;<br>}<br><br>The above example is that of "COMPOSITION" type of "HAS A" Inheritance. The Human Being "HAS A" heart and human being can not live with out heart. There is no sense of the lifecycle of human without heart ( one verson of the academia ) and no lifecycle of heart without human ( another version of the academia ). So if one destroys human, the heart also gets destroyed. This is signified as "COMPOSITION".<br><br>Consider the following<br><br>class Sheep<br>{<br>}<br><br>class HerdOfSheep<br>{<br>private Sheep *_pSheep;<br>}<br><br>In the above case the HerdOfSheep can be empty and sheep can exist without being in the herd. This kind of "HAS A" relationship in inheritance is called as "AGGREGATION". The scope of sheep is even outside the scope of the herd.<br><br>-Parthasarathy Source: CoolInterview.com

Answered by: Parthasarathy | Date: 12/27/2007 | Contact Parthasarathy Contact Parthasarathy

Inheritance is a "Is A" relationship and composition is "Has A" relationship. Composition is for code reuse. Inheritance is not for code reuse but for flexibility. Source: CoolInterview.com

Answered by: Nagendra | Date: 1/7/2008 | Contact Nagendra Contact Nagendra

Composition - A class has the object of class B i.e we can access the member variable of class B through class A also.<br>ex. <br>Class B<br>{<br>}<br>Class A<br>{<br>A a;<br>}<br><br>Inheritance : If we say that class A inherit from Class B that means Class B has the characteristics of Class A (depending on the access modifier).<br> Source: CoolInterview.com

Answered by: Amit | Date: 1/9/2008 | Contact Amit Contact Amit

We can say private inheritance is a syntactic variant of composition (has-a).<br><br>E.g., the "Car has-a Engine" relationship can be expressed using composition:<br><br>class Engine {<br>public:<br>Engine(int numCylinders);<br>void start(); // Starts this Engine<br>};<br><br>class Car {<br>public:<br>Car() : e_(8) { } // Initializes this Car with 8 cylinders<br>void start() { e_.start(); } // Start this Car by starting its Engine<br>private:<br>Engine e_; // Car has-a Engine<br>};<br><br>The same "has-a" relationship can also be expressed using private inheritance:<br><br>class Car : private Engine { // Car has-a Engine<br>public:<br>Car() : Engine(8) { } // Initializes this Car with 8 cylinders<br>Engine::start; // Start this Car by starting its Engine<br>};<br><br>There are several similarities between these two forms of composition:<br>* In both cases there is exactly one Engine member object contained in a Car<br>* In neither case can users (outsiders) convert a Car* to an Engine*<br><br>There are also several distinctions:<br>* The first form is needed if you want to contain several Engines per Car<br>* The second form can introduce unnecessary multiple inheritance<br>* The second form allows members of Car to convert a Car* to an Engine*<br>* The second form allows access to the protected members of the base class<br>* The second form allows Car to override Engine's virtual[20] functions<br><br><br>Use composition when you can, private inheritance when you have to.<br><br>Normally you don't want to have access to the internals of too many other<br>classes, and private inheritance gives you some of this extra power (and<br>responsibility). But private inheritance isn't evil; it's just more expensive<br>to maintain, since it increases the probability that someone will change<br>something that will break your code.<br><br>A legitimate, long-term use for private inheritance is when you want to build a<br>class Fred that uses code in a class Wilma, and the code from class Wilma needs<br>to invoke member functions from your new class, Fred. In this case, Fred calls<br>non-virtuals in Wilma, and Wilma calls (usually pure virtuals[22.4]) in itself,<br>which are overridden by Fred. This would be much harder to do with<br>composition.<br><br>class Wilma {<br>protected:<br>void fredCallsWilma()<br>{<br>cout << "Wilma::fredCallsWilma()<br>";<br>wilmaCallsFred();<br>}<br>virtual void wilmaCallsFred() = 0; // A pure virtual function[22.4]<br>};<br><br>class Fred : private Wilma {<br>public:<br>void barney()<br>{<br>cout << "Fred::barney()<br>";<br>Wilma::fredCallsWilma();<br>}<br>protected:<br>virtual void wilmaCallsFred()<br>{<br>cout << "Fred::wilmaCallsFred()<br>";<br>}<br>};<br><br><br> Source: CoolInterview.com

Answered by: Richa gupta | Date: 1/10/2008 | Contact Richa gupta Contact Richa gupta

I too like to know the answer. Source: CoolInterview.com

Answered by: Prabhuraj | Date: 1/13/2008 | Contact Prabhuraj Contact Prabhuraj

Inheritance:- Two class can exhibits an IS_A relationship called inheritance.<br><br>Composition:- This concept comes under the containment(in which one(outer) object holds another(Inner) object). This shows the HAS_A relationship. In this type of containment the outer object controls the life time of the inner objects. e.g (outer)[Bank] HAS_A [Account](inner). Source: CoolInterview.com

Answered by: Jitendra singh | Date: 1/26/2008 | Contact Jitendra singh Contact Jitendra singh

Composition is nothing but the Has-A relationship which is indicates inner class is not independent from outer class .<br><br>This is creates the COHESION and dependencies and which is tightly coupled relationship and this is not recommended many times in OOPS .<br> Source: CoolInterview.com

Answered by: Ashwinkumar | Date: 1/26/2008 | Contact Ashwinkumar Contact Ashwinkumar

Defining a class as a member of another class is composition.<br><br>A process by which object of one class acquire the properties of another class is inheritance. Source: CoolInterview.com

Answered by: Manoj Jangid | Date: 2/2/2008 | Contact Manoj Jangid Contact Manoj Jangid

java is object oriented program but another one is not. Source: CoolInterview.com

Answered by: tamil | Date: 2/5/2008 | Contact tamil Contact tamil

In a Class composition <br>the use of the class has clear knowledge of all the classes which are used in the composition. the classes appear as data members. <br><br>In inheritance the base class is totally "dismantled" and the use of the class has knowledge of the base class. all the methods and data members of the base class is the member of the new derived class. this is different from the above case in which the composing class it self appears as data member. Source: CoolInterview.com

Answered by: Amaren | Date: 4/3/2008 | Contact Amaren Contact Amaren

Composition means Creating object of one class in another class.<br><br>Inheritance means deriving a new class from already existing class. Source: CoolInterview.com

Answered by: Ravikumar Horatti | Date: 7/27/2008 | Contact Ravikumar Horatti Contact Ravikumar Horatti

From a Desing Perspective Favour Composition over Inheritance.<br>(1) Inheritance favours tight coupling.<br>(2) Compisition favours loose coupling.<br>(3) Inheritance: Any changes in the base class might have ripple effect in the derived class.<br>(4) Inheritance: Favours weak encapsulation.<br>(5) Composition: favours strong encapsulation.<br>(6) Inheritance is achieved by Extension.<br>(7) Composition is ahcieved by Instance variable.<br>(8) Inheritance is static binding, where as composition is dynamic binding.<br><br>As per design principle:<br>A class should be closed for modification and open for extension.<br> Source: CoolInterview.com

Answered by: rakshith | Date: 12/7/2009 | Contact rakshith Contact rakshith

Composition means a class which is composed of other class and it is called has a relation...e.g Car has an engine, has tyre etc.<br>here is an example of composition where class Y is composed of class X<br><br>'''''''''''''''''<br>Class X<br>{<br>private:<br>int i;<br><br>X(){i=0;}<br>void Set(int ii)<br>{<br>i=ii;<br>}<br><br>int Get()<br>{<br>return i;<br>}<br><br>void improve(int ii)<br>{<br>return i+=ii;<br>}<br><br>};<br><br>Class Y<br>{<br>private:<br>int i;<br><br>public:<br>X x; //emeding obj of X<br><br>void Set(int ii)<br>{<br>i=ii;<br>}<br><br>int Get()<br>{<br>return i;<br>}<br>};<br><br>int main()<br>{<br>Y y;<br>y.Set(5);<br>y.x.Set(5);<br>} Source: CoolInterview.com

Answered by: Danish Afzal | Date: 5/24/2010 | Contact Danish Afzal Contact Danish Afzal


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
What Is The Use of VirtualDestructer?
View Answer
Is there any way to write a class such that no class can be inherited from it. Please include code

View Answer
What do you mean by inheritance?


View Answer
class A()
{
};

int main()
{
A a;
}

Whether there will be a default constructor provided by the compiler in above case ?


View Answer
what is the use of volatile keyword? Give me one example?

View Answer
it possible to inherit the private member in drived class?

View Answer
Can you prevent your class from being inherited and becoming a base class for some other classes?
View Answer
Can you allow class to be inherited, but prevent the method from being over-ridden?
View Answer
Can you inherit multiple interfaces?
View Answer
If a base class has a bunch of overloaded constructors, and an inherited class has another bunch of overloaded constructors, can you enforce a call from an inherited constructor to an arbitrary base constructor?
View Answer
What do you mean by inheritance?
View Answer
What is RTTI?
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 Inheritance 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-2025 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions