Access Control in Inheritance public, protected, and private in c++

Access Control in Inheritance public, protected, and private in c++

In the C++ programming language, class parameter data member access control indicates how the data type members (class parameter variables and function methods) of an existing user-defined class can be accessed and managed from outside the current class. These class access data object modifiers, public, protected, and private, also play an important role in class data types. Class data access methods also play an important role when a base or parent class is defined as a derived or subclass of another class. For example, in the inheritance concept of parent and subclasses.

Access Control in Inheritance public, protected, and private in c++

There are three primary class data access specifiers in C++.

  • Public specifiers – User-defined class data members in public class data types can be accessed and managed from both inside and outside the class.
  • Protected specifiers – User-defined class data members in protected class data types can only be accessed and managed from within the class and its derived subclasses. But they are not accessible from outside the current class.
  • Private Specifiers – User-defined class data members in a private class data type can only be accessed and managed within the class itself, and not from outside the class or from derived subclasses.

Class Data Member Access Control Concepts in C++ Inheritance.

When a user-defined class inherits from another class, the access level of the data type members of the base parent root class depends on the type of inheritance method (public, protected, or private) used in the class access method. Here, the class data access modifier applied to the class inheritance declaration determines how the base or root class members are inherited from the base class to the derived subclass, and how they can be accessed and managed by the derived class if needed.

Public Inheritance Concepts in C++.

Public inheritance is one of the most common concepts in C++ programming, and it is often used to represent the “is-a” relationship model. In this concept, the public and protected class data member types of the base (or root) class remain public and protected in the derived subclass class.

Public data member parameters of a user-defined base root class become public in the derived subclass class.

Protected class data members of a user-defined base root class become protected in the derived subclass class.

In this, private class data members of the base or root class are not inherited from and are directly user-inaccessible in the derived subclass class.

Example of public class data member inheritance.

#include <iostream>

class Root {

public:

int publicArgs; // here Public class member argument define

void publicMethod() {

std::cout << “Root/Base class public method define” << std::endl;

}

protected:

int protectedArgs; // here Protected class member argument define

void protectedMethod() {

std::cout << “Root/Base class protected method define” << std::endl;

}

private:

int privateArgs; // here Private class member argument define

void privateMethod() {

std::cout << “Root/Base class private method define” << std::endl;

}

};

class Derived : public Root {

Public:

void preview() {

// Accessing public member from Root class

publicArgs = 3; // here it publicArgs Allowed

publicMethod(); // here it is Allowed publicMethod

// here it Accessing protected class member from the base or Root class

protectedArgs = 4; //here it Allowed protectedArgs

protectedMethod(); //here it Allowed protectedMethod

// here it Accessing private class member from Root class (not allowed) method

// privateArgs = 7; // Display Error – ‘privateArgs’ is private class method within this context

// privateMethod(); // Display Error – ‘privateMethod’ is private class method within this context

}

};

int main() {

Derived args;

args.preview();

return 0;

}

Explanation of public class data member inheritance.

  • Here in this example, the derived class inherits publicly from the base class (Root).
  • The public data members (publicArgs, publicMethod()) of the base class are directly accessible in the derived class, and can be directly accessed and managed within the class.
  • Whereas the protected class members (protectedArgs, protectedMethod()) of the base class are also accessible in the derived subclass class through the data type.
  • The private members (privateArgs, privateMethod()) of the base class are not accessible in either the derived or subclass class, and remain private class data members for the base class.

Protected Inheritance Concepts in C++.

The use of protected class data member inheritance methods is minimized in C++ programming. When a user-defined class data is inherited with protected inheritance behavior in a C++ program, the public and protected class data members of the base or parent class acquire protected behavior in the derived subclass.

Public class data members of the base or root class acquire protected behavior in the derived subclass.

And protected class data members declared and defined within the class remain protected.

Private class data members defined within the class are still not inherited and remain inaccessible to the program.

Example of protected class data member inheritance.

#include <iostream>

class Root {

public:

int publicArgs; // here Public class member argument define

void publicMethod() {

std::cout << “Root/Base public class method define” << std::endl;

}

protected:

int protectedArgs; // here Protected class member argument define

void protectedMethod() {

std::cout << “Root/Base protected class method define” << std::endl;

}

private:

int privateArgs; // here Private class member argument define

void privateMethod() {

std::cout << “Root/ Base private class method define” << std::endl;

}

};

class Derived : protected Root { // here we define a Protected class inheritance

Public:

void preview() {

publicArgs = 2; // here it permits, but publicArgs is now protected class method

publicMethod(); // here it permits, but publicMethod() is now protected class method

protectedArgs = 7; //here it permits

protectedMethod(); //here it permits

// privateArgs = 9; // Display Error – ‘privateArgs’ is private class method within this context

// privateMethod(); //Display Error – ‘privateMethod’ is private class method within this context

}

};

int main() {

Derived args;

args.preview();

// here it tries to Access from outside the derived subclass class method

// args.publicArgs = 11; // Display Error – ‘publicArgs’ is now protected method

// args.publicMethod(); // Display Error – ‘publicMethod()’ is now protected method

return 0;

}

Explanation of protected class data member inheritance.

  • In this example, a derived subclass inherits from a base class using protected inheritance.
  • As a result, public data members of the base class become protected in the derived class.
  • Protected class data members in the existing class always remain protected.

Private class data members cannot be accessed in the existing class.

The main difference between public and protected class inheritance in C++ programming is that protected class inheritance prevents access to inherited class data members. User-defined class data members that are normally defined as public in the derived subclass (with public inheritance features) become protected in nature, and cannot be directly accessed from outside the existing class.

Private Inheritance Class Concepts in C++.

In C++ programming, the concept of private inheritance class data members is used to represent a “has-a” relationship model, where derived subclasses defined in the current class do not directly represent the interface of the base parent class to the outside world.

In this case, the public class data and protected data members of the base or root class become private in the derived subclass.

Private class data members are still inaccessible to the derived subclass.

Example of private inheritance class data member.

#include <iostream>

class Root {

public:

int publicArgs; // here the public class member argument is defined

vo publicMethod() {

std::cout << “Root/Base public class method defined” << std::endl;

}

protected:

int protectedArgs; // here Protected class member argument define

void protectedMethod() {

std::cout << “Root/Base protected class method define” << std::endl;

}

private:

int privateArgs; // here Private class member argument define

void privateMethod() {

std::cout << “Root/Base private class method define” << std::endl;

}

};

class Derived : private Root { // here we define a Private inheritance

Public:

void preview() {

publicArgs = 4; // here it permit, but publicArgs is now private in Derived subclass method

publicMethod(); // here it permit, but publicMethod() is now private in Derived subclass method

protectedArgs = 5; // here it permit, but protectedArgs is now private in Derived subclass

protectedMethod(); //here it permits

// privateArgs = 7; // Display Error – ‘privateArgs’ is a private class method within this context

// privateMethod(); // Display Error – ‘privateMethod’ is a private class method within this context

}

};

int main() {

Derived args;

args.preview();

// Here it is accessed from outside the derived class

// args.publicArgs = 9; // Display Error – ‘publicArgs’ is now private in the Derived class method

// args.publicMethod(); // Display Error – ‘publicMethod()’ is now private in the Derived method

return 0;

}

Explanation of private inheritance class data member.

  • In this example, the Derived subclass inherits from the Base root class’s properties by applying private inheritance.
  • Because of this, both the public and protected class data members of this base parent class are now private in nature in the derived subclass.
  • In this example, the private class data members still retain the inaccessible behavior.

Leave a Reply