Thread: inheritance vs composition

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    19

    Question inheritance vs composition

    i have a question..

    what the advantages/disadvantages of using classes through inheritance or through composition?

    Raz

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Advantages/Disadvantages do not really apply when comparing inheritance and composition. You use one or the other based on the type of relationship. Inheritance is an "is a" relationship and composition is a "has a" relationship.

    For instance, let's say you have a dog class and want to know if a poodle class should inherit or use the dog class through composition. Well, a poodle "is a" dog so you know you should inherit. But what about using a tongue class? Is a poodle a tongue (some may agree)? No, but a poodle "has a" tongue and therefore the poodle class should contain a tongue reference as a member of the class.

    This is the key to keep in mind when trying to choose between inheritance and composition.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 10:19 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM