0% found this document useful (0 votes)
15 views18 pages

Lab Manual 05 - Association, Composition and Aggregation

Uploaded by

f2024408246
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)
15 views18 pages

Lab Manual 05 - Association, Composition and Aggregation

Uploaded by

f2024408246
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/ 18

Lab Manual: Object Oriented Programming

University of Management and Technology,


Lahore Campus
Lab- 06 Manual

Lab Instructor: Jawad Akhtar


Department of INFS
Email: [email protected]

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

Department of INFS, UMT, Lahore. 1


Lab Manual: Object Oriented Programming

Lab 5: Relationships in Object Oriented Programming

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.

2. Activity Time boxing


Task No. Activity Name Activity time Total Time
1 Lecture 20 minutes 20 minutes
2 Setting-up Visual Studio 5 minutes 5 minutes
3 Example Tasks 20 minutes 20 minutes
4 Practice tasks 65 minutes 65 minutes
5 Evaluation Task 40 minutes 40 minutes
Total Time 150 Minutes

3. Objective of the Experiment


After completing this lab, the student should be able to:
 model relationship between existing classes
 distinguish between association, aggregation, and composition
 understand difference between aggregation and composition in terms of ownership and
life cycle

Department of INFS, UMT, Lahore. 2


Lab Manual: Object Oriented Programming

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

4.2 Simple Association


The two interacting objects have no intrinsic relationship with other object. It is the weakest link between
objects. It is a reference by which one object can interact with some other object.
Customer gets cash from cashier Employee
works for a company Ali lives in a house
Ali drives a car

Department of INFS, UMT, Lahore. 3


Lab Manual: Object Oriented Programming

It is generally called as “association” instead of “simple association”


Kinds of Simple Association

Simple association can be categorized in two ways,


• With respect to direction (navigation)
• With respect to number of objects (cardinality)

Kinds of Simple Association w.r.t Navigation


With respect to navigation association has the following types,
a. One-way Association
b. Two-way Association

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:

Department of INFS, UMT, Lahore. 4


Lab Manual: Object Oriented Programming

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:

Employee works for company Company


employs employees

Two-way Association - Example

Yasir is a friend of Ali


Ali is a friend of Yasir

Kinds of Simple Association w.r.t Cardinality


With respect to cardinality association has the following types,
a. Binary Association
b. Ternary Association
c. N-ary Association

a. Binary Association
It associates objects of exactly two classes; it is denoted by a line, or an arrow between the associated
objects.
Example:

Association “works-for” associates objects of exactly two classes

Association “drives” associates’ objects of exactly two classes

Department of INFS, UMT, Lahore. 5


Lab Manual: Object Oriented Programming

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>

using namespace std;

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;

Teacher(const string& name) : name(name), studentCount(0) {}

void addStudent(Student* student) {


if (studentCount < 10) {
students[studentCount++] = student;
} else {
cout << "Cannot add more students, limit reached.\n";
}
}

void printStudents() {
cout << "Teacher " << name << " teaches:\n";
for (int i = 0; i < studentCount; ++i) {
cout << " - " << students[i]->name << "\n";
}

Department of INFS, UMT, Lahore. 6


Lab Manual: Object Oriented Programming
}
};

int main() {
Student s1("John");
Student s2("Emily");

Teacher t1("Mr. Smith");


t1.addStudent(&s1);
t1.addStudent(&s2);

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

Department of INFS, UMT, Lahore. 7


Lab Manual: Object Oriented Programming
Example – Composition of Chair

Composition is stronger relationship:


Composition is a stronger relationship, because
Composed object becomes a part of the composer
Composed object can’t exist independently

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

They can’t exist independently

Department of INFS, UMT, Lahore. 8


Lab Manual: Object Oriented Programming
Code –Example: University has department
#include <iostream>
#include <string>

using namespace std;

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 addRoom(const string& roomName) {


if (roomCount < 10) {
rooms[roomCount++] = new Room(roomName);
} else {
cout << "Cannot add more rooms, limit reached.\n";
}
}

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
}

Department of INFS, UMT, Lahore. 10


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

Department of INFS, UMT, Lahore. 11


Lab Manual: Object Oriented Programming

Example – Aggregation

Aggregation is weaker relationship


Aggregation is weaker relationship, because
• Aggregate object is not a part of the container
• Aggregate object can exist independently

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

Code-Example: Company has Employee


#include <iostream>

using namespace std;


class Employee{ private:
int id;
string EmpName;
public:
Employee(int id=0,string name=""){

this->id=id;
this->EmpName=name;
}
void show(){ cout<<"\
nEmployee ID:"<<id;
cout<<"\n Employee Name:"<<EmpName;

}
};
class company{
private:
string cmpName;
Employee *emp;

Department of INFS, UMT, Lahore. 12


Lab Manual: Object Oriented Programming

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;}

Department of INFS, UMT, Lahore. 13


Lab Manual: Object Oriented Programming

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

 Attributes are day, month and year


 Design Nullary constructor to set the default values
 Design parameterize constructor to initialize with user defined values
 Design show function to show the person information.

Implement to these classes and illustrate the relationship.

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.

Implement to these classes and illustrate the relationship.

Department of INFS, UMT, Lahore. 14


Lab Manual: Object Oriented Programming

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>

using namespace std;


class Birthday{
private:
int day,month,year;
public:
Birthday():day{0},month{0},year{0}{}
Birthday(int d,int m,int y){
day=d;
month=m;
year=y;
}
void show(){
cout<<day<<"/"<<month<<"/"<<year;
}

};
class
Person{ priv
ate:
string name,city;
Birthday bth;
public:
Person(string name,string cName,Birthday bd){
this->name=name;
city=cName;
bth=bd;
}

void display(){ cout<<"\


nName::"<<name; cout<<"\
ncity::"<<city<<endl; bth.show();

}
};
int main()
{
Birthday bth(24,10,1990);
Person p("ALi","Mia Channu",bth);
p.display();
return 0;}

Department of INFS, UMT, Lahore. 15


Lab Manual: Object Oriented Programming

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>

using namespace std;


class Course{
private:
string
CName;
string Pdate;
public:

Course(string name="",string date=""){


CName=name;
Pdate=date;
}
void Display(){
cout<<"\nName:"<<CName<<"\n Publish date:"<<Pdate;
}

};
class
Teacher{ priv
ate:
string name;
int id;
Course *CourseName;
public:
Teacher(string name,int id,Course *cs)
{ this->name=name;

Department of INFS, UMT, Lahore. 16


Lab Manual: Object Oriented Programming
this->id=id;
CourseName=cs;
}
int
getid()const{ re
turn id;
}
string getname()const{
return name;
}
void display(){ cout<<"\
nID::"<<getid(); cout<<"\
nName::"<<getname()<<endl;
CourseName->Display();

}
};
int main()
{
Course
cs("DSA","12/03/1960");
Teacher T("ALi",123,&cs);
T.display();
return 0;
}

Department of INFS, UMT, Lahore. 17


Lab Manual: Object Oriented Programming

6. Practical Tasks

Tasks 1 Time Allowed:120


Minutes

Consider four classes: University, Department, Location, and Employee

Class University has


 Two attributes of string type name and sector
 Two attributes name dept of type Department and geo of type location
Class Location has
 Two attributes of int type x and y coordinates.
 Public functions to set and display the location
Department has
 One attribute of string type deptName;
 One attribute of Emp of type Employee
Class Employee has
 Three attributes are Id ,name and age
 Make appropriate public functions to set and get the information

Implement all of these classes while illustrating the concept of aggregation


and composition in terms of ownership and life-cycle.

Department of INFS, UMT, Lahore. 18

You might also like