Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Encapsulation and Access Modifiers

User image

Published by

sanya sanya

Published at: 6th Aug, 2023
2.705 mins read

Introduction

Encapsulation is one of the four fundamental principles of Object-Oriented Programming (OOP). It involves the bundling of data (attributes) and methods (functions) that operate on that data within a single unit, known as a class. The main idea behind encapsulation is to hide the internal implementation details of a class from the outside world, providing a clean and controlled interface to interact with the class.

Encapsulation promotes data security and code maintainability by controlling access to the internal state of an object. It allows you to protect the data from unauthorized access and modification by providing public interfaces (member functions) that enforce specific rules and validations before changing the data.

Example

#include

#include

using namespace std;

class BankAccount {

private:

string accountNumber;

double balance;

public:

// Constructor

BankAccount(const string& accNum, double initialBalance) {

accountNumber = accNum;

balance = initialBalance;

}

// Public member functions (getters and setters)

string getAccountNumber() const {

return accountNumber;

}

double getBalance() const {

return balance;

}

void deposit(double amount) {

if (amount > 0) {

balance += amount;

}

}

void withdraw(double amount) {

if (amount > 0 && amount <= balance) {

balance -= amount;

}

}

};

In this example, the ‘BankAccount’ class encapsulates the account number and balance within private data members. Access to these data members is controlled through public member functions, such as ‘getAccountNumber()’, ‘getBalance()’, ‘deposit()’, and ‘withdraw()’.

Access Modifiers

Three access specifiers that control the visibility and accessibility of class members (data members and member functions) in the context of inheritance:

- ‘public’: Members declared as ‘public’ are accessible from anywhere in the program. They form the public interface of the class and are intended to be accessed by external code.

- ‘protected’: Members declared as ‘protected’ are accessible within the class and its derived classes. They are used to implement restricted access for derived classes.

- ‘private’: Members declared as ‘private’ are only accessible within the class. They are hidden from external code, providing data security and encapsulation.

Getters and setters

Getters and setters are special member functions used to access and modify private data members of a class in a controlled manner. They allow external code to retrieve or modify the values of private data members while enforcing specific rules or validations.

- Getter: A getter is a member function that returns the value of a private data member. It is often named using the prefix "get" followed by the name of the data member it retrieves.

- Setter: A setter is a member function that sets the value of a private data member. It is often named using the prefix "set" followed by the name of the data member it modifies.

In the BankAccount example above, ‘getAccountNumber()’ and ‘getBalance()’ are getters that allow external code to retrieve the account number and balance, respectively. The ‘deposit()’ and ‘withdraw()’ functions are setters that modify the balance based on specific conditions.

Using getters and setters, you can enforce validation rules, such as not allowing negative account balances or accepting only positive deposit amounts, ensuring the integrity of the class's data and promoting encapsulation.

Library

WEB DEVELOPMENT

FAANG QUESTIONS