0% found this document useful (0 votes)
16 views21 pages

Encapsulation and Data Hiding in C++

The document covers the concepts of encapsulation and data-hiding in object-oriented programming, emphasizing the importance of UML class diagrams for designing classes. It explains access specifiers (public, private, protected) and their roles in controlling access to class members, as well as the use of accessor and mutator methods for managing internal data. The document concludes with a class activity to apply these concepts in a practical scenario.

Uploaded by

Jesse
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)
16 views21 pages

Encapsulation and Data Hiding in C++

The document covers the concepts of encapsulation and data-hiding in object-oriented programming, emphasizing the importance of UML class diagrams for designing classes. It explains access specifiers (public, private, protected) and their roles in controlling access to class members, as well as the use of accessor and mutator methods for managing internal data. The document concludes with a class activity to apply these concepts in a practical scenario.

Uploaded by

Jesse
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/ 21

1

DR. (MRS) T-S.M.A. ADJAIDOO


z
06
Encapsulation &
COE 254: Object-Oriented Prgramming

Data-Hiding
2

z
Today’s Lesson

UML Class What is Accessors and C++


Data-Hiding
Diagrams Encapsulation? Mutators Implementation
COE 254: Object-Oriented Prgramming
3

z
Learning Outcomes

• Be able to design simple class


1 diagrams correctly

• Appreciate the need for data


2 encapsulation and hiding
COE 254: Object-Oriented Prgramming

• Learn to use access specifiers and


3 restrict access to data in python
4

z
UML Class Diagrams

UNIFIED MODELING LANGUAGE (UML)


▪ UML is currently the standard notation for documenting object-oriented
systems

▪ It provides a pictorial or graphical notation for documenting the artefacts


such as classes, objects and packages that make up an object-oriented
system
COE 254: Object-Oriented Prgramming
5

z
UML Class Diagrams

UNIFIED MODELING LANGUAGE (UML)


UML diagrams can be divided into three categories.

▪ Structure diagrams: show the static architecture of the system irrespective


of time.
▪ For example, structure diagrams for a university system may include
diagrams that depict the design of classes such as Student, Faculty, etc.

▪ Behaviour diagrams: depict the behaviour of a system or business process.


COE 254: Object-Oriented Prgramming

▪ Interaction diagrams: show the methods, interactions and activities of the


objects.
▪ For a university system, a possible behaviour diagram would show how
a student registers for a course.
6

z
UML Class Diagrams

▪ Class diagrams are structured diagrams Student


which show the classes, their methods - name : String
and fields - gender : String
- dateofBirth :String
▪ We will use UML class diagrams to - studentNo : int
- cwa : double
illustrate the classes that we build in this
course so let’s take a look at how we
draw class diagrams + Student ( name : String, gender : String, dateofBirth : String )
+ setName ( name : String ) : void
▪ As we delve more deeply into OO + getName ( ) : String
+ setCWA ( cwa : double ) : void
COE 254: Object-Oriented Prgramming

design, these class diagrams will get


+ assignStudentNo ( ) : void
much more sophisticated and convey + showStudent ( ) : String
much more information on how the
different classes interact with each other.
Example of a class diagram
7

z
UML Class Diagrams

▪ Each class is represented by a box, which is


divided into three rectangles. Student
- name : String
▪ The name of the class is given in the top rectangle. - gender : String
- dateofBirth :String
- studentNo : int
▪ The attributes are shown with their names and - cwa : double
their types in the second box.

▪ The third box shows the methods with their return + Student ( name : String, gender : String, dateofBirth : String )
+ setName ( name : String ) : void
types and parameters (names and types) + getName ( ) : String
+ setCWA ( cwa : double ) : void
▪ The access specifier for each field and method is + assignStudentNo ( ) : void
COE 254: Object-Oriented Prgramming

+ showStudent ( ) : String
given just in front of the field name or method name.
▪ − (minus) sign indicates private access

▪ + (plus) stands for public access and

▪ # (hash) is used for protected access


8

z
What is Encapsulation?

▪ One of the primary advantages of using objects is that the object need not
reveal all its attributes and behaviours.

▪ Encapsulation is defined by the fact that objects contain both the attributes
and behaviours. A class itself is an example of encapsulation as it captures
attributes and behaviours in objects

▪ Data hiding is a major part of encapsulation


COE 254: Object-Oriented Prgramming
9

z
Data-Hiding

▪ Data-hiding involves hiding data or information from users

▪ Object details (data members and implementations) that are not relevant to
the use of an object are hidden from other objects

▪ This makes the program more robust and secures

▪ For instance, in our school management program, a student object has a


method which generates a studentNo. A course object requesting for the
COE 254: Object-Oriented Prgramming

studentNo does not need to have access to how that number is generated.
So that aspect is hidden from the course object

▪ To achieve data-hiding we use access specifiers


10

z
Data-Hiding

Access Specifiers
▪ Most O-O programming languages control access to class members using
access modifiers

▪ There are fundamentally three types of access specifiers used:


▪ Public Access Specifier
COE 254: Object-Oriented Prgramming

▪ Private Access Specifier

▪ Protected Access Specifier


11

z
Data-Hiding

Public Access Specifier


▪ Class members which are specified as public are accessible to all
members of their class and also to objects outside their class

▪ They can be accessed easily for any part of the program

▪ By default all class members are private unless specified otherwise


COE 254: Object-Oriented Prgramming

▪ This is the least secure specifier

▪ In C++ public members are written under the label, public:


12

z
Data-Hiding

Private Access Specifier


▪ Class members which are declared as private are accessible to only
members of their class

▪ They cannot be accessed from without the class

▪ This is the most secure type of access specifier


COE 254: Object-Oriented Prgramming

▪ In C++ private members are written under the label, private:


13

z
Data-Hiding

Protected Access Specifier


▪ Class members which are declared as protected are accessible to only
members of their derived classes (We will learn about derived classes in
the next chapter)

▪ In C++, protected members are written under the label, protected:


COE 254: Object-Oriented Prgramming
14

z
Accessors and Mutators

▪ Recall the getX() and setX() methods we mentioned in the previous chapter

▪ We have just discussed the need to keep internal data of objects private

▪ This means there must be some methods in the class that allow for
controlled access to object data
COE 254: Object-Oriented Prgramming

▪ These methods are referred to as Accessors and Mutators


15

z
Accessors and Mutators

Accessor Methods
▪ These grant access to internally hidden data in an object for use.

▪ This method cannot be used to change the data values

▪ They are commonly called getters

▪ The methods are prefixed with the word get


COE 254: Object-Oriented Prgramming

▪ Eg: getName( ), getId( )


16

z
Accessors and Mutators

Mutator Methods
▪ These grant access to change or modify the values of internally hidden
data in an object.

▪ This method cannot be used to retrieve the data values

▪ They are commonly called setters


COE 254: Object-Oriented Prgramming

▪ The methods are prefixed with the word set

▪ Eg: setName( nameValue ), getId( idValue )


17

C++
Implementation
Let’s look at encapsulation
in this example.

Pay attention to the access


specifiers
COE 254: Object-Oriented Prgramming
18

C++
Implementation
The derived class
accesses the protected
method
COE 254: Object-Oriented Prgramming
C++ Implementation

Our Output
z
19
COE 254: Object-Oriented Prgramming
20

z
Class Activity

▪ Draw class diagrams for the following program:


▪ An application that lets a user send and receive messages from other users of
the same application

▪ What objects would you need?

▪ What attributes and methods would be required by the objects to


COE 254: Object-Oriented Prgramming

accomplish the task?


21

Any Questions?
z
The End
Contact: [email protected]

Office: Caesar Building, Room 413


COE 254: Object-Oriented Prgramming

You might also like