0% found this document useful (0 votes)
60 views

Oop Difference Between Association and Aggregation

Aggregation and association are two types of relationships between classes in object-oriented design. Aggregation describes a whole-part relationship where the whole object has ownership over the part object. Association describes a relationship between two independent classes. Some key differences are that aggregation uses a "has-a" relationship and ownership while association uses a non-owning "has-a" relationship. Object composition builds complex objects using simpler object parts, creating a whole-part relationship similar to aggregation. Object delegation involves one object passing work to another object rather than having a containing relationship.

Uploaded by

wajid Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Oop Difference Between Association and Aggregation

Aggregation and association are two types of relationships between classes in object-oriented design. Aggregation describes a whole-part relationship where the whole object has ownership over the part object. Association describes a relationship between two independent classes. Some key differences are that aggregation uses a "has-a" relationship and ownership while association uses a non-owning "has-a" relationship. Object composition builds complex objects using simpler object parts, creating a whole-part relationship similar to aggregation. Object delegation involves one object passing work to another object rather than having a containing relationship.

Uploaded by

wajid Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Difference between Association and Aggregation

Association: An association is defined as an organization of people with a common


purpose and having a formal structure. It represents a binary relationship between two
objects that describes an activity. It is a relationship between objects. For example, A doctor
can be associated with multiple patients.

Aggregation: An aggregation is a collection, or the gathering of things together. This


relationship is represented by a “has a” relationship. In other words, aggregation is a group,
body, or mass composed of many distinct parts or individuals For example, phone number
list is an example of aggregation.
Difference between Aggregation and Association:

Aggregation Association

Aggregation describes a special type of an Association is a relationship between


association which specifies a whole and part two classes where one class use
relationship. another.

It in flexible in nature It is inflexible in nature

Special kind of association where there is It means there is almost always a link
whole-part relation between two objects between objects

It is represented by a “has a”+ “whole-part” It is represented by a “has a”


relationship relationship
Aggregation Association

Diamond shape structure is used next to the Line segment is used between the
assembly class. components or the class

Object Composition-Delegation in C++ with Examples


Object Compositions
An object is a basic unit of Object-Oriented Programming and represents real-life
entities. Complex objects are objects that are built from smaller or a collection of
objects. For example, a mobile phone is made up of various objects like a camera,
battery, screen, sensors, etc. This process of building complex objects from simpler
ones is called object composition.
In object-oriented programming languages, object composition is used for objects
that have a “has-a” relationship with each other. Therefore, the complex object is
called the whole or a parent object whereas a simpler object is often referred to as a
child object.
Syntax:
class A
{
// body of a class
};
class B
{
A objA;
public:
B(arg-list) : objA(arg-list1);
};

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>

using namespace std;

// Simple class

class A {

public:

int x;

// COnstructor initializing

// the data members

A() { x = 0; }

A(int a)

cout << "Constructor A(int a) is invoked" << endl;

x = a;

};

// Complex class
class B {

int data;

A objA;

public:

// COnstructor initializing the

// data members

B(int a)

: objA(a)

data = a;

// Function to print values

// of data members in class

// A and B

void display()

cout << "Data in object of class B = " << data

<< endl;

cout << "Data in member object of "


<< "class A in class B = " << objA.x;

};

// Driver code

int main()

// Creating object of class B

B objb(25);

// Invoking display function

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++

// C++ program to illustrate the

// Object Delegation

#include <iostream>

using namespace std;


class First {

public:

void print() { cout << "The Delegate"; }

};

class Second {

// Creating instance of the class

First ob;

public:

void print() { ob.print(); }

};

// 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.

In composition, the methods of the inner object may Delegation involves


2
be used only privately and not re-exposed. re-exporting methods.

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.

You might also like