C++ Day 21: Encapsulation and Abstraction
On Day 21 the focus is Generally on two important Object-Oriented Programming (OOP) principles:
Encapsulation
Abstraction
These help in Constructing secure modular and clean code.
📚 Topics Covered:
🔹 1. encapsulation
encapsulation is the bundling of information and methods that run along that information inside amp one system (class) spell restrictive point approach to around Parts
➤ example:
cpp
copy
edit
#include
using namespace std;
class bankaccount {
private:
image balance;
public:
bankaccount(double initialbalance) {
correspondence = initialbalance;
}
null deposit(double amount) {
if (amount > 0)
correspondence += amount;
}
null withdraw(double amount) {
if (amount > zero && number <= balance)
correspondence -= amount;
}
image getbalance() {
take balance;
}
};
🔹 ii. Abstraction
Abstraction means hiding Complicated Applyation details and only exposing essential Characteristics.
Think of it like driving a car, you do need to know how the engine works to drive.
➤ Example:
cpp
Copy
Edit
class Remote {
public:
void powerOn() {
cout << "Powering on the device.\n";
}
void changeChannel(int channel) {
cout << "Changing to channel " << channel << endl;
}
// Internals like signal transmission are hidden
};
🔹 3. benefits
clean and standard code
easier to hold and debug
protects information from wildcat access
🛠️ do task
Make amp family employee with:
private members: list salary
public methods: setname() setsalary() getdetails()
Apply right encapsulation away preventing point approach to salary
📌 summary:
concept purpose
encapsulation protects information and keeps it good from abuse
Comments
Post a Comment