C++: Inheritance protected section comparison with all inheritance visibility modes like private, public, protected

 C++: Inheritance protected section comparison with all inheritance visibility modes like private, public, protected -In inheritance private member of a base class 


  • In inheritance private member of a base class cannot be inherited and thus it is not available in derived class directly

  • In case if we want to inherit some of private member of base class? It is can done by making those members as public so that it can be accessed in both derived class as well as outside the class also  but it violates data hiding feature of OOP. Hence without violating data hiding feature we can solve above issue using a special keyword called "protected".

  • C++ provides a third visibility modifier called protected through which private members can also be made inheritable to its immediately derived from it and also by it's member functions.

class Demo

{

 private:

//visible to member functions only

//not inheritable data

 protected:

//visible to member functions of it own

//and derived class

 public : 

//visible to all functions in program

};


Visibility of inherited members



Example 1: 

// protected members during inheritance in public 


mode


#include<iostream>

class A

{

 private :  int x;

 protected: int y;

 public : int z;

  void readA(int m)

{

x=m;

}

void printA()

{

cout<<"\nx="<<x;

}

};

class B:public A

{

 private: int a;

 public: void readB(int s,int t)

{

a=s; 

y=t; //y is a Protected data of base class

}

void printB()

{

cout<<"\nPrivate member of B class a="<<a;

cout<<"\nProtected member y="<<y;

}

};

main()

{

 B obj1;

 obj1.readA(101);

 obj1.readB(20,50);

 obj1.z=30; //public data of Base class

 obj1.printA();

 obj1.printB();

 cout<<"\npublic member of class A z="<<obj1.z;

}

Example 2:


//Inheriting protected data into derived using private 

//mode inheritance:


#include<iostream>

class A

{

 private :  int x;

 protected: int y;

 public : int z;

  void readA(int m)

{

x=m;

}

void printA()

{

cout<<"\nx="<<x;

}

};

class B:private A

{

 private: int a;

 public: void readB(int s,int t,int r,int w)

{

readA(r);

a=s; 

y=t; //y is a Protected data of base class

z=w; //public data of base class

}

void printB()

{

cout<<"\nPrivate member of B class 


a="<<a;

cout<<"\nProtected member y="<<y;

cout<<"\nPublic member z="<<z;

}

};

main()

{

 B obj1;

 obj1.readB(20,50,60,80);

 obj1.printB();


}


Example 3:


//Inheriting protected data into derived using protected 

//mode inheritance:


#include <iostream>


using namespace std;

class A

{

 private :  int x;

 protected: int y;

 public : int z;

  void readA(int m)

{

x=m;

}

void printA()

{

cout<<"\nx="<<x;

}

};

class B:protected A

{

 private: int a;

 protected:int h; 

public: void readB(int t)

{

  a=t; 

}

void printB()

{

cout<<"\nPrivate member of B class 


a="<<a;

}

};

class C : private B

{

 private : int g;

 public: 

void readC(int a,int b, int c, int d,int e,int f)

{

g=a;

readA(b);

readB(f);

h=c;

y=d;

z=e;

}

void printC()

{

printA();

printB();

cout<<"\nprivate member g="<<g

<<"\nprotected of A class y="<<y

<<"\nPublic of A class z="<<z

<<"\nprotected of B class h="<<h;

}

};


int main()

{

 C obj1;


 obj1.readC(20,50,60,80,90,100);

 obj1.printC();

 return 0;

}


Comments