What is encapsulation in C++?

Encapsulation in C++ is a core principle of Object-Oriented Programming (OOP) that involves bundling the data (variables) and methods (functions) that operate on the data into a single unit called a class. It also restricts direct access to some of an object’s components, which is a way to hide the internal implementation details and only expose necessary aspects of an object to the outside world.

Encapsulation is implemented in C++ using access specifiers:

  • private: Members declared as private are accessible only within the class itself and are hidden from external access.
  • protected: These members are accessible within the class and its derived classes.
  • public: Public members are accessible from outside the class.

Benefits of Encapsulation:

  1. Data Hiding: Encapsulation ensures that the internal state of an object is hidden from the outside world. Only the relevant methods can be used to access or modify the data.
  2. Modularity: Encapsulation promotes modularity, as each class can be independently developed and tested.
  3. Security: By restricting access to the internal details of an object, encapsulation helps prevent unintended interference with its state.
  4. Maintenance: Since the internal workings of a class are hidden, changes to its implementation do not affect other parts of the code, making it easier to maintain.

Example of Encapsulation in C++:

#include <iostream> using namespace std; class Car { private: int speed; public: // Setter to modify speed void setSpeed(int s) { speed = s; } // Getter to access speed int getSpeed() { return speed; } }; int main() { Car myCar; myCar.setSpeed(100); // Setting speed using setter cout << "Car speed: " << myCar.getSpeed() << endl; // Accessing speed using getter return 0; }

In the example above, the variable speed is encapsulated within the class Car. It can only be accessed or modified via the setSpeed and getSpeed methods, ensuring that the internal state of speed remains protected from unintended changes or invalid values.

Sources:

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Why do you want to work for Pinterest?
How to understand dependency injection for coding interviews?
What Is the Grafana Labs Interview Process Like? (Round by Round)
Grafana Labs runs a recruiter call, a hiring manager interview, practical coding, and a concrete system design round. What each stage evaluates.
Which IT job is most in demand?
What is highest job at Apple?
What is the next big thing in the tech industry?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.