Skip to content
  • Access Modifiers · OOPs

Access Modifiers

LearnViaNotes @learnvianotes· 1 min read

Control who can see and use which data members and methods. The primary tool for data hiding.

Inheritance access table

Base classDerived: private modeDerived: protected modeDerived: public mode
privateNot inheritedNot inheritedNot inherited
protected→ private→ protected→ protected
public→ private→ protected→ public

Getters and Setters

C++
class Student {

  private:

    int age;

  public:

    void setAge(int a) {     // setter — validates before setting

      if(a > 0) age = a;

    }

    int getAge() {           // getter — read-only access

      return age;

    }

};

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report