In this lesson, we will learn how to use public, protected, and private inheritance in C++ using examples to understand the topic better.
Different access modes can derive a child class from the base class in C++ inheritance.
An access specifier is a defining piece of code that can specify which program elements are permitted access to a particular variable or other pieces of code. Access specifiers specify how a class’s members—its attributes and methods—can be accessed. For instance, in C++ inheritance, three keywords are known as access specifiers.
Accessibility | Within Same Class | In Derived Class | Outside Class |
---|---|---|---|
Private Members | Yes | No | No |
Protected Members | Yes | Yes | No |
Public Members | Yes | Yes | Yes |
Example
class Base { public: int var1; protected: int var2; private: int var3; }; class PublicDerived: public Base { // var1 is public // var2 is protected // var3 is not accessible from PublicDerived }; class ProtectedDerived: protected Base { // var1 is protected // var2 is protected // var3 is not accessible from ProtectedDerived }; class PrivateDerived: private Base { // var1 is private // var2 is private // var3 is not accessible from PrivateDerived };
Public inheritance makes protected base class members protected in the derived class and makes public base class members public in the derived class.
Example: C++ Access Specifiers Public Inheritance
// C++ program demonstrates public inheritance #include <iostream> using namespace std; class Base { private: int privateVariable = 100; protected: int protectedVariable = 200; public: int publicVariable = 300; // function to access private member int getPrivateMember() { return privateVariable; } }; class PublicDerived: public Base { public: // function to access protected member from Base int getProtected() { return protectedVariable; } }; int main() { PublicDerived obj; cout << "Private Access Specifier = " << obj.getPrivateMember() << endl; cout << "Protected Access Specifier = " << obj.getProtected() << endl; cout << "Public Access Specifier = " << obj.publicVariable << endl; return 0; }
Output
Private Access Specifier = 100 Protected Access Specifier = 200 Public Access Specifier = 300
In this case, we used public mode to derive PublicDerived
from Base. Because of this, in PublicDerived:
Base Class public and protected members are protected in the derived class due to protected inheritance.
Example: C++ Access Specifiers Protected Inheritance
// C++ program demonstrates protected inheritance #include <iostream> using namespace std; class Base { private: int privateVariable = 100; protected: int protectedVariable = 200; public: int publicVariable = 300; // function to access private member int getPrivateMember() { return privateVariable; } }; class ProtectedDerived: protected Base { public: // function to access protected members from Base int getProtected() { return protectedVariable; } // function to access public members from Base int getPublic() { return publicVariable; } }; int main() { ProtectedDerived obj; cout << " error getPrivateMember() is inaccessible within this context" << endl; cout << "Protected Access Specifier = " << obj.getProtected() << endl; cout << "Public Access Specifier = " << obj.getPublic() << endl; return 0; }
Output
error getPrivateMember() is inaccessible within this context Protected Access Specifier = 200 Public Access Specifier = 300
Due to private inheritance, the base class of public and protected members becomes private in the derived class.
Example: C++ Access Specifiers Private Inheritance
// C++ program demonstrates private inheritance #include <iostream> using namespace std; class Base { private: int privateVariable = 100; protected: int protectedVariable = 200; public: int publicVariable = 300; // function to access private member int getPrivateMember() { return privateVariable; } }; class PrivateDerived: private Base { public: int getProtected() { return protectedVariable; } int getPublic() { return publicVariable; } }; int main() { PrivateDerived obj; cout << "error: 'int Base::getPrivateMember()' is inaccessible within this context" << endl; cout << "Protected Access Specifier = " << obj.getProtected() << endl; cout << "Public Access Specifier = " << obj.getPublic() << endl; return 0; }
Output
error: 'int Base::getPrivateMember()' is inaccessible within this context Protected Access Specifier = 200 Public Access Specifier = 300
This concludes the C++ Access Specifiers lesson. In The next lesson, you will learn about Inheritance Types in C++ and their usage.