Encapsulation in C++

Posted on December 15, 2023 by Vishesh Namdev
Python C C++ Java
C++ Programming

Encapsulation is one of the fundamental principles of Object-Oriented Programming (OOP), and it refers to the bundling of data and methods that operate on that data within a single unit, known as a class. In C++, encapsulation is primarily achieved through the use of access specifiers and the organization of class members.

Access Specifiers:

1. Public (public)

Members declared as public are accessible from anywhere in the program.
Public members form the interface of the class, defining how external code can interact with the class.
// Example class
class MyClass {
public:
int publicVar;
void publicMethod() {
// Code here
}
}

2. Private (private):

Members declared as private are accessible only within the class itself.
Private members are hidden from external code, providing a level of data protection.
// Example class
class MyClass {
private:
int privateVar;
void privateMethod() {
// Code here
}

public:
void publicMethod() {
// Code here can access privateVar and privateMethod
}
};

3. Protected (protected):

Similar to private, but with a difference in inheritance. Members declared as protected are accessible within the class and its derived classes.
// Base class definition
class MyBaseClass {
protected:
  int protectedVar;
};

// Derived class definition
class MyDerivedClass : public MyBaseClass {
public:
  void accessProtected() {
    protectedVar = 42; // Accessing protectedVar from the base class
  }
};

Implementing Encapsulation:

1. Private Data Members:

Declare data members as private to encapsulate them within the class.
Access them only through public methods (getter and setter methods) to control and validate access.
class BankAccount {
private:
double balance;

public:
// Getter method
double getBalance() const {
return balance;
}

// Setter method
void setBalance(double amount) {
// Additional validation logic can be added here
balance = amount;
}
};

2. Getter and Setter Methods:

Getter methods allow external code to access the values of private members.
Setter methods provide a controlled way to modify the values of private members, often with additional validation.
class Student {
private:
string name;
int age;

public:
// Getter methods
string getName() const {
return name;
}

int getAge() const {
return age;
}

// Setter methods
void setName(const string& newName) {
name = newName;
}

void setAge(int newAge) {
// Validation logic can be added here
age = newAge;
}
};

Benefits of Encapsulation:

(a). Data Hiding: Encapsulation hides the internal details of a class from external code, preventing direct access to the implementation details.

(b). Modularity: The encapsulated code is modular, allowing changes to one part of the code without affecting other parts. This enhances maintainability.

(c). Security: By keeping data members private and controlling access through methods, encapsulation enhances security by preventing unintended interference.

Example:

#include <iostream>Copy Code
#include <string>

class Person {
private:
std::string name;
int age;

public:
// Getter methods
std::string getName() const {
return name;
}

int getAge() const {
return age;
}

// Setter methods
void setName(const std::string& newName) {
name = newName;
}

void setAge(int newAge) {
if (newAge > 0) {
age = newAge;
}
// Additional validation logic can be added here
}
};

int main() {
Person person;
person.setName("John Doe");
person.setAge(25);

std::cout << "Name: " << person.getName() << std::endl;
std::cout << "Age: " << person.getAge() << std::endl;

return 0;
}

In this example, the Person class encapsulates the name and age members. The external code interacts with these members through getter and setter methods, ensuring controlled access and encapsulation. Encapsulation, along with other OOP principles, contributes to building robust, modular, and maintainable software systems in C++.