Lesson: C++ - Data Abstraction
JM’S PART
I. What is Data Abstraction?
Data Abstraction in C++ is a key concept of Object-Oriented Programming (OOP). It
refers to hiding the internal implementation details and showing only the necessary
information to the user.
It's like using a TV remote – you know what buttons to press (interface), but you
don’t know how it works inside (implementation).
In C++, abstraction is achieved mainly using:
Classes
Access Specifiers (public, private, protected)
II. Real-Life Analogy
Think of a Car:
When you drive a car, you use the steering wheel, accelerator, and brake.
You don’t need to know how the engine, transmission, or braking system work.
The car manufacturer hides the complexity and provides a simple interface to
drive the car.
This is abstraction: hiding complexity and exposing only the essentials.
III. Why is Data Abstraction Important?
Reduces complexity for the user
Improves code reusability
Enhances security (data is not directly accessible)
Makes code more maintainable and scalable
Helps in managing large codebases
[HAZEL’S PART]
IV. Access Specifiers and Abstraction
In C++, access specifiers control how members (variables and functions) of a class can
be accessed:
Specifier Description
Members are hidden from outside the class. Used for internal
private
implementation.
public Members are accessible from outside. Used as interface to the class.
protected Like private but accessible in derived classes.
(TAKE NOTE: ) The idea is to hide internal data (private) and expose only what is
necessary (public).
[LANIBA’S PART]
V. Syntax of Data Abstraction using Classes
VI. Explanation of the Example
In the above program:
accountHolder and balance are private → they cannot be accessed directly.
Functions like deposit(), withdraw(), and displayBalance() are public → they act
as the interface for the user.
The internal implementation of how the balance is managed is abstracted away
from the user.
[EDUARD’S PART]
VII. Advantages of Using Abstraction in This Code
Encapsulation of data – no direct access to balance
User-friendly interface – just use deposit/withdraw functions
Flexible for future changes – logic can change without affecting interface
Security – prevents misuse of critical data
[JONVERT’S PART]
VIII. Comparison: Without Abstraction
If we allowed direct access like this:
account.balance = -1000;
It would break the logic and allow invalid data.
Using abstraction prevents this.
[LUMNA’S PART]
IX. Summary
Concept Explanation
Data Hiding the internal implementation and showing only necessary
Abstraction features
Concept Explanation
Achieved by Using classes and access specifiers (private, public)
Purpose Reduce complexity, improve security, and promote reusability
C++ Tools Classes, Objects, Access Specifiers
X. Challenge for Practice
Create a Student class with:
• Private data: name, grade, studentID
• Public functions: setDetails(), getDetails(), updateGrade()
ANSWER:
EXPLANATION:
name, studentID, and grade are private: hidden
setDetails(), getDetails(), and updateGrade() are public: user interface
Invalid grades are prevented from being assigned: secure & validated
data