C++ Access Modifiers : Public Private Protected

Advertisements

C++ Access Modifiers

Access modifiers defines the scope of members of BaseClass into DerivedClass.

C++ supports the followling access modifiers:

  • Private Modifier
  • Protected Modifier
  • Public Modifier

Private Modifier: The scope of private members are restricted to its own class. Private members can't be accessed by the derived class or in main() function.

Protected Modifier: The scope of protected members are restricted to its own class and derived class. Protected members can be accessed by the derived class but they can't be accessed in main() function.

Public Modifier: Public members can be accessed by its own class, derived class and in main() function.

Own classDerived classmain()
Private
Protected
Public

				

Scope of base class members in derived class.

Access base class as private

When a derived class inherit base class as private, protected and public members of base class will become private members of derived class.

Access base class as protected

When a derived class inherit base class as protected, protected and public members of base class will become protected members of derived class.

Access base class as public

When a derived class inherit base class as public, protected members of base class will become protected members of derived class and public members of base class will become public members of derived class.

Advertisement