Encapsulation in C++
Based on 'Data Structures and Algorithms in C++' by
Adam Drozdek (3rd Edition)
Presented by: GROUP 3
• Encapsulation is a 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:
• Deep dive into 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:
• Encapsulation refers to 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
 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:
 Protects data from unintended access and
modification.
 Promotes code modularity and maintainability.
 Allows changes in implementation without
affecting other code.
Why Encapsulation?
ATM Machine:
• Private Data: 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
Syntax:
class Account {
private:
double balance; // 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++
class Student {
private:
int grade;
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++
Create a class named 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 ->
#include <iostream>
using namespace std;
class Person {
private:
string name; // private variable
public:
void setName(string n) {
name = n;
}
string getName() {
return name;
}
};
Example.
Try changing the name 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
#include <iostream>
using namespace std;
class Secret {
private:
int number;
public:
void setNumber(int n) {
number = n;
}
void revealNumber() {
cout << “The secret number is: “ << number << endl;
}
};
MORE.
Change the number to 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
#include <iostream>
using namespace std;
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?
 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.
 Hides complexity
 Enhances security
 Improves code reusability
 Facilitates easier maintenance
Benefits of Encapsulation
 Encapsulation is vital in OOP.
 It enables safer and more organized code.
 C++ provides robust support through class
definitions.
Summary
 Drozdek, A. (2005). Data Structures and
Algorithms in C++ (3rd ed.).
 Thomson Course Technology.
Reference
 Thank you for listening!
 Any questions or clarifications?
Questions?

g3-encapsulation.pptxyyyyyyy66te5ujuuuyyt6

  • 1.
    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.
  • 19.
     Hides complexity Enhances security  Improves code reusability  Facilitates easier maintenance Benefits of Encapsulation
  • 20.
     Encapsulation isvital in OOP.  It enables safer and more organized code.  C++ provides robust support through class definitions. Summary
  • 21.
     Drozdek, A.(2005). Data Structures and Algorithms in C++ (3rd ed.).  Thomson Course Technology. Reference
  • 22.
     Thank youfor listening!  Any questions or clarifications? Questions?