Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 OOP  ·  Lesson 42

Access Specifiers in C++

In this lesson: C++ access specifiers explained with a visibility table and inheritance examples with output. In Hindi & English.

What are access specifiers?

Access specifiers decide who is allowed to use each member of a class. There are three: public, private and protected. They are how C++ enforces encapsulation — showing a safe interface while hiding the internals.

public

public members can be used from anywhere — inside the class, and by outside code holding an object:

C++
class Dog {
public:
    string name;
    void bark() { cout << name << " says Woof!\n"; }
};
int main() {
    Dog d;
    d.name = "Rex";     // OK: public
    d.bark();           // OK: public
}
Output:
Rex says Woof!

private

private members can be used only inside the class. Outside code cannot touch them directly:

C++
class Account {
private:
    double balance = 0;      // hidden
public:
    void deposit(double a) { balance += a; }
    double getBalance() { return balance; }
};
int main() {
    Account acc;
    acc.deposit(500);
    // acc.balance = 999;    // ERROR: private
    cout << acc.getBalance();  // 500
}
Output:
500

protected

protected is like private, but derived classes can still reach the member. It matters only with inheritance:

C++
class Animal {
protected:
    int legs = 4;      // hidden from outside, seen by subclasses
};
class Dog : public Animal {
public:
    void show() { cout << "Legs: " << legs; }  // OK
};

Getters and setters

The usual pattern is private data with public functions to read and change it safely — often called getters and setters:

C++
class Temperature {
    double celsius = 0;        // private
public:
    void setC(double c) { if (c >= -273.15) celsius = c; }  // validate
    double getC() { return celsius; }
};

The setter can reject invalid values — impossible if the data were public.

Default access

classstruct
Default accessprivatepublic

That default is the only real difference between class and struct.

Common mistakes

  • Forgetting that class members are private by default, then wondering why access fails.
  • Accessing a private member from outside the class.
  • Using protected when private would do — expose to subclasses only when needed.
  • Making all data public, throwing away encapsulation.
🏋️ Practice

Write a BankAccount class with a private balance, a deposit that rejects negative amounts, a withdraw that refuses to overdraw, and a getter. Try to change the balance directly and see the compiler stop you.

Summary

  • Access specifiers control who can use each member.
  • public: everywhere; private: same class only; protected: class + derived.
  • Class members are private by default; struct members public.
  • Private data with public getters/setters is the encapsulation pattern.
  • Setters can validate input, keeping the object consistent.

Frequently Asked Questions

What are access specifiers in C++?
Access specifiers are keywords — public, private and protected — that control which parts of a program can use a class member. They are the mechanism behind encapsulation, letting a class expose some things while keeping others hidden and protected.
What is the difference between public, private and protected in C++?
public members are accessible from anywhere, private members only from within the same class, and protected members from the class and any classes derived from it. Private is the most restrictive; protected sits between private and public for inheritance.
What is the default access specifier in a C++ class?
In a class, members are private by default, so anything before the first specifier cannot be accessed from outside. In a struct, members are public by default. This default difference is the only real distinction between class and struct.
Why should data members be private in C++?
Making data members private hides the internal state and forces outside code to go through public functions, which can validate input and keep the object consistent. This encapsulation protects the data and lets you change the internals later without breaking code that uses the class.
What is the difference between private and protected in C++?
Both hide members from outside code, but a protected member is still accessible inside derived classes, while a private member is not accessible even to derived classes. You use protected when a base class wants subclasses to reach a member directly.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicStatic Data Members and Static Functions Your progress: 42 / 81 (52%) · Lesson 43 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free