Page 80 - Modular_V2.0_C++_Flikpbook
P. 80
Program 2: To demonstrate data abstraction using a Bank Account class.
#include<iostream.h>
#include<conio.h>
class BankAccount
{
private:
float balance; // Hidden data
public:
float amount;
float newBalance;
BankAccount()
{ DOSBox 0.74, Cpu speed: max 100%
balance = 5000; Current Balance: $6500
amount = 1500;
newBalance = balance + amount; // Abstracting balance update
}
};
void main()
{
clrscr();
BankAccount account;
cout<< "Current Balance: $" << account.newBalance;
getch();
}
The balance variable is private, preventing direct access and ensuring data security. The balance
update calculation is hidden within the class, and only newBalance is exposed to the user. This
demonstrates abstraction by simplifying access while keeping internal details hidden.
Encapsulation
Encapsulation refers to a process of binding data and function together into a single unit , similar
to a capsule. It is used to restrict access to private data members from outside the class, ensuring
data security and integrity. To implement encapsulation, all data members of a class should be
declared private, while functions that access or modify these data members should be declared
public. The private keyword is used to make a data member accessible only within the class in
which it is declared. Let's create a program to understand the concept better.
Program 3: To demonstrate encapsulation using a Student class
#include<iostream.h>
#include<conio.h>
class Student
78
Touchpad MODULAR (Ver. 2.0)

