OnlineCBT Logo OnlineCBT
C++ · LESSON 8

Classes and Objects: Encapsulation & Access Specifiers

By Ratan Updated: 15 Oct 2026

Build object blueprints using class keyword, private, public, and protected access specifiers.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Protect an object's rules with a class

A class combines data with operations. Encapsulation means callers use an interface that preserves valid state rather than directly rewriting every field. Access specifiers control which parts of the interface can be named from different contexts.

A bounded score object

#include 
class Score {
    int value_{0};
public:
    bool set(int candidate) {
        if (candidate < 0 || candidate > 100) return false;
        value_ = candidate;
        return true;
    }
    int value() const { return value_; }
    bool passed() const { return value_ >= 40; }
};
int main() {
    Score score;
    std::cout << score.set(82) << ' ' << score.value() << '\n';
    std::cout << score.set(120) << ' ' << score.value() << '\n';
}

Expected lines: 1 82 and 0 82. Rejecting 120 leaves the previous valid score intact. The private member cannot be assigned directly from main. The const member functions promise not to change the object's ordinary data members through this.

Access levels

  • public: available to callers as the class interface.
  • private: available to members and friends, not ordinary outside code.
  • protected: available under derived-class access rules; it is not public data.

Members of class are private by default; members of struct are public by default. Both can otherwise define constructors, member functions and access labels. Access control is a design/compiler mechanism, not an encryption boundary against someone who can inspect process memory.

Objects have independent state

Creating two Score objects creates two separate values. Calling set on one does not modify the other. A non-static member function has an implicit this pointer identifying its current object. Prefer a clear small interface over dozens of unrelated getters and setters that expose all implementation details.

Design before writing methods

Write the invariant first: “a Score is always between 0 and 100.” Then ensure construction and every mutating method preserve it. Validation should happen before assignment so failure cannot leave half-updated state.

Practice

  1. Add a reset method that sets the value to zero.
  2. Create two scores, set them to 35 and 75, and report passed() for each.
  3. Try score.value_ = 500; and read the access diagnostic.
  4. Design a bounded counter whose increment returns false at its maximum instead of overflowing.

हिन्दी संकेत: private data छिपाना ही काफी नहीं; हर public operation को valid state बनाए रखनी चाहिए।

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
Address Contact

+91 7877547686

E-mail

onlinecbtportal@gmail.com

Helpline Number

+91 7877547686


Click To Download
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat