473,395 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Composition vs. Inheritance

Hi All

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

Thanks
Pallav
Jun 27 '08 #1
4 2459
Pallav singh wrote:
i have dout when to use Composition and Inheritance while designing
Module in C++
If you mean private inheritance, then the conversion from the class to
the one it inherits from will still be available to the class and its
friends, so I'd stick with composition.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #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 classis a <base class>"?
If yes, then maybe inheritance will work.

Moreover, the <base classshould 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'."
Jun 27 '08 #3
Pallav singh <si**********@gmail.comwrote:
i have dout when to use Composition and Inheritance while designing
Module in C++
Use composition when you can, inheritance when you must.
Jun 27 '08 #4
On Jun 3, 10:40 pm, Pallav singh <singh.pal...@gmail.comwrote:
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’.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: preetam | last post by:
Hi, This question is more towards design than towards c++ details. By looking at books on design patterns and various google threads on the same topic, I see that composition is favoured to...
9
by: Code4u | last post by:
My colleagues and I have been discussing techniques for implementing interfaces in C++. We're looking for a mechanism similar to COM's QueryInterface, in which a certain types of objects can be...
1
by: Tony Johansson | last post by:
Hello experts! I know it's easy to override when you have inheritance. But if you instead use object composition having a pointer to a class X. If you want to override when having this object...
3
by: Gary Wessle | last post by:
class Client { public: Client(); virtual void login()=0; }; Client::Client(){} class Play : public Client { public: Play();
5
by: =?Utf-8?B?Wg==?= | last post by:
Hi, I want to create a class that sends/receives data from the serial port. There is such a class in .NET v 2.0 but what I need is a serial port with some extra features. For example I want my...
6
by: hemanbansal | last post by:
Hi Guys, Can anyone tell What is composition? in detail along with its differences with inheritance in general.
2
by: elangobala | last post by:
can anyone suggest me abt composition .and difference between composition and inheritance.
7
by: snewman18 | last post by:
In learning about design patterns, I've seen discussion about using inheritance when an object's relationship to another object is 'is-a' and composition when the relationship is 'has-a'. Since...
2
by: traineeirishprogrammer | last post by:
I am starting to develop applications in the form of 3 tier really (4 tier) architecture at the moment. I am still learning and at the moment I am developing a simple guest book. With the power...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.