0

What's the difference between composition and inheritance in C++?

By composition,I mean taking one object of a class as another class member

13th Dec 2016, 3:13 PM
Jawwad Hossain
Jawwad Hossain - avatar
2 Answers
+ 2
u know inheritance , child-parent and all that relation. the concepr of composition should not be taught its absurd. its useless. we have class such that class student{ int a; public: ..... ..... ... }; WHAT we do in composition?? instead of int a we use teachr b. thats it. class is also a data type hence we can use it as other members.
13th Dec 2016, 3:32 PM
manish rawat
manish rawat - avatar
+ 1
These concepts are the same in all OOP languages but, ok, lets use C++: Inheritance is a form of specialization i.e. the child (BMW) specializes the behaviour of the parent (Car) by inheriting its behaviour and specializing (or extending it). We typically say inheritance is a "is a" relationship i.e. BMW "is a" Car. Composition is a form of ownership i.e. the class is made up ("composed") of some members that are instances of other classes i.e. a Shape class has a Color member. We typically say composition is a "has a" relationship i.e. a Shape "has a" colour.
13th Dec 2016, 3:37 PM
Ettienne Gilbert
Ettienne Gilbert - avatar