Encapsulation

What Are the Three Types of Encapsulation?

Encapsulation isn't usually split into three types. In C++, the three access levels are public, private and protected, and here is how each one works.

On this page
  1. Short answer
  2. Private
  3. Protected
  4. Public
  5. An example with all three
  6. Default access in a class and a struct
  7. Why these are not three separate types

Short answer

Encapsulation is not usually divided into three named types. When people ask this question, they most often mean the three C++ access levels: public, private and protected. These keywords decide who can use a class’s members, and they are how encapsulation is put into practice.

If a course or quiz asks for three types, check your course notes first. The answer depends on how your teacher defined the term.

Private

A private member can be used only by the class’s own member functions. Code outside the class cannot read or change it. Data is usually private, because that keeps the class in control of its own state.

Protected

A protected member can be used by the class itself and by classes derived from it. Code outside both cannot use it. Protected access matters once you use inheritance in c.

Public

A public member can be used by any code that can see the class. Public member functions form the class’s interface, which is the part other code is meant to use.

An example with all three

In this example, Car inherits from Vehicle. Car can use wheels_, which is protected, but not speed_, which is private.

#include <iostream>

class Vehicle {
public:
    Vehicle(int wheels) : wheels_(wheels), speed_(0) {}

    int speed() const { return speed_; }

    void accelerate(int amount) {
        if (amount > 0) {
            speed_ += amount;
        }
    }

protected:
    int wheels_;

private:
    int speed_;
};

class Car : public Vehicle {
public:
    Car() : Vehicle(4) {}

    int totalWheels() const { return wheels_; }
};

int main() {
    Car car;
    car.accelerate(20);

    std::cout << "Speed: " << car.speed() << "\n";
    std::cout << "Wheels: " << car.totalWheels() << "\n";

    // car.speed_ = 100;   // error: speed_ is private
    // car.wheels_ = 6;    // error: wheels_ is protected
    return 0;
}

The program prints:

Speed: 20
Wheels: 4

speed() and accelerate() are public, so main can call them. wheels_ is protected, so only Vehicle and its derived class Car can use it. speed_ is private, so only Vehicle can use it. The two commented-out lines would not compile.

Default access in a class and a struct

Members of a class are private by default, and members of a struct are public by default. Readers often ask, is struct private or public by default? In C++, the answer is public.

Why these are not three separate types

All three keywords are access levels. They are tools for encapsulation, not three separate kinds of it. The idea stays the same: keep the data inside the class, and expose only what other code needs. The access levels are how you do that.

The complete guide to encapsulation

Encapsulation in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Unary Operators in C++

Learn what unary operators in C++ are, from the one-operand symbols like !, ~, ++ and * to prefix vs postfix, precedence and common mistakes, with examples.