Encapsulation

What Is Encapsulation? Definition and Example

Encapsulation bundles data with the functions that use it and hides the details. See a simple C++ Rectangle example that shows it in action.

On this page
  1. Definition
  2. Why hide the data?
  3. Example: a Rectangle class
  4. Encapsulation and abstraction
  5. Mistakes to avoid

Definition

Encapsulation is the idea of bundling data together with the functions that work on it, and hiding the details from code outside. In C++, a class is the usual tool. Its data members are private, and its public member functions are the only way for other code to read or change them.

Why hide the data?

Hidden data can change only in the ways the class allows. Checks, rules and updates happen in one place, so the data stays in a known state. When something goes wrong, there are fewer places to look.

Example: a Rectangle class

This class keeps its width and height private. The setters accept only positive values, and the area is calculated from the two stored values.

#include <iostream>

class Rectangle {
public:
    void setWidth(double width) {
        if (width > 0) {
            width_ = width;
        }
    }

    void setHeight(double height) {
        if (height > 0) {
            height_ = height;
        }
    }

    double area() const {
        return width_ * height_;
    }

private:
    double width_ = 1.0;
    double height_ = 1.0;
};

int main() {
    Rectangle rect;
    rect.setWidth(4.0);
    rect.setHeight(2.5);
    rect.setWidth(-3.0);  // rejected, so width stays 4.0

    std::cout << "Area: " << rect.area() << "\n";

    // rect.width_ = -3.0;  // error: width_ is private
    return 0;
}

The program prints:

Area: 10

Because width_ and height_ are private, outside code cannot store a negative size in them. The setter with the negative value simply does nothing. The area is always worked out from the current width and height, so it cannot fall out of step with them.

Encapsulation and abstraction

People often mix these two ideas up. Encapsulation keeps data together with the code that changes it, and hides the internal details. Abstraction decides what a class offers, so that users see only the features they need. A class often uses both. Abstraction shapes its interface, and encapsulation protects how its data is stored and changed.

Mistakes to avoid

  • Making data members public, which removes the protection.
  • Writing a setter for every field without any checks.
  • Returning a reference to a private member, which lets callers change it directly.

Access levels are the keywords that control this, and they are explained in access modifiers in c. For a fuller guide to encapsulation in c, including a bank account example and a checklist, see the overview.

The complete guide to encapsulation

Encapsulation in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Class Constructors in C++

Learn C++ class constructors with compilable examples: default and parameterized forms, member initializer lists, explicit, and initialization order.

Constructors 6 min read

SOLID Principles in C++

Learn the five SOLID principles in C++ with short examples of single responsibility, open/closed, Liskov, interface segregation and dependency inversion.