Composition vs. Inheritance

Discussion in 'C++' started by Pallav singh, Jun 3, 2008.

  1. Pallav singh

    Pallav singh Guest

    Hi All

    i have dout when to use Composition and Inheritance while designing
    Module in C++

    Thanks
    Pallav
    Pallav singh, Jun 3, 2008
    #1
    1. Advertising

  2. Pallav singh wrote:
    > i have dout when to use Composition and Inheritance while designing
    > Module in C++


    Rule of thumb: Can you say that "<derived class> is a <base class>"?
    If yes, then maybe inheritance will work.

    Moreover, the <base class> should usually be more abstract as a
    concept than <derived class>, which should be a more concrete concept.

    Example: Base class is a "widget" and derived class is a "button". You
    can say "a 'button' is a 'widget'". A "widget" is a more abstract
    concept because there are many things which can "be a widget", while
    "button" is a more concrete concept because not so many things can "be a
    button".

    Example: A "dialog" class has two "button" widgets. You cannot say "a
    'dialog' is a 'button'", ergo composition. "A 'dialog' has a 'button'."
    Juha Nieminen, Jun 3, 2008
    #2
    1. Advertising

  3. Pallav singh

    sumsin Guest

    On Jun 3, 10:40 pm, Pallav singh <> wrote:
    > Hi All
    >
    > i have dout when to use Composition and Inheritance while designing
    > Module in C++
    >
    > Thanks
    > Pallav


    First of all try to find out the various entities in your design. Then
    for a group of similar entities try to generalize them, as Juha said
    for various kind of buttons like Radio-Button and Push-Button, it is
    possible to generalize them with a button class. So here you should
    use inheritance, where Button would the generic class for Radio-Button
    and Push-Button.
    While in case of composition you should think about the ingredient of
    the entity. As Juha said, in general most of the Dialog Boxes have
    ‘Ok’ and ‘Cancel’ buttons. So here you should use composition, where
    your Dialog box is composed of with two Push-Buttons ‘Ok’ and
    ‘Cancel’.
    sumsin, Jun 4, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    1
    Views:
    471
    John C. Bollinger
    Apr 11, 2005
  2. Code4u
    Replies:
    9
    Views:
    2,735
  3. Gary Wessle

    inheritance and composition

    Gary Wessle, Nov 16, 2006, in forum: C++
    Replies:
    3
    Views:
    280
  4. Replies:
    8
    Views:
    520
    James Kanze
    Jul 29, 2007
  5. Replies:
    18
    Views:
    1,608
    Odalrick
    Nov 18, 2007
Loading...

Share This Page