Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Composition vs. Inheritance

Reply
Thread Tools

Composition vs. Inheritance

 
 
Pallav singh
Guest
Posts: n/a
 
      06-03-2008
Hi All

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

Thanks
Pallav
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      06-03-2008
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'."
 
Reply With Quote
 
 
 
 
sumsin
Guest
Posts: n/a
 
      06-04-2008
On Jun 3, 10:40 pm, Pallav singh <(E-Mail Removed)> 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’.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Design Patterns - composition vs. inheritance snewman18@gmail.com Python 18 11-18-2007 05:43 PM
Composition versus Implementation Inheritance chsalvia@gmail.com C++ 8 07-29-2007 07:28 PM
inheritance and composition Gary Wessle C++ 3 11-17-2006 12:45 AM
C++ and Interfaces, Multiple Inheritance or Composition? Code4u C++ 9 05-13-2005 10:47 PM
composition or inheritance for a jdialog - Java 1 04-11-2005 01:53 PM



Advertisments