0% found this document useful (0 votes)
8 views26 pages

Chap1 Encapsulation

The document provides an overview of encapsulation in object-oriented programming, highlighting its importance in ensuring data integrity, security, and ease of use. It covers key concepts such as data hiding, access control, and the use of constructors, getters, and setters. Additionally, it includes exercises and best practices for implementing encapsulation in programming.

Uploaded by

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

Chap1 Encapsulation

The document provides an overview of encapsulation in object-oriented programming, highlighting its importance in ensuring data integrity, security, and ease of use. It covers key concepts such as data hiding, access control, and the use of constructors, getters, and setters. Additionally, it includes exercises and best practices for implementing encapsulation in programming.

Uploaded by

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

Advanced Programming

CO2039
Chapter 1: Object Oriented Programming
Revision – Encapsulation

Quan Thanh Tho


[email protected]
ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH
TRƯỜNG ĐẠI HỌC BÁCH KHOA
TP.HCM, 03/01/2025
CONTENT
01 INTRODUCTION TO ENCAPSULATION

02 KEY CONCEPTS OF ENCAPSULATION

03 HOW ENCAPSULATION WORKS?

04 BEST PRACTICES

05 CONCLUSION

06 EXTENSION: CLASS DIAGRAM

2
INTRODUCTION TO
01 ENCAPSULATION

3
What is Encapsulation?
When creating new data types (classes) the details of the actual data
and the way operations work is hidden from the other programmers
who will use those new data types.

4
Why is Encapsulation important?
By controlling how data and methods are
accessed or modified, encapsulation ensures:
● Data Integrity: Prevents unauthorized or
accidental changes to data.
● Security: Sensitive data is hidden from
unauthorized access.
● Ease of Use: Users of the class do not need
to understand its internal complexities.
● Code Maintenance: Internal implementation
can change without impacting external
usage.
5
KEY CONCEPTS OF
02 ENCAPSULATION

6
Encapsulation

Before class? struct vs class?

7
Core principles of Encapsulation
Data Hiding: The internal state of an object is kept private and can only
be accessed through public methods (getters and setters).
Access Control: Access specifiers are used to control the visibility of class
members:
● private: members (attributes & methods) cannot be accessed (or
viewed) from outside the class ⇒ Default.
● protected: members cannot be accessed from outside the class, but
can be accessed in inherited classes. Learn more about Chap 2 -
Inheritance.
● public: members are accessible from outside the class.
8
Example
class BankAccount {
private:
double balance; string password;

public:
BankAccount(double initBalance, string pwd) : balance(initBalance), password(pwd) {}

double getBalance(string pwd) { return (pwd == password) ? balance : -1; }

void deposit(double amount) { balance += amount; }

void withdraw(double amount, string pwd) {


if (pwd == password && amount <= balance) balance -= amount;
}
};
9
Homework

Java Python

10
HOW
03 ENCAPSULATION
WORKS?

11
How Encapsulation works?

● Defining Classes and Objects


⇒ Can I use Struct instead of
Class?
● Using Getters and Setters for
Data Access
Default: public private

12
Constructor in Encapsulation

A constructor is a special function in a class:


● Same name as the class.
● Not have a return value.
● It is automatically called when an object is created (the programmer
does not call it directly).
● If no constructor is declared, the program automatically provides a
default empty constructor.
⇒ What is the access modifier for the default constructor?
⇒ Can we create a private constructor? (Singleton Pattern)

13
Constructor characteristics

- Same name as class


- No return type
- Automatically called when
object is declared

14
Constructor

Default constructor Multiple constructors

15
Setters & Getters

WHY we need? Data hiding / Controlled access / Flexibility


HOW they work?
1. Private Data Members: Data members of the class are declared
private to restrict direct access.
2. Public Methods:
○ Getter: Provides controlled access to retrieve the value of a
private data member.
○ Setter: Provides controlled access to modify the value of a
private data member.

16
04 BEST PRACTICES

17
Exercise 1 - Multiple constructor
Create a Car class with the following private attributes:
● brand (string)
● model (string)
● year (int)
Add the following:
● A default constructor that sets default values.
● A parameterized constructor to initialize all attributes.
● Setters and getters for all attributes.
Write a main function to:
● Create an object using the default constructor and display its details.
● Create another object using the parameterized constructor and display its
details.
18
Exercise 2 - Constructor chaining

Create a Laptop class with attributes:


● brand (string)
● model (string)
● price (double)
Add the following:
● A default constructor.
● A parameterized constructor to initialize all attributes.
● Use constructor chaining to call one constructor from another.
Write a main function to test the class.

19
Exercise 3 (Bonus)

Create a Library system using encapsulation and constructors.


1. Create a Book class with attributes: title, author, ISBN, price, and stock.
2. Create a Library class with:
a. An array of Book objects.
b. Methods to:
i. Add new books.
ii. Search for books by title or author.
iii. Borrow books (reduce stock).
3. Write a main function to simulate library operations.

20
05 CONCLUSION

21
Conclusion

1. Encapsulation is the foundation of OOP, focusing on bundling data and


functions within a class.
2. The main difference between a class and a struct lies in the level of data
security (private/public).
3. A constructor is a special mechanism in OOP that automatically executes
when an object is created and can cause errors if not properly designed.

22
EXTENSION:
06 CLASS DIAGRAM

23
Class diagram

Can you write a program based on the above class diagram?

24
Hands-on exercise A class called Ball, which models
a bouncing ball, is designed as
shown in the following class
diagram.
It contains its radius, x and y
position. Each move-step
advances the x and y by delta-x
and delta-y, respectively. delta-x
and delta-y could be positive or
negative.
The reflectHorizontal() and
reflectVertical() methods could
be used to bounce the ball off
the walls. Write the Ball class.
Study the main() on how the
ball bounces.
https://www.programiz.com/onl
ine-compiler/5VrKcTuK0GRr6
25
Thank you for your
attention!
https://www.cse.hcmut.edu.vn

ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH


TRƯỜNG ĐẠI HỌC BÁCH KHOA

You might also like