Table of Contents
Back Button
Back to Chapter
C++ Setting Up
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.

C++ Visibility Modes

Visibility Modes

  1. Public mode: If we derive a class from a public base class. Then the public member of the parent class will become public in the derived class and protected members of the base class will become protected in derived class.
  2. Protected mode: If we derive a child-class from a Protected base class. Then both public members and protected members of the base class will become protected in the derived class.
  3. Private mode: If we derive a subclass from a Private base class. Then both public members and protected members of the base class will become Private in the derived class.
class A { public: int x; protected: int y; private: int z; }; class B: public A { // x is public // y is protected // z is not accessible from B }; class C: protected A { // x is protected // y is protected // z is not accessible from C }; class D : private A // 'private' is default for classes { // x is private // y is private // z is not accessible from D };

The table below is a simple representation of how the base class members are inherited in each mode of inheritance.

The table below is a simple representation of how the base class members are inherited in each mode of inheritance.

#include using namespace std; //base parent class class Parent { // protected data members protected: int rollNo; }; // derived child class from public base class class Child : public Parent { public: void setrollNo(int id) { // Child class is able to access the inherited // protected data members of base class rollNo = id; } void displayId() { cout << "id_protected is: " << id_protected << endl; } }; // main function int main() { Child obj1; // member function of the derived class can // access the protected data members of the base class obj1.setId(19); obj1.displayId(); return 0; }
Output: id_protected is: 19
Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?