473,800 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2479
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**********@g mail.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...@g mail.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
1985
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 inheritence. One more article I read was Allen holub's "Why extends is evil". http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html One general advice I found was that - extending behaviour is better done with interfaces and...
9
2316
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 queried, at run-time, for a particular interface and if it is supported, a pointer or reference to that interface passed to the caller. Two possible implementations came up, multiple inheritance and composition. Using MI the class multiply inherits...
1
1598
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 composition how is that done. Give some easy example if you have some.
3
1732
by: Gary Wessle | last post by:
class Client { public: Client(); virtual void login()=0; }; Client::Client(){} class Play : public Client { public: Play();
5
1984
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 class to raise a certain event not only when data has been received but also upon some additional condition. If the class I want to extend was an Windows.Forms class I could write this: class MyButton:Button {
6
1950
by: hemanbansal | last post by:
Hi Guys, Can anyone tell What is composition? in detail along with its differences with inheritance in general.
2
1326
by: elangobala | last post by:
can anyone suggest me abt composition .and difference between composition and inheritance.
7
3073
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 this is all new and I'm still learning, I was hoping someone can give me some pointers on best practices on applying these ideas. If my logic is incorrect on anything, please don't hesitate to tell me where I'm wrong - I'm completely open to any...
2
1691
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 of this tiering system however I hope to increase the functionality of it soon quickly and easily. Anyway I am sort of getting confused with my class structures. I dont know whether it is better to use inheritance (subclasses) or composition...
0
10507
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10279
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10255
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.