Welcome to The Coding College! In this tutorial, we’ll dive into Inheritance Access in C++. Access specifiers play a crucial role in determining how members of a base class are inherited by derived classes. Understanding inheritance access is essential to design robust and secure object-oriented systems.
What Is Inheritance Access?
Inheritance access in C++ defines how the public, protected, and private members of a base class are inherited and accessible in derived classes. The access level depends on:
- The access specifier used in the base class for the member.
- The mode of inheritance specified while deriving the class.
Modes of Inheritance:
- Public
- Protected
- Private
Access Specifiers in C++
1. Public Members
- Accessible to all.
- Remain public in the derived class if inheritance mode is
public
.
2. Protected Members
- Accessible only within the class and its derived classes.
- Do not remain directly accessible to objects of the derived class.
3. Private Members
- Accessible only within the base class.
- Not accessible in the derived class.
Syntax
class BaseClass {
public:
// Public members
protected:
// Protected members
private:
// Private members
};
class DerivedClass : <inheritance_mode> BaseClass {
// Derived class members
};
Inheritance Modes
Public Inheritance
- Public Members: Remain public in the derived class.
- Protected Members: Remain protected in the derived class.
- Private Members: Not accessible in the derived class.
Protected Inheritance
- Public Members: Become protected in the derived class.
- Protected Members: Remain protected in the derived class.
- Private Members: Not accessible in the derived class.
Private Inheritance
- Public Members: Become private in the derived class.
- Protected Members: Become private in the derived class.
- Private Members: Not accessible in the derived class.
Examples of Inheritance Access
Example 1: Public Inheritance
#include <iostream>
using namespace std;
class Base {
public:
int publicVar = 10;
protected:
int protectedVar = 20;
private:
int privateVar = 30;
};
class Derived : public Base {
public:
void showVars() {
cout << "Public Var: " << publicVar << endl; // Accessible
cout << "Protected Var: " << protectedVar << endl; // Accessible
// cout << "Private Var: " << privateVar; // Not Accessible
}
};
int main() {
Derived obj;
obj.showVars();
cout << "Public Var from object: " << obj.publicVar << endl; // Accessible
// cout << obj.protectedVar; // Not Accessible
return 0;
}
Output:
Public Var: 10
Protected Var: 20
Public Var from object: 10
Example 2: Protected Inheritance
#include <iostream>
using namespace std;
class Base {
public:
int publicVar = 10;
protected:
int protectedVar = 20;
private:
int privateVar = 30;
};
class Derived : protected Base {
public:
void showVars() {
cout << "Public Var: " << publicVar << endl; // Accessible as protected
cout << "Protected Var: " << protectedVar << endl; // Accessible
}
};
int main() {
Derived obj;
obj.showVars();
// cout << obj.publicVar; // Not Accessible (now protected in Derived)
return 0;
}
Output:
Public Var: 10
Protected Var: 20
Example 3: Private Inheritance
#include <iostream>
using namespace std;
class Base {
public:
int publicVar = 10;
protected:
int protectedVar = 20;
private:
int privateVar = 30;
};
class Derived : private Base {
public:
void showVars() {
cout << "Public Var: " << publicVar << endl; // Accessible as private
cout << "Protected Var: " << protectedVar << endl; // Accessible as private
}
};
int main() {
Derived obj;
obj.showVars();
// cout << obj.publicVar; // Not Accessible (now private in Derived)
return 0;
}
Output:
Public Var: 10
Protected Var: 20
Summary Table
Base Class Member | Public Inheritance | Protected Inheritance | Private Inheritance |
---|---|---|---|
Public | Public | Protected | Private |
Protected | Protected | Protected | Private |
Private | Not Inherited | Not Inherited | Not Inherited |
Practical Applications
- Use public inheritance when the derived class “is a” type of the base class.
- Use protected inheritance when you want to hide the base class interface from the outside but allow it to be accessed in derived classes.
- Use private inheritance for internal implementation details that are not exposed to external users or further derived classes.
Explore More at The Coding College
Keep learning about object-oriented programming principles and advanced C++ features with The Coding College!