Association
• An associationis 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 simply shows that objects are connected and can interact with each other.
• It does not show ownership or “part-of” meaning.
• Stronger forms of Association are Aggregation and Composition.
3.
Properties of Association
•Direction
• Can be unidirectional (one-way) or bidirectional (two-way).
• Example: Student knows Teacher (one-way) or Student Teacher
↔ (two-way).
• Multiplicity (Cardinality)
• Defines how many objects can be linked.
• One-to-One A person has one passport.
→
• One-to-Many A library has many books.
→
• Many-to-Many A student attends many courses, and each course has many students.
→
• Independence
• Both classes can exist independently.
• Association just defines interaction.
• No Ownership
• Neither class owns the other.
• Example: A library and a book are linked, but a book is not part of the library.
4.
Example:
class Book {
public:
stringtitle;
Book(string t) : title(t) {}
};
class Library {
public:
string name;
Library(string n) : name(n) {}
void showBook(Book& b) {
cout << name << " has the book: " << b.title << endl;
}
};
int main() {
Book b1("C++ Programming");
Library l1("Central Library");
l1.showBook(b1);
}
Output:
Central Library has the book: C++ Programming
6.
Aggregation
• Aggregation refersto a relationship between objects where one object,
known as the "whole," is composed of one or more other objects, referred
to as "parts."
• It represents a "Whole-part" or “has-a” relationship.
• Parts can exist independently of the whole.
• It is a weaker form of composition (loose coupling).
7.
Characteristics of Aggregation
•Modularity& Reusability Objects can be reused in different systems.
→
•Encapsulation Hides internal details, promotes clean interfaces.
→
•Complex System Representation Helps model hierarchical structures.
→
•Maintainability Easy to extend/modify without affecting the whole system.
→
•Loose Coupling Whole and parts are weakly connected.
→
•“Has-a” Relationship Whole contains parts but does not own them.
→
•Multiplicity One whole can be linked to many parts (0..*).
→
8.
Example:
class Book {
public:
stringtitle;
Book(string t) : title(t) {}
};
class Library {
private:
vector<Book*> books; // Aggregation: Library HAS Books
public:
void addBook(Book* b) {
books.push_back(b);
}
void showBooks() {
cout << "Library contains: ";
for (auto b : books) {
cout << b->title << " ";
}
cout << endl;
}
};
int main() {
Book b1("C++ Programming");
Book b2("Data Structures");
Book b3("Operating Systems");
Library lib;
lib.addBook(&b1);
lib.addBook(&b2);
lib.addBook(&b3);
lib.showBooks();
}
Output:
Library contains C++ Programming Data Structures Operating Systems
10.
Feature Association Aggregation
DefinitionA general relationship between two classes.
A special type of association that shows a
whole–part relationship.
Nature Objects just “know” each other. Whole “has” parts (whole–part structure).
Ownership No ownership, only connection.
Weak ownership – whole contains parts but
does not control them fully.
Dependency Objects are independent.
Parts are independent, but logically belong to
the whole.
Storage No storage of other object; used temporarily.
Whole stores references to its parts (e.g.,
collection/list).
Multiplicity One-to-one, one-to-many, or many-to-many. Often one-to-many (whole has multiple parts).
Lifecycle Both objects exist independently.
Parts exist independently, but are grouped
inside the whole.
UML Symbol Straight line ( ).
── Hollow diamond ( ).
◇──
11.
what is composition?
Composition is an object-oriented design principle that defines a strong 'has-a' relationship between two classes. In this relationship,
one class (the whole) is composed of one or more instances of another class (the parts), and the lifetime of the part is strictly tied
to the lifetime of the whole.
When a class uses composition, it means that the contained objects are created and managed entirely by the container class. If the
container (parent) object is destroyed, all the contained (child) objects are also destroyed. This is why composition is also called a strong
form of association or strong containment.
Key Points of Composition:
• Strong ownership: The parent is fully responsible for the child's lifecycle.
• Tight coupling: If the parent object is destroyed, the child objects are also destroyed.
• Composition is shown using a filled black diamond (◼) in UML diagrams.
• Child Cannot Exist Without Parent
• The lifetime of the child (part) is fully dependent on the parent (whole).
• Helps in Encapsulation : Composition can help hide internal parts (child objects) and simplify the external interface of the parent class.
• More Flexible than Inheritance
• Composition allows better control and flexibility than deep inheritance chains.
• Often used in design patterns like Strategy or Decorator.
12.
What is a"Has-A" Relationship?
In object-oriented programming and Unified Modeling Language (UML), a "has-a" relationship, also known as an association, describes
how one class contains or owns an instance of another class. It's a fundamental concept for structuring complex systems by showing how
different components relate to each other. Instead of inheriting behaviors ("is-a" relationship), one object holds a reference to another
object, thereby utilizing its functionalities or data.
Types of "Has-A" Relationships
• It can be implemented in two ways:
• Aggregation (weak ownership)
• Composition (strong ownership)
Example of Has -A relationship
Object Has-A Relationship
Car has an Engine
House has Rooms
Student has an Address
Phone has a Battery
13.
Example: House andRooms
House is the whole; Rooms are parts
Consider a typical house. A house is made up of multiple rooms—a living room, bedrooms, kitchen, bathroom, etc. In this analogy, the
House class would be the "whole" and the Room class would be the "part." The relationship between them perfectly illustrates
composition.
Rooms cannot exist independently without the House
A room, in the context of being part of a house, cannot exist on its own if the house does not exist. You can't have a "floating" living room
without a structure containing it. The very definition of these rooms is tied to their containment within a house.
Composition relationship perfectly models this scenario
This strong, dependent lifecycle and exclusive ownership make the House-Rooms relationship an ideal example of composition. It clarifies
that a Room instance's existence is solely dependent on its enclosing House instance, illustrating the power of composition in designing
tightly coupled systems.
14.
code explain
#include <iostream>
usingnamespace std;
class Room{
public:
Room() {
cout << "Room createdn"; } };
class House {
Room room; // Composition House "has-a" Room
public:
House() {
cout << "House createdn";
} };
int main() {
House h; //Creating House also creates Room due to composition
return 0; }
15.
UML Symbol for
Composition
TheUML symbol for composition is designed to visually convey its strong
meaning at a glance. It consists of a solid line connecting the two classes
involved in the relationship: the "whole" class (parent) and the "part" class
(child). At the end of the line connected to the "whole" class, a filled (black)
diamond is placed. This filled diamond is the unmistakable indicator of
composition.
Example in UML Terms:
House ⬛─── Room
• If the House is deleted, the Room cannot exist.
The Room is fully managed by the House.
16.
Composition vs Aggregation:Key Differences
Composition vs Aggregation: Key Differences
Aspect Composition Aggregation
Definition Strong "has-a" relationship Weak "has-a" relationship
Ownership Parent fully owns the child Parent uses the child
Lifetime Dependency Child cannot exist without parent Child can exist independently
UML Symbol Filled diamond (◼) Hollow diamond ( )
◇
Tightness Tightly coupled Loosely coupled
Real-world Example House Room
→ School Teachers
→
Object Creation Usually created inside parent Usually passed to parent
Destruction Deleting parent deletes child too Deleting parent doesn't affect child
Editor's Notes
#2 Association is a general relationship between two classes.
It is the simplest relationship in object-oriented design.
Association = “knows” relationship.
Doctor treats patient
#3 UML Symbol
Represented by a straight line connecting two classes.
#4 Here, Library only interacts with Book through a function.
It does not store the Book inside itself.
The relationship is temporary → Library “knows about” Book for that moment.
If the function ends, the link disappears.
Here, Library and Book classes are independent.
A Library can have many Books, but a Book is not part of the Library (it can exist elsewhere).
They just know each other through association.
If the Library is deleted, Books still exist.
Association = relationship without ownership.
#6 In other words, aggregation is a group, body, or mass composed of many distinct parts or individuals
looser coupling between the objects, allowing the parts to exist independently of the whole.
#7 Flexibility & Scalability → Parts can be added or removed at runtime.
Object Collaboration → Encourages communication via well-defined interfaces.
#8 Here, Library maintains a collection of Books.
It stores references to Books inside itself.
This makes Library a “whole”, and Books its “parts”.
If the Library object is destroyed, the Books still exist.