3 types of Visibility modes in C++


3 types of Visibility modes in C++

These three visibility modes in C++ that specify how the members of the class are accessible inside a program. There are 3 types of Visibility modes in C++.

  • Private
  • Protected
  • Public

Private visibility mode in C++

This is the default visibility mode of class members in C++. Private members of class can be accessed by only other member functions and friends of class.

We can use keyword private to declare  data members as well as member functions to be private members of the class.  Private members of class can’t be accessed through the object of class. 

Protected visibility mode in C++

We can use keyword protected to declare  data members as well as member functions to be protected members of the class.  

Protected members of a class can’t be called directly through the object of class but they have to be accessed through member functions of the same class or child class.

Public visibility mode in C++

We can use keyword public to declare  data members as well as member functions to be protected members of the class. Public members basically provide the interface to the outside program components. They can be accessed through the object of class. 

class student
{
private:
            int rollno;
            void getrollno();
protected:
            char name[20];
            void getname();
public:
          void show();
};
student obj;

In the above code, rollno is a private data member. So it can be accessed through member functions of same class getrollno(), getname() and show().

getrollno() is a private member function. So it can be accessed through getname() and show() member functions.

name is a protected data member. So it can be accessed through member functions of same class getrollno(), getname() and show() as well as  member functions of child of this class.

getname() is a protected member function. So it can be accessed through member functions getrollno(), show() as well as  member functions of child of this class.

show() is a public member function. So it can be accessed through object obj of this class as well as other member functions of this class.



Spread the love
Lesson tags: 3 types of Visibility modes in C++, private visibility mode in c++, protected visibility mode in c++, public visibility mode in c++
Back to: C++ Programming Notes
Spread the love