answersLogoWhite

0


Best Answer

Composition relates to the way in which complex objects can be constructed from smaller, simpler objects. We refer to this as object composition. While simple objects typically contain one or more embedded primitive member attributes, such as int, char, float and so on, composite objects typically embed other objects (often in addition to other primitive attributes), and each of those objects may themselves be composite or they may be simple. This allows highly complex object models to be constructed such that each object within a composition is self-contained and ultimately operates no differently to how it would operate if it were a standalone object. The composite object effectively acts as an object coordinator, calling specific methods upon the simpler objects as required. In other words, the actual workload is delegated to the object or objects that are actually responsible for that workload, and they themselves may delegate to simpler objects, and they to theirs. The composite object's only real workload is in processing any relevant results of those delegations. In other words, a simple function call through a member method of the composite object can produce a cascade of member methods within the embedded objects, each of which works completely independently, but the results of which can be combined by the containing object in order to produce highly complex behaviour.

By way of an example, a motor car is built up from thousands of individual but ultimately simple components, such as nuts and bolts, switches, cogs, levers, wheels and so on. Some of those simple objects are combined to produce more complex objects, such as an engine, a transmission unit, a steering mechanism, door locks, and so, on. Ultimately, they all combine to produce a car. But no matter how complex a composite object is, the behaviour of their embedded objects does not change: a nut and bolt is still a nut and bolt whether it is holding a door hinge onto a chassis, or a suspension strut to a wheel hub. In other words, the job of physically holding one component to another is delegated to the nut and bolt, but is coordinated according to the object that embeds the nut and bolt, while that object is coordinated and delegated to by the object that contains it, and so on.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

A composition is any class that embeds another class rather than inherits from it. You may embed as many classes as required in a composition. You may also combine composition with inheritance.

class A {};

class B { A a };

In the above example, class A is embedded within class B, thus class B is a composition. The same class could also be declared with inheritance:

class B: class A {};

Which method you use is often a matter of preference, however composition should always be considered before inheritance as it is generally much easier to work with. As a general rule of thumb, if you need to expose a minor interface with major specialisation then you will typically use composition. But if you need to expose a major interface with partial specialisation then you will generally use inheritance.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Composition refers to a class that contains one or more embedded objects and is often used as an alternative to inheritance. The following example demonstrates the concept:

class foo {};

class derived : public foo {

};

class composed {

foo m_foo;

};

We have not shown any interface or implementation at this stage, but it is important to realise that the derived class is a kind of foo, but the composed class is not. That is, we can pass instances of derived to functions that expect instances of foo, but we cannot pass instances of composed to those same functions.

Classes that are intended to be derived from will include one or more virtual methods (as well as a virtual destructor). This ensures that when you call those methods, the most-derived implementation is always invoked, thus ensuring correct behaviour even when the runtime type of your derivative is not known at compile time.

A class that has no virtual methods is intended to be used solely in compositions. However, any class can be used in a composition, including those with virtual methods. Assuming virtual methods are not an issue, you can use whichever method most suits your needs. If you need an "is-a" relationship between your class and the existing class, then you will use inheritance. If you do not need an "is-a" relationship or the existing class has no virtual methods then you can use composition. In some cases you may even use both, inheriting from one class while embedding another.

When you embed an object in another object, the new object must provide the interface to that existing object. In some cases the object may have no invariants in which case the existing interface will suffice and the object may be declared public. However, if there are invariants, you must declare the object private and implement your own interface to cater for those invariants. Member data (including embedded objects) should never be declared protected without good reason, as this can easily undermine encapsulation.

With inheritance, you not only gain the "is-a" relationship, you also inherit the public and protected interface, and can easily override the virtual interface to provide a more specific implementation. Where you have the choice of composition or inheritance, inheritance is often the best choice. However, if your derivative also employs composition, there is a danger of slicing objects. That is, a function that expects a base class reference can only see the base class members, it cannot see the derived class' composition members unless you use expensive runtime type information to dynamically downcast your base class (which is usually a sign of poor class design). However, making good use of virtual methods can often overcome this problem.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Explain term composition with example in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Explain what the term data means and give an example?

shiting it


Which term refers to the way a composition is constructed?

The term that refers to a way a composition is constructed is "Form".


What is the term for the sum of a number and all its descending numbers for example 10 plus 9 plus 8 plus 7 etc?

i dont know but the term for the number times the numbers below it is factorial and is represented by an ! for example: 5!=5x4x3x2x1=120


How do you use the term physical features in a sentence?

Example sentence - His physical features were difficult to explain.


Can you Define the term symphony and explain its internal sectional development?

What is the internal sectional development of the term symphony


Is it possible to find three solutions of the equation x3 plus 2x2 plus 3x plus 6 equals 0 Explain why or why not?

The variable term, X^3, is a third order polynomial term and will render three solutions, though one of those may be zero.


What is video composition?

Without a context, it is difficult to give you the answer you want. You could term a filmed sequence as a 'video composition' in much the same way that you could term a musical sequence as a 'musical composition'.


Can the term solution be used to identify a mixture that has the same composition throughout?

The term solution can be used to identify a mixture that has the same composition throughout.


Explain the method of translation of historical institutional term?

"Explain the method of translation of historical institutional term?" Institutional term and National institutional term? "Explain the method of translation of historical institutional term?"


What term describes genetic composition?

DNA?


What is a term in a math expession?

A "term" is each of the parts of the expression that are combined with plus or minus symbols. For example, in x2 + xy + y2, you have three terms; one before the first plus symbol; one between the two plus symbols; one after the second plus symbol.


What does the term composition mean as it applies to igneous rock?

it can mean two things, (1)mineralogical composition or (2)Chemical composition