C++ Encapsulation
In this page:
What is Encapsulation?
Encapsulation is the practice of bundling a class's data variables together with the methods that operate on them into one self-contained unit, while hiding those variables from direct modification by any code outside the class.
Example: What is Encapsulation?
#include <iostream>
class Account {
private:
double balance = 0;
public:
void deposit(double amount) { balance += amount; }
double getBalance() { return balance; }
};
int main() {
Account acc;
acc.deposit(100);
std::cout << acc.getBalance() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Getters and Setters
Getters and setters are the standard public interface used to interact safely with an object's private data: getters read a value without exposing the field itself, and setters write a value in a controlled way rather than allowing direct external assignment.
Example: Getters and Setters
#include <iostream>
class Account {
private:
double balance = 0;
public:
double getBalance() { return balance; }
void setBalance(double amount) { balance = amount; }
};
int main() {
Account acc;
acc.setBalance(500);
std::cout << acc.getBalance() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Data Validation
Because setters funnel every write through a single function, they give you a natural place to validate incoming data before it's actually stored, rejecting values that would leave the object in an invalid state and catching bugs closer to their source.
Example: Data Validation
#include <iostream>
class Account {
private:
double balance = 0;
public:
void setBalance(double amount) {
if (amount >= 0) balance = amount;
}
double getBalance() { return balance; }
};
int main() {
Account acc;
acc.setBalance(-50);
std::cout << acc.getBalance() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Abstraction through Encapsulation
Encapsulation also enables abstraction: by hiding complicated internal logic behind a clean public method name, you let other code use a class's functionality without needing to understand — or depend on — how that functionality is actually implemented.
Example: Abstraction through Encapsulation
#include <iostream>
class Account {
private:
double balance = 0;
double calculateInterest() { return balance * 0.05; }
public:
void applyInterest() { balance += calculateInterest(); }
double getBalance() { return balance; }
};
int main() {
Account acc;
acc.applyInterest();
std::cout << acc.getBalance() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Benefits of Encapsulation
The practical payoff of encapsulation is that it keeps code secure, modular, and easy to maintain: because outside code only ever touches the public interface, you're free to rewrite a class's internal implementation later without breaking anything that already uses it.
Example: Benefits of Encapsulation
#include <iostream>
class Account {
private:
double balance = 100;
public:
double getBalance() { return balance; }
};
int main() {
Account acc;
std::cout << acc.getBalance() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: