Composition Lec1
Composition Lec1
Composition Lec1
Composition:
Composition is referred to building a complex thing with the use of smaller and simple parts.
the way of implementing complex objects using simpler or smaller ones.
The object composition concept work on the model of has-a relationship among two
different objects.
Complex objects are often referred to as parent components or whole components while
objects which are simpler or smaller are often referred to as child components or part
components.
it gives us the freedom to design complex classes by using simpler and smaller manageable
parts.
composition in an uncomplicated way by accessing the classes as a member function in
other classes.
In object composition, the object created is a part of another object which is referred to as
a sub-object. A Sub-object is destroyed when an object composition is destroyed, known as
a do and die relationship.
Types of Object Composition in C++
Object composition is basically of the following subtypes:
Composition: composition relationship is also called a part-whole relationship in which the part
component can only be a part of a single object at the same time.
In composition relationships, the part component will be created when the object is created and the
part will be destroyed when the object is destroyed.
A person's body and heart is a good example of a part-whole relationship where if a heart is part of a
person's body then it cannot be a part of someone else's body at one time.
To be qualified as a composition, the part and object must follow the relationships described below:
The part component or child component (also referred to as a member) belongs to a single
object (also referred to as class).
The part component can show its presence with the help of an object.
The part(member) component is the element of the object.
The part component has no idea about the presence of the object.
There are certain sets of rules for creating and destroying parts of the object:
There is no need to create a part (member) of the objects until it is not required.
The composition should use the part that was given to it as an input rather than
completely creating a part by itself.
The composition can assign the part's destruction to any other object.
Aggregation :
the part component can belong to more than one object at the same time.
It is also a part-whole relationship.
The object(class) will not be responsible for the existence or presence of the parts.
To be qualified as aggregation, the part and object must follow the relationship
describes below:
The part component or child component (also referred to as a member) belongs to
more than one object (also referred to as a class) at the same time.
The part component does not show its presence with the help of an object.
The part(member) component is the element of the object.
The part component does not have an idea about the presence of the object.
Object Delegation :