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

OOP - Aggregation, Composition

The document discusses object-oriented programming concepts, focusing on relationships such as association, aggregation, and composition. It explains the definitions and characteristics of these relationships, including their representations in UML and examples in code. Additionally, it covers multiplicity in associations and provides code snippets to illustrate aggregation and composition in practice.

Uploaded by

yilari6159
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

OOP - Aggregation, Composition

The document discusses object-oriented programming concepts, focusing on relationships such as association, aggregation, and composition. It explains the definitions and characteristics of these relationships, including their representations in UML and examples in code. Additionally, it covers multiplicity in associations and provides code snippets to illustrate aggregation and composition in practice.

Uploaded by

yilari6159
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Object-Oriented

Programming
WEEK 05 Abeeha Sattar
Types of Relationships:

 Has – a / has – an
 An object contains one or more objects of other classes as members
 Association/Aggregation/Composition fall into this category
 Is – a / is – an
 A class is an extension of another class
 Inheritance falls into this category
Association in OOP

 Association is a relationship between two objects.


 It defines the multiplicity between objects. Some of the multiplicities are:
 one-to-one, 1
1
 one-to-many, 1
*
 many-to-one, *
1
 many-to-many. *
*
 You can have other numbers as multiplicities as well
More on Multiplicity
Example of Association
Aggregation

 Aggregation is a specialized form of association between two or more objects in which


each object has its own life cycle but there exists an ownership as well.
 Aggregation is also called a “Has-a” relationship.
 Aggregation is usually represented in UML using a line with a
hollow diamond towards the class that owns the other object.
Composition

 Composition is a special case of aggregation.


 In a more specific manner, a restricted aggregation is called composition.
 When an object contains the other object, if the contained object cannot exist without
the existence of container object, then it is called composition.
 Composition is represented in UML using a line connecting
the objects with a solid diamond at the end of the object that
owns the other object.
Examples

Scrollbar
1 1
Window Titlebar
1 1
Menu
1 1 ..

Note that these are partial class diagrams.


We’ll always be making complete ones!
Composition in Code

class Engine{};

class Car {
Engine e;
public:
Car() {} //empty default constructor
};
Aggregation in Code (pt. 1)

class Person { void setSeated(bool s) {


bool seated = false; seated = s;
public: }
void isSeated() { };
if (seated)
cout << "I've been
seated!" << endl;
else
cout << "Not seated yet!"
<< endl;
}
Aggregation in Code (pt. 2)
class Car { void setP(Person* p) {
Engine e; this->p = p;
Person *p; this->p->setSeated(true);
public: }
Car() { void unloadPassenger() {
p = nullptr; p->setSeated(false);
} p = nullptr;
Car(Person* p) { }
this->p = p; ~Car() {
this->p->setSeated(true); if (p) {
} p->setSeated(false);
Person* getP() { p = nullptr;
return p; }
} }
};
Aggregation in Code (pt. 3)

int main()
{
Person p;
p.isSeated();

Car c(&p);
p.isSeated();

c.setP(&p);
p.isSeated();

c.unloadPassenger();
p.isSeated();

return 0;
}
Mid Term – I Discussion
Fin.

3
:

You might also like