Oop Difference Between Association and Aggregation
Oop Difference Between Association and Aggregation
Aggregation Association
Special kind of association where there is It means there is almost always a link
whole-part relation between two objects between objects
Diamond shape structure is used next to the Line segment is used between the
assembly class. components or the class
In the classes given above, B uses objects of class A as its data members. Hence, B
is a complex class that uses a simple class A. Let’s have a look at the program that
makes use of the composition.
Below is the implementation of the composite class:
C++
#include <iostream>
// Simple class
class A {
public:
int x;
// COnstructor initializing
A() { x = 0; }
A(int a)
x = a;
};
// Complex class
class B {
int data;
A objA;
public:
// data members
B(int a)
: objA(a)
data = a;
// A and B
void display()
<< endl;
};
// Driver code
int main()
B objb(25);
objb.display();
return 0;
Output:
Constructor A(int a) is invoked
Data in object of class B = 25
Data in member object of class A in class B = 25
Types of Object Compositions:
There are two basic subtypes of object composition:
1. Composition: The composition relationships are part-whole relationships where a
part can only be a part of one object at a time. This means that the part is created
when the object is created and destroyed when the object is destroyed. To qualify as
a composition, the object and a part must have the following relationship-
1. The part (member) is part of the object (class).
2. The part (member) can only belong to one object (class).
3. The part (member) has its existence managed by the object (class).
4. The part (member) does not know about the existence of the object (class).
There are some variations on the rule of creating and destroying parts:
1. A composition may avoid creating some parts until they are needed.
2. A composition may opt to use a part that has been given to it as input
rather than creates the part itself.
3. A composition may delegate the destruction of its parts to some other
object.
2. Aggregation: The aggregation is also a part-whole relationship but here in
aggregation, the parts can belong to more than one object at a time, and the whole
object is not responsible for the existence of the parts. To qualify as aggregation, a
whole object and its part must have the following relationships:
1. The part (member) is part of the object (class).
2. The part (member) can belong to more than one object (class) at a time.
3. The part (member) does not have its existence managed by the object
(class).
4. The part (member) does not know about the existence of the object (class).
Benefits of Object Composition:
Using object composition can provide the following benefits:
1. Reuse existing codes: Object composition allows to reuse of the existing
code without a need to model an is-a relationship as usually done in
inheritance.
2. Clean design APIs: Using object composition can help to design clean
and composed APIs. This is because when the class is composed it is
easier to decide if the referenced class will become part of the API or will
be hidden.
3. Change in implementation of the class used in the composition
without requiring external clients: Composition also allows to make
code easier to change and adapt if necessary. The internal classes can be
changed without any side effects and changes can be handled internally.
Object Delegation
Object Delegation means using the object of another class as a class member of
another class. It is known as object delegation. Delegation can be an alternative to
inheritance, but in an inheritance, there is an i-s a relationship, but in the delegation,
there is no inheritance relationship between the classes.
C++
// Object Delegation
#include <iostream>
public:
};
class Second {
First ob;
public:
};
// Driver Code
int main()
Second ob1;
ob1.print();
return 0;
Output:
The Delegate
Benefits of object delegation:
1. The reuse class can be changed without changing the reusing class.
2. Multiple reusing classes can share reused class variables simultaneously.
3. Variables and methods of reusing and the reused classes can be on
separate computers.
4. It works in single inheritance languages.
Object composition vs Object Delegation
S
Object Composition Object Delegation
No.
It is about passing
1 It is about relationships between objects. work from one object
to another.
The composition has some implications for object The delegation does
3 lifecycle, the parent object owns the child and the not have this
child doesn’t have much reason to exist on its own. implication.