Lab Manual 05 - Association, Composition and Aggregation
Lab Manual 05 - Association, Composition and Aggregation
Lab: 06
Table of Contents
1. Introduction
2. Activity Time boxing
3. Objective of the Experiment
4. Concept Map
Association
Composition
Example
Aggregation
Example
5. Walkthrough Tasks
Task 01
Task 02
Code (Task 01 & 02)
6. Practice Tasks
Practice Task 1
1. Introduction
In OOP, various kinds of relationships (i.e. inheritance, association, aggregation, composition)
exist between various classes/objects.
Inheritance is modeled as is-a relationship. Inheritance exists between classes and the main goal of
inheritance is to make objects that would represent specialized versions of generalized original objects.
For example, a car is-a vehicle, manager is-an employee etc.
On the other hand, association and aggregation/composition represent uses-a and has-a
relationship, respectively between objects. It is based on the object-oriented design where an
object can contain other object(s). Association specifies that objects of different kinds are
connected with each other and there exist no ownership and lifecycle dependency. In
advance, aggregation and composition are two types of association representing weak and
strong (whole-part) relationship between contained (whole) and owing (part) objects.
Therefore, aggregation and composition are other forms of code reusability along with
inheritance. Aggregation is a special kind of association represents ownership relationship
between objects. Composition is special kind of aggregation which represents ownership
between objects along with the existence of life-cycle dependency. A department-employees
relationship is an example of aggregation whereas university-departments relationship is an
example of composition.
4. Concept Map
4.1 Association
Association represents a relationship between two classes that allows one object instance to
cause another to perform an action on its behalf. It is a more general relationship, indicating
that objects of one class are connected to objects of another class.
Kinds of Association:
There are two main types of association which are then further subdivided i.e
1. Class Association
2. Object Association
1. Class Association
Class association is implemented in terms of Inheritance. Inheritance implements
generalization/specialization relationship between objects. Inheritance is considered class association.
• In case of public inheritance it is “IS-A” relationship.
• In case of private inheritance, it is “Implemented in terms of” relationship.
This relationship ensures that public members of base class are available to derived class in case of public
inheritance.
When we create objects of classes in which we have implemented inheritance relationships we are forcing
the inheritance relationship between created objects. In this case the derived class objects will also contain
base class objects attributes and methods.
2. Object Association
It is the interaction of stand alone objects of one class with other objects of anther class.
It can be of one of the following types,
• Simple Association
• Composition
• Aggregation
a. One-way Association
In One way association we can navigate along a single direction only, it is denoted by an
arrow towards the server object.
Examples:
b.Two-way Association
In two way association we can navigate in both directions, it is denoted by a line between the
associated objects
Examples:
a. Binary Association
It associates objects of exactly two classes; it is denoted by a line, or an arrow between the associated
objects.
Example:
b. Ternary Association
It associates objects of exactly three classes; it is denoted by a diamond with lines connected to associated
objects.
Example
Objects of exactly three classes are associated
c. N-ary Association
An association between 3 or more classes its practical examples are very rare.
#include <iostream>
#include <string>
class Student {
public:
string name;
Student(const string& name) : name(name) {}
};
class Teacher {
public:
string name;
Student* students[10]; // Assuming a maximum of 10 students for simplicity
int studentCount;
void printStudents() {
cout << "Teacher " << name << " teaches:\n";
for (int i = 0; i < studentCount; ++i) {
cout << " - " << students[i]->name << "\n";
}
int main() {
Student s1("John");
Student s2("Emily");
t1.printStudents();
return 0;
}
4.3 Composition
An object may be composed of other smaller objects, the relationship between the “part” objects and the
“whole” object is known as Composition, and Composition is represented by a line with a filled-diamond
head towards the composer object
Example – Composition of Ali
Example I
Ali is made up of different body parts
They can’t exist independent of Ali
Example II
Chair’s body is made up of different parts
class Room {
public:
string name;
Room(const string& name) : name(name) {}
void describe() {
cout << "Room: " << name << "\n";
}
};
class House {
private:
Room* rooms[10]; // Assuming a maximum of 10 rooms for simplicity
int roomCount;
public:
House() : roomCount(0) {}
void describeHouse() {
cout << "House contains:\n";
for (int i = 0; i < roomCount; ++i) {
rooms[i]->describe();
}
}
~House() {
for (int i = 0; i < roomCount; ++i) {
delete rooms[i];
}
}
};
int main() {
House house;
house.addRoom("Living Room");
house.addRoom("Bedroom");
house.addRoom("Kitchen");
house.describeHouse();
return 0;
Department of INFS, UMT, Lahore. 9
Lab Manual: Object Oriented Programming
}
4.4 Aggregation
An object may contain a collection (aggregate) of other objects, the relationship between the container
and the contained object is called aggregation, Aggregation is represented by a line with unfilled-diamond
head towards the container
Example – Aggregation
Example – Aggregation
Example I
Furniture is not an intrinsic part of room
Furniture can be shifted to another room, and so can exist independent of a particular room
Example II
A plant is not an intrinsic part of a garden
It can be planted in some other garden, and so can exist independent of a particular garden
this->id=id;
this->EmpName=name;
}
void show(){ cout<<"\
nEmployee ID:"<<id;
cout<<"\n Employee Name:"<<EmpName;
}
};
class company{
private:
string cmpName;
Employee *emp;
public:
company(string name="")
{ cmpName=name;
}
void setEMp(Employee *emp)
{ this->emp=emp;
}
void display(){
cout<<"The Company\t "<<cmpName<<"\t has Employee \n";
emp->show();
}
};
int main()
{
company cmp("Bata");
Employee emp(1234,"Ali
Ahmed"); cmp.setEMp(&emp);
cmp.display();
return 0;}
5. Walkthrough Tasks
5.1
The person has birthday. The person class has following behavior and attributes
He has attributes name and city, Birthday
Design parameterize constructor to initialize with user defined values
Design Display function to show the person information.
The Birthday class has following attributes
5.2
The Teacher has a course. The Teacher has following type of attributes
He has id, name and course name
Design default argument constructor to set the default values
Getter functions to get the information
The course has following type of attributes
Name and publication date
Design default argument constructor to set the default values
Design Display function to show the Course information.
Example Task 01: Person has a birthday; you can see the ownership and life-dependency. Both has
strong relationship, if the person dies then birthday will be destroyed. So, both has relation type
“Composition”.
#include <iostream>
};
class
Person{ priv
ate:
string name,city;
Birthday bth;
public:
Person(string name,string cName,Birthday bd){
this->name=name;
city=cName;
bth=bd;
}
}
};
int main()
{
Birthday bth(24,10,1990);
Person p("ALi","Mia Channu",bth);
p.display();
return 0;}
Example Task 02: The Teacher has course .You can see the ownership but no life dependency .If
person one of them die ,there will be no impact to other so both has weak relationship that is known as
aggregation.
#include <iostream>
};
class
Teacher{ priv
ate:
string name;
int id;
Course *CourseName;
public:
Teacher(string name,int id,Course *cs)
{ this->name=name;
}
};
int main()
{
Course
cs("DSA","12/03/1960");
Teacher T("ALi",123,&cs);
T.display();
return 0;
}
6. Practical Tasks