Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views14 pages

OOP W5 - Class Relationships and Class Members

Uploaded by

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

OOP W5 - Class Relationships and Class Members

Uploaded by

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

Object Oriented Programming

Class Relationships and Class Members

1
Inter-Class Relationships

▪ Classes do not live alone. They work together using different types of
Relationships. The strength of a class relationship is based on how
dependent the classes involved are on each other.

▪ Two classes that are strongly dependent on one another are said to
be tightly coupled; changes to one class will most likely affect the other
class.

▪ Tight coupling is usually, but not always, a bad thing; therefore, the
stronger the relationship, the more careful you need to be.

2
Inter-Class Relationship Types

Weaker Class Relationship Stronger Class Relationship

Dependency Association Aggregation Composition Inheritance

“depends-on” “uses” “has-a” “contains” “is-a”

When objects of When objects of When one class When one class When one class is
one class work one class work owns but share a contains objects of a type another
briefly with objects with another class reference to another class class
of another class for some objects of another
prolonged time class

3
Dependency
▪ A dependency is a relationship between two or more objects
in which an object depends on a 3rd party objects for its
implementation.

▪ For example, receiving 3rd party objects as parameters in a


method. If one of these 3rd party objects change, the other
object can be impacted.

▪ We do not show all dependencies on a class diagram.


Instead only show relevant dependencies.

▪ Course object is not stored as a field in CourseSchedule


object. Only its the methods use Course as a parameter.

4
Association
▪ Whenever two objects are related
which each other the relationship is
called as an association.

▪ Therefore when one object is


changed the other is affected,
therefore it is also a dependency but
much stronger.

▪ The key here is, references to


associated object is stored as a field
in the other object class.

5
Aggregation
▪ Aggregation is much stronger type of association.

▪ If you could use the words "part-of" or "has-a" to


describe the relationship between two classes then
that relationship is an aggregation.

▪ The “part” object can have a lifecycle which is wider


than its “whole” object, therefore if you destroy the
whole object, the part object is not necessarily
destroyed.

▪ In this example, books will remain even when the


library is dissolved.

6
Composition
▪ Composition is more stronger type of aggregation.

▪ It is also like aggregation a whole-part relationship


which can be described using the words
“contains” or “entirely made of”.

▪ The key difference in composition is the part


objects cannot have an independent life cycle,
they live and die with the whole object.

▪ In this example, if the book is removed the its


chapters also should be destroyed.

7
Inheritance
▪ As we discussed previously this is a relationship
between classes which can be described using the
words “is-a” or “is-a kind of”.

▪ The class which is described as “is-a” becomes the


child class which derives its features from the other
class which is called the base class.

▪ Although this has strongest dependency it adds to


extensibility and reusability.

▪ In the example here, the SavingsAccount “is-a” a


BankAccount.

8
A Class is a 3-Compartment Box

▪ A class contains 3 components:


A Class is a 3-Compartment Box
encapsulating Data and Functions
▪ Class name (or identifier): identifies the class.

▪ Data Members or Variables (attributes, states, fields):


contains the static attributes of the class.

▪ Member Functions (methods, behaviours, operations):


contains the dynamic operations of the class.

▪ A class encapsulates the static attributes (data) and


dynamic behaviours (operations that operate on the data)
in a box.

9
Accessors & Mutators
▪ Mutator (Setter) : a member function that stores a value
in a private a member variable, or changes its value in
some way. (e.g. setLength, setWidth )

▪ Accessor (Getter) : a member function that retrieves a


value from a private member variable. They usually
return the value of the private data member, alternatively
they can print it also. (e.g. getLength, getWidth )

▪ Since accessors do not change object’s data, they can


be ended with const keyword in the header to force
them not do any change.

10
Class Example in C++

Data Members

Mutators

Member
Functions
Accessors

11
Constructors
▪ A constructor is a special member function which has the same name as the
class name.

▪ Constructors are called when a new object of a class is created and their role to
initialize the new object.

▪ Constructor have no return value, not even void.

▪ Types of Constructors :
1. Default : constructor with no parameters

2. Parameterized : constructors with parameters

3. Copy : constructor with reference to same type object as a parameter

12
Destructors
▪ A destructor is also a special member function which has the same name as the
class name with a prefix ~.

▪ Destructors role is to properly destroy the object. A destructor that does nothing is
automatically added by the compiler.

Constructors

Destructor

13
Lesson Summary

▪ Inter-class Relationships
▪ Dependency & Association
▪ Aggregation & Composition
▪ Inheritance
▪ Class Components
▪ Data Members and Member Functions
▪ Accessors and Mutators
▪ Constructors and Destructors

14

You might also like