Ugenz Academy/Tutorial

Encapsulation

5 min read917 words

Reading progress

Keep learning

Encapsulation in C++

Introduction

Encapsulation is one of the fundamental concepts of Object-Oriented Programming (OOP).

Encapsulation means wrapping data and functions together inside a single class and controlling direct access to the data.

It helps protect the internal data of an object from unauthorized or accidental modification.

Definition

Encapsulation is the process of binding data members and member functions together inside a class and controlling access to them.

For example:

Class ├── Data └── Functions

Both data and functions are combined inside the class.

1. Simple Example

#include <iostream> using namespace std; class Student { private: string name; int age; public: void setName(string n) { name = n; } void setAge(int a) { age = a; } void display() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; int main() { Student s1; s1.setName("Anuradha"); s1.setAge(22); s1.display(); return 0; }

Output

Name: Anuradha Age: 22

Here, name and age are private, so they cannot be accessed directly from outside the class.

2. Access Specifiers

C++ provides three main access specifiers:

  1. private
  2. public
  3. protected

Private

Private members can be accessed only from within the class.

class Student { private: int age; };

The following is not allowed:

Student s1; s1.age = 22; // Error

Public

Public members can be accessed from outside the class.

class Student { public: void display() { cout << "Hello"; } };

Example:

Student s1; s1.display();

Protected

Protected members can be accessed inside the class and by derived classes.

class Parent { protected: int value; }; class Child : public Parent { public: void show() { value = 10; cout << value; } };

3. Getters and Setters

A common way to implement encapsulation is by using getter and setter functions.

  • Setter → Used to set or modify data.
  • Getter → Used to read or retrieve data.

Example

#include <iostream> using namespace std; class Account { private: double balance; public: void setBalance(double b) { if (b >= 0) { balance = b; } } double getBalance() { return balance; } }; int main() { Account account; account.setBalance(5000); cout << "Balance: " << account.getBalance(); return 0; }

Output

Balance: 5000

The user cannot directly modify balance.

4. Data Validation Using Encapsulation

Encapsulation allows us to validate data before storing it.

Example

#include <iostream> using namespace std; class Student { private: int age; public: void setAge(int a) { if (a > 0 && a <= 100) { age = a; } else { cout << "Invalid age" << endl; } } int getAge() { return age; } }; int main() { Student s1; s1.setAge(22); cout << "Age: " << s1.getAge(); return 0; }

If someone tries to enter an invalid age, the setter can reject it.

5. Real-Life Example

Consider a Bank Account.

A bank account contains sensitive information such as:

Account ├── Account Number ├── Balance └── PIN

These details should not be directly accessible.

Instead, users interact through functions such as:

deposit() withdraw() getBalance()

For example:

class BankAccount { private: double balance; public: void deposit(double amount) { if (amount > 0) { balance += amount; } } double getBalance() { return balance; } };

The balance is protected from direct access.

6. Advantages of Encapsulation

1. Data Security

Encapsulation prevents direct access to sensitive data.

2. Data Validation

Input can be checked before it is stored.

3. Controlled Access

Users can access data only through defined methods.

4. Easy Maintenance

Internal implementation can be changed without changing how users interact with the class.

5. Reduces Complexity

It hides unnecessary implementation details from the user.

6. Code Organization

Related data and functions are grouped together inside a class.

7. Encapsulation vs Data Hiding

Although closely related, they are not exactly the same.

Encapsulation

Encapsulation means combining data and functions into a single unit, such as a class.

Data Hiding

Data hiding means restricting direct access to internal data.

For example:

class Student { private: int age; // Data hiding public: void setAge(int a) { age = a; } };

The class provides encapsulation, while the private member provides data hiding.

8. Encapsulation Example with Constructor

#include <iostream> using namespace std; class Student { private: string name; int age; public: Student(string n, int a) { name = n; age = a; } string getName() { return name; } int getAge() { return age; } }; int main() { Student s1("Anuradha", 22); cout << "Name: " << s1.getName() << endl; cout << "Age: " << s1.getAge() << endl; return 0; }

Output

Name: Anuradha Age: 22

9. Key Points

  • Encapsulation is an important OOP concept.
  • It binds data and functions together inside a class.
  • Private data cannot be accessed directly from outside the class.
  • Getters and setters provide controlled access.
  • Encapsulation improves data security.
  • It allows data validation.
  • It makes programs easier to maintain.
  • It supports data hiding.

Summary

Encapsulation | ┌───────────┴───────────┐ ↓ ↓ Data Functions │ │ └───────────┬───────────┘ ↓ Class ↓ Controlled Access

Simple Definition

Encapsulation = Binding data and functions together + controlling access to data.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy