Click here to Skip to main content
15,305,430 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
This is a more of a thought question.

Lets say I have a problem that could be solved by using either Inheritance or composition.
Which one is better and why?
Posted
Comments
Thanks7872 25-Sep-13 3:50am
   
http://www.cplusplus.com/forum/general/45494/
Raghuveer Kasyap 25-Sep-13 5:40am
   
what is the problem plz.

1 solution

Usually if you have the choicie, then probably that composition is better as it will better separate what main and secondary object do.

Main object will not be polluted by functions that would otherwise be inherited but are of no use to external user of the main object.

Typically you want to use inheritance only for IS-A relationship or if you have to override virtual functions.
   
Comments
CPallini 25-Sep-13 9:01am
   
5
Volynsky Alex 25-Sep-13 10:57am
   
Nice

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900