Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit the properties and behaviors of another. Understanding how access specifiers affect inheritance is crucial for designing robust and maintainable C++ programs. This tutorial will explore public, protected, and private inheritance, their implications on member accessibility, and when to use each type.
Inheritance in C++ can be modified using three access specifiers: public, protected, and private. These specifiers determine how the members of a base class are accessible in derived classes. Properly using these specifiers helps maintain encapsulation and control over class interfaces, which is essential for effective software design.
Public inheritance is the most common type of inheritance. When a class inherits from another class publicly, all public and protected members of the base class become public and protected members of the derived class, respectively. Private members remain inaccessible to the derived class.
1#include <iostream>23class Base {4public:5int publicVar;6protected:7int protectedVar;8private:9int privateVar;10};1112class Derived : public Base {13public:14void display() {15std::cout << "Public: " << publicVar << std::endl; // Accessible16std::cout << "Protected: " << protectedVar << std::endl; // Accessible17// std::cout << "Private: " << privateVar << std::endl; // Error: inaccessible18}19};2021int main() {22Derived d;23d.publicVar = 10;24// d.protectedVar = 20; // Error: protected in Base, not accessible here25// d.privateVar = 30; // Error: private in Base, not accessible here26d.display();27return 0;28}
Note
In private inheritance, all public and protected members of the base class become private members of the derived class. Private members remain inaccessible.
1#include <iostream>23class Base {4public:5int publicVar;6protected:7int protectedVar;8private:9int privateVar;10};1112class Derived : private Base {13public:14void display() {15std::cout << "Public: " << publicVar << std::endl; // Accessible, but now private16std::cout << "Protected: " << protectedVar << std::endl; // Accessible, still private17// std::cout << "Private: " << privateVar << std::endl; // Error: inaccessible18}19};2021int main() {22Derived d;23// d.publicVar = 10; // Error: now private in Derived24// d.protectedVar = 20; // Error: private in Derived25// d.privateVar = 30; // Error: private in Base, not accessible here26d.display();27return 0;28}
In this example:
Account is a base class with public and protected members.SavingsAccount inherits from Account publicly, allowing it to use the deposit method and access the balance.CheckingAccount inherits from Account privately, making its methods and variables inaccessible outside of CheckingAccount.| Type of Inheritance | Accessibility in Derived Class |
|---|---|
| Public | Public members: public Protected members: protected Private members: not accessible |
| Protected | Public members: protected Protected members: protected Private members: not accessible |
| Private | Public members: private Protected members: private Private members: not accessible |
Now that you understand public, protected, and private inheritance, the next step is to explore different types of inheritance such as single inheritance, multiple inheritance, and hierarchical inheritance. These concepts will further enhance your ability to design complex and flexible C++ programs.
Stay tuned for the next tutorial on "Types of Inheritance" where we'll dive deeper into these topics!