OOP

Access Modifier

In object-oriented programming, public, private, and protected are called access modifiers. They are keywords that define the accessibility of a class’s members (properties and methods) to other parts of the program.

These access modifiers help control how code interacts with a class and its data, promoting data integrity and modularity.

Practice

Try to answer following questions:

  1. In the following snippet of code which access modifier is used for:
    • data members
    • member functions
  2. Which access modifier has not been used in the following snippet
  3. What will happen when the compiler reaches cout<<dist.feet
  4. What is the correct method of obtaining the value of feet of dist object
class Distance
{
    private: 
      int feet;
      float inches;
    public:
      int getFeet()
      {
        return feet;
      }
      void setFeet(int newValue)
      {
         feet = newValue;
      }
};
int main()
{
    Distance dist;
    cout << dist.feet;
    return 0;
}