Object Oriented Programming: Chapter 3: Inheritance and Compile Time Polymorphism

Public, Private and Protected Access Specifier

C++ Programming | Object Oriented Programming

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

Public, Private and Protected Access Specifier

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 members 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.

 

Object Oriented Programming: Chapter 3: Inheritance and Compile Time Polymorphism : Tag: Oops, Computer Programming : C++ Programming | Object Oriented Programming - Public, Private and Protected Access Specifier


Object Oriented Programming: Chapter 3: Inheritance and Compile Time Polymorphism



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 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


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

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


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation