Imagine you have a school locker.
You can open the locker using your key, put things inside it, or take things out. But you cannot simply reach through the locker wall and change what is inside.
This is similar to encapsulation in C++.
Encapsulation means keeping data protected inside a class and allowing access through functions. Let’s understand the C++ Encapsulation
Why Do We Need Encapsulation?
Let’s first look at a class without encapsulation:
#include <iostream>
using namespace std;
class Student {
public:
int marks;
};
int main() {
Student student;
student.marks = 95;
cout << student.marks;
return 0;
}
Output:
95
This works, but there is a problem.
Anyone using the object can change marks directly:
student.marks = -50;
That doesn’t make sense because marks should normally be between 0 and 100.
We can solve this problem using encapsulation.
Using private
We can make marks private.
class Student {
private:
int marks;
};
Now marks cannot be changed directly from outside the class.
Instead, we can create a function that controls how the marks are changed.
#include <iostream>
using namespace std;
class Student {
private:
int marks;
public:
void setMarks(int m) {
if (m >= 0 && m <= 100) {
marks = m;
}
}
int getMarks() {
return marks;
}
};
int main() {
Student student;
student.setMarks(95);
cout << "Marks: " << student.getMarks();
return 0;
}
Output:
Marks: 95
Now the program has control over the marks.
If someone tries:
student.setMarks(150);
nothing happens because 150 is not a valid mark.
What are C++ Getters and Setters?
You will often see two simple types of functions when working with encapsulation.
Setter
A setter is used to set or change a value.
void setMarks(int m) {
if (m >= 0 && m <= 100) {
marks = m;
}
}
Here, setMarks() changes the value of marks.
Getter
A getter is used to get or read a value.
int getMarks() {
return marks;
}
So:
student.setMarks(95);
sets the marks, while:
student.getMarks();
gets the marks.
You can remember it like this:
Setter → Sets the value
Getter → Gets the value
A Simple Bank Account Example
Let’s take another example.
Suppose we create a bank account:
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance = 0;
public:
void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
double getBalance() {
return balance;
}
};
int main() {
BankAccount account;
account.deposit(5000);
cout << "Balance: " << account.getBalance();
return 0;
}
Output:
Balance: 5000
Here, balance is private.
The user cannot do this:
account.balance = 5000;
Instead, they use:
account.deposit(5000);
The class decides how the balance should be changed.
public and private
Let’s make this very simple.
public
Anything under public can be used from outside the class.
public:
void show();
You can call it using an object:
object.show();
private
Anything under private cannot be accessed directly from outside the class.
private:
int marks;
So this is not allowed:
student.marks = 95;
Instead, we use a public function:
student.setMarks(95);
Think About It Like a Real-Life Example
Imagine a car.
You can use the steering wheel, accelerator, and brakes.
You don’t need to know exactly how the engine works internally.
You
↓
Car controls
↓
Internal parts
The controls give you a simple and safe way to interact with the car.
Encapsulation works in a similar way.
User
↓
Public Functions
↓
Private Data
The private data stays protected, while public functions provide controlled access.
A Simple Example
Let’s create one final example using a student’s age.
#include <iostream>
using namespace std;
class Student {
private:
int age;
public:
void setAge(int a) {
if (a > 0) {
age = a;
}
}
int getAge() {
return age;
}
};
int main() {
Student student;
student.setAge(14);
cout << "Age: " << student.getAge();
return 0;
}
Output:
Age: 14
The important part is that age is protected inside the class, and we use functions to work with it.
Key Points to Remember
- Encapsulation means protecting data inside a class.
privatekeeps data hidden from direct outside access.publicallows controlled access.- A setter changes a value.
- A getter reads a value.
- Encapsulation can help prevent invalid data.
- It keeps the internal details of a class better organized.
What’s Next?
In the next chapter, we will learn about C++ Abstraction and see how we can hide complicated details and show only the important parts to the user.
Frequently Asked Questions (FAQs)
Q1. What is encapsulation in C++?
Encapsulation in C++ means keeping data inside a class and controlling access to it through functions.
Q2. How does data hiding work in C++?
Data hiding is commonly achieved by making class data private so it cannot be accessed directly from outside the class.
Q3. What are C++ getters and setters?
A getter reads a private value, while a setter changes it. They provide controlled access to class data.
Q4. What is the difference between public and private in C++?
public members can be accessed from outside the class, while private members cannot be accessed directly.
Q5. Why are getters and setters used in C++?
They allow a class to control how its private data is read or changed, including validating values before storing them.
Q6. Is encapsulation the same as data hiding in C++?
They are closely related but not exactly the same. Encapsulation groups data and functions together and controls access, while data hiding focuses on preventing direct access to internal data.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
