🟣 OOP  ·  Lesson 42

Access Specifiers: public, private, protected

Access Specifiers: public, private, protected

What is Access Specifiers: public, private, protected?

Access Specifiers: public, private, protected
Access specifiers control visibility of class members. They protect data and support encapsulation.
Level
🟣 Object-Oriented Programming
Example File
access-specifiers.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
publicPublic used in Access Specifiers: public, private, protected programming.
privatePrivate used in Access Specifiers: public, private, protected programming.
protectedProtected used in Access Specifiers: public, private, protected programming.
encapsulationEncapsulation used in Access Specifiers: public, private, protected programming.
visibilityVisibility used in Access Specifiers: public, private, protected programming.

Syntax / Pattern

private members are accessed through public methods.

Example Program

#include <iostream>
using namespace std;
class Student{
private:
    int marks;
public:
    void setMarks(int m){ marks = m; }
    int getMarks(){ return marks; }
};
int main(){ Student s; s.setMarks(91); cout << s.getMarks(); }

Expected Output

91

Program Explanation

  • marks is private, so it cannot be accessed directly outside class.
  • Public methods provide controlled access.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • secure data models
  • validation
  • clean class design

Common Mistakes

  • Making all members public.
  • Trying to access private data directly.

Practice Tasks

  1. Create private balance and public deposit method.
  2. Differentiate public/private/protected.

Summary

Access Specifiers: public, private, protected is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

What is Access Specifiers: public, private, protected?

Access Specifiers: public, private, protected
Access specifiers control visibility of class members. They protect data and support encapsulation.
Level
🟣 Object-Oriented Programming
Example File
access-specifiers.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
publicPublic used in Access Specifiers: public, private, protected programming.
privatePrivate used in Access Specifiers: public, private, protected programming.
protectedProtected used in Access Specifiers: public, private, protected programming.
encapsulationEncapsulation used in Access Specifiers: public, private, protected programming.
visibilityVisibility used in Access Specifiers: public, private, protected programming.

Syntax / Pattern

private members are accessed through public methods.

Example Program

#include <iostream>
using namespace std;
class Student{
private:
    int marks;
public:
    void setMarks(int m){ marks = m; }
    int getMarks(){ return marks; }
};
int main(){ Student s; s.setMarks(91); cout << s.getMarks(); }

Expected Output

91

Program Explanation

  • marks is private, so it cannot be accessed directly outside class.
  • Public methods provide controlled access.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • secure data models
  • validation
  • clean class design

Common Mistakes

  • Making all members public.
  • Trying to access private data directly.

Practice Tasks

  1. Create private balance and public deposit method.
  2. Differentiate public/private/protected.

Summary

Access Specifiers: public, private, protected is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

← Back to C++ Tutorial