Encapsulation in C++
Basedon 'Data Structures and Algorithms in C++' by
Adam Drozdek (3rd Edition)
Presented by: GROUP 3
2.
• Encapsulation isa fundamental concept in Object-
Oriented Programming (OOP), where data and methods
are bundled together, and access to the data is restricted
from the outside world.
• This presentation will help you understand the purpose,
advantages, and implementation of encapsulation in
C++ using real-world examples.
Overview:
3.
• Deep diveinto encapsulation and its role in C++.
• Explore real-world use cases of encapsulation in
software design.
• Interactive class activities to help you implement
encapsulation and understand its impact on
system security and maintainability.
Session Goals:
4.
• Encapsulation refersto the practice of hiding an
object's internal state and requiring all interaction
to be done through well-defined public methods.
• The object's state is protected and cannot be
directly accessed from outside the class.
Introduction to Encapsulation
6.
Private Data- only accessible from within the same
class.
Public Methods - accessible from anywhere in the
program, including outside the class. These are
typically used for interfaces (e.g., getter and setter
methods) that allow controlled interaction with private
data.
Protected - are accessible within the class itself and
by derived classes (in the context of inheritance).
Access Specifiers:
8.
Protects datafrom unintended access and
modification.
Promotes code modularity and maintainability.
Allows changes in implementation without
affecting other code.
Why Encapsulation?
9.
ATM Machine:
• PrivateData: The balance in a bank account.
• Public Methods: Deposit, withdraw, check
balance. The ATM machine hides the
implementation (e.g., calculations, validations).
Real-World Analogy
10.
Syntax:
class Account {
private:
doublebalance; // Private variable
public:
// Public Method to Set Balance
void setBalance(double value) {
if(value > 0)
balance = value;
}
// Public Method to Get Balance
double getBalance() {
return balance;
}
};
Anatomy of a Class in C++
11.
class Student {
private:
intgrade;
public:
void setGrade(int g) { grade = g; }
int getGrade() { return grade; }
};
grade is hidden from direct access.
Controlled access through setGrade() and getGrade().
Encapsulation Example in C++
12.
Create a classnamed Person.
The class should have one private variable: name.
Create two public functions:
setName() — to set the name.
getName() — to get and print the name.
• In the main() function, create a Person object, set
a name, then display it.
Instruction ->
13.
#include <iostream>
using namespacestd;
class Person {
private:
string name; // private variable
public:
void setName(string n) {
name = n;
}
string getName() {
return name;
}
};
Example.
14.
Try changing thename to your own name.
Add another private variable like age.
Add a setAge() and getAge() function.
• Display both name and age in main().
Now.
Output:
The person’s name is: Sir Arjay
The person’s age is: idk years
old
15.
#include <iostream>
using namespacestd;
class Secret {
private:
int number;
public:
void setNumber(int n) {
number = n;
}
void revealNumber() {
cout << “The secret number is: “ << number << endl;
}
};
MORE.
16.
Change the numberto your favorite number.
Try calling s.number = 100; in main() — What
happens? Why?
(Hint: You can’t access private data directly — this is
encapsulation!)
• Add another method doubleNumber() that prints
the number × 2.
Try this
Output must be 2x
17.
#include <iostream>
using namespacestd;
class Bank {
private:
int money;
public:
void setMoney(int m) {
money = m;
cout << “Money set!” << endl;
}
void showMoney() {
cout << “Money in account: “ << money << endl;
}
More?
18.
Sample Output:
Money set! Money in
account: 100 Adding
50 pesos...
Money in account: 150
Hehe
Your task.
Add a function called
addMoney(int amount)
that adds more money to
the account.