C++ Academy · Lesson

The this Pointer and Encapsulation

Use the this pointer and enforce encapsulation with private and public access.

Lesson 4 of 413 steps

The this Pointer and Encapsulation is a free C++ Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

What is this?

Inside a non-static member function, this is an implicit pointer to the current object. Use it to disambiguate or to return references.

class Counter {
    int n_ = 0;
public:
    Counter& add(int x) {
        n_ += x;       // implicit this
        return *this;  // chain calls
    }
};

Implicit Access

You usually do not need to write this->. Member names are looked up automatically — this->n_ and n_ are equivalent.

When this-> Is Required

Use this-> when a parameter shadows a member, or when template lookup needs help finding inherited members.

class Person {
    std::string name_;
public:
    void setName(const std::string& name) {
        this->name_ = name;  // or name_ = name;
    }
};

Returning *this for Chaining

Return *this to support fluent interfaces.

class StringBuilder {
    std::string s_;
public:
    StringBuilder& append(const std::string& part) {
        s_ += part;
        return *this;
    }
};

StringBuilder sb;
sb.append("Hello, ").append("World!");

this in Const Methods

Inside a const method, this has type const ClassName*. You cannot modify members through it.

Encapsulation: The Big Idea

Encapsulation means hiding internal state and exposing only safe operations. Make data private; expose intentional public methods.

class Stack {
    std::vector<int> data_;     // hidden
public:
    void push(int v) { data_.push_back(v); }
    int  pop()       { int v = data_.back(); data_.pop_back(); return v; }
    bool empty() const { return data_.empty(); }
};

Why Encapsulate?

Encapsulation lets you:

  • Change the internal representation without breaking callers
  • Enforce invariants in one place
  • Validate inputs at the boundary
  • Reason about object state locally

Public Interface = Contract

The public interface is a promise to callers. Keep it minimal, intentional, and stable. Make implementation details private.

Getters and Setters

Common pattern but easy to overuse. Avoid mirroring every member with get/set — that defeats encapsulation. Expose operations, not fields.

Const Correctness Again

Mark every read-only method const. Callers know which methods are safe to call on const instances and the compiler enforces it.

Friend Functions and Classes

The friend keyword grants another function or class access to private members. Use sparingly — it tightens coupling.

class Account {
    double balance_;
    friend void transfer(Account& a, Account& b, double amount);
};

Quick Check

What is the type of this inside a method declared void f() const of class Foo?

Recap

this points to the current object inside non-static methods. Use it for chaining (return *this) and disambiguation. Encapsulate state with private data and a small, intentional public interface.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “The this Pointer and Encapsulation” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “The this Pointer and Encapsulation”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “The this Pointer and Encapsulation”?

Use the this pointer and enforce encapsulation with private and public access. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “The this Pointer and Encapsulation” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. struct vs class When to Use Each
  2. Member Variables and Methods
  3. Constructors and Member Initializer Lists
  4. The this Pointer and Encapsulation
← Back to C++ Academy