Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism

Public, Protected and Private Inheritance

Data Structures using C++ Program

Question: Explain the usage of public, private and protected access specifiers.

Public, Protected and Private Inheritance

• The access specifier or access modifier such as public, private or protected determines how elements of base class are inherited by the derived class.

• When the access specifier for the inherited base class is public then all public members of the base class become public members of the derived class.

• If the access specifier is private then all public members of the base class become private members of the derived class.

• The private members of base class are inaccessible to derived class. The public members of the base class become private members to derived class but those are accessible to derived class. If the access specifier is not present then it is taken as private by default.


Inheriting Base Class in Public Mode


For example

#include <iostream>

using namespace std;

class Base

{

   int x

public:

void set x(int n)

{

   x=n;

}

void show_x()

{

   cout <<"\n x= "<<x;

}

};

//Inherit as public

class derived : public Base

{

   int y;

public:

   void set_ y(int n)

   {

   y = n;

   }

   void show_y()

   {

   cout<<"\n y= "<<y;

   }

};

int main()

{

   derived obj;//object of derived class

   int x, y;

   cout<<"\n Enter the value of x",

   cin>>x;

   cout<<"\n Enter the value of y";

   cin>>y;

   //using obj of derived class base class member is accessed

   obj.set_x(x);

   obj.set_y(y); // access member of derived class

   obj.show_x(); // access member of base class

   obj.show_y(); // access member of derived class

   return 0;

}

Output

Enter the value of x30

Enter the value of y70

x= 30

y= 70

 

• In above program the obj is an object of derived class. Using obj we are accessing the member function of base class. The derived class inherits base class using an access specifier public. Now following program will contain error.

Inheriting Base Class in Private Mode

#include <iostream>

using namespace std;

class Base

{

   int x;

public:

void set_x(int n)

{

   x = n;

}

void show_x( )

{

   cout<<"\n x= "<<X;

}

};

// Inherit as private

class derived : private Base

{

int y;

public:

void set y_(int n)

{

   y = n;

}

void show_y()

{

   cout<<"\n y= "<<y;

}

};

using namespace std;

int main()

{

   derived obj://object of derived class

   int x, y,

   cout<<"\n Enter the value of x";

   cin>>x;

   cout<<"\n Enter the value of y";

   cin>>y:

   obj.set_x(x); // error: not accessible

   obj.set y(y);

   obj.show x(); // access member of base class

   obj.show_y(); // error:not accessible

   return 0;

}

 

• As indicated by the comments the above program will generate error messages "not accessible". This is because the derived class inherits the base class privately. Hence the public members of base class become private to derived class.


Using Protected

• The protected access specifier is equivalent to the private specifier with the sole exception that protected members of a base class are accessible to members of any class derived from that base. Outside the base or derived classes, protected members are not accessible. Following program illustrates the same.

#include <iostream>

using namespace std;

class Base

{

protected:

   int x;

public:

   void set_x(int n)

   {

      x = n;

   }

   void show_x( )

   {

      cout<<"\n x= "<<x;

   }

};

class derived: public Base

{

   int y;

   public:

   void set_y(int n)

   {

      y=n;

   }

   void show_ xy()

   {

      //can access protected member in derived class

      cout<<"\nderived::x = "<<x;

      cout<<"\n y= "<<y;

   }

};

int main()

{

   derived obj;

   int x, y;

   cout<<"\n Enter the value of x";

   cin>>x;

   cout<<"\n Enter the value of y";

   cin>>y;

   obj.set_x(x);

   obj.set_y(y); // access member of derived class

   obj.show_x();

   obj.show_xy(); // access member of derived class

   cout<<"\n Setting another value to x"<<endl;

   //protected members become private to outside base and derived class

   obj.x=100; //error:not accessible

   return 0;

}

 

Review Question

1. Explain the usage of public, private and protected access specifiers.

 

Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism : Tag: Data Structure, C++, C Programing : Data Structures using C++ Program - Public, Protected and Private Inheritance


Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Electron Devices

EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Circuits and Network Analysis

EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Engineering Drawing - Laboratory

ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus - Laboratory

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Devices and Circuits Laboratory

EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation