← Back to C++ Course | Chapter 10: Polymorphism & Abstraction | Lesson 7 of 11

C++ Encapsulation

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?

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.