Inheritance in C++: Visibility Modes & Protected Members
1. What is Inheritance? (In Simple Words)
In real life, a child inherits qualities (like eye color or height) and property from their parents.
In C++, Inheritance works the exact same way!
It is the process by which a new class (called the Derived Class or Child Class) automatically gets all the variables and functions of an existing class (called the Base Class or Parent Class).
Vehiclespeed, fuelCapacity, startEngine()
CarGets all Vehicle features automatically + adds: numberOfDoors, openSunroof()
Why is Inheritance so Important?
- Code Reusability: You don’t have to rewrite the same code again and again. Write common features in the parent class once, and all child classes inherit them automatically.
- Real-World “IS-A” Modeling: Easily models relationships like:
- A Car IS-A Vehicle.
- A Student IS-A Person.
- A Manager IS-AN Employee.
2. Syntax of Inheritance
class ChildClassName : visibility_mode ParentClassName {
// Extra features specific to Child Class
};
Exam Tip: If you forget to write the visibility mode (e.g.
class Dog : Animal), C++ makes itprivateby default!
3. Why Do We Need the protected Access Specifier?
Let’s understand the problem with private and public:
- If you make a variable
private, it is hidden from the outside world — but it is also completely hidden from child classes! A child cannot access its own parent’s private variables! - If you make a variable
public, your child class can access it — but now any code inmain()can also change it directly, which breaks data security!
To solve this, C++ created the protected specifier:
A protected variable is hidden from main() (just like private), but fully accessible to Child Classes!
class Employee {
protected:
float salary; // Child classes can use this, but main() CANNOT!
private:
int bankPin; // Strictly private! Even child classes cannot touch this!
};
4. The Three Visibility Modes (Guaranteed Exam Question!)
When a child class inherits a parent class, the Visibility Mode decides how the parent’s members appear inside the child class:
| Base Class Member | Under public Mode |
Under protected Mode |
Under private Mode |
|---|---|---|---|
private |
Never Inherited (Hidden) | Never Inherited (Hidden) | Never Inherited (Hidden) |
protected |
Becomes protected |
Becomes protected |
Becomes private |
public |
Becomes public |
Becomes protected |
Becomes private |
Easy Explanation of the 3 Modes:
- Public Inheritance (
class B : public A): The most common mode. Everything stays in its original state (publicstayspublic,protectedstaysprotected). - Protected Inheritance (
class B : protected A): Bothpublicandprotectedmembers of the parent becomeprotectedin the child. - Private Inheritance (
class B : private A): Bothpublicandprotectedmembers becomeprivateinside the child class.
5. Complete Working C++ Program: Single Inheritance
#include <iostream>
#include <string>
using namespace std;
// 1. BASE CLASS (Parent)
class Person {
protected:
string name;
int age;
public:
void setPersonDetails(string n, int a) {
name = n;
age = a;
}
void displayPerson() const {
cout << "Name: " << name << "\tAge: " << age << endl;
}
};
// 2. DERIVED CLASS (Child publicly inherits from Person)
class Student : public Person {
private:
int rollNo;
float marks;
public:
void setStudentDetails(string n, int a, int r, float m) {
// Can directly access 'protected' variables 'name' and 'age'!
name = n;
age = a;
rollNo = r;
marks = m;
}
void displayReportCard() const {
displayPerson(); // Calls inherited public function
cout << "Roll: " << rollNo << "\tMarks: " << marks << endl;
}
};
int main() {
Student s;
s.setStudentDetails("Ankit Verma", 20, 101, 88.5f);
// Calling inherited public function
s.displayReportCard();
// The line below gives a COMPILER ERROR because 'name' is protected:
// s.name = "Vikram";
return 0;
}
DAVV Exam Preparation Tips
Frequently Asked 5-Mark Questions:
- What is inheritance in C++? State two benefits.
Model Answer:- Define: The mechanism where a derived child class acquires variables and methods of a base parent class.
- Benefits: 1. Code Reusability (no rewriting), 2. Models real-world “IS-A” relationships.
- Explain the role of the
protectedaccess specifier in C++.
Model Answer:protectedmembers remain hidden frommain(), but can be directly accessed and modified by child classes.
Frequently Asked 10-Mark Questions:
- Explain the three visibility modes in C++ inheritance: Public, Protected, and Private derivation. Draw the 3x3 access table and demonstrate with a working program.
Model Answer:- Write base and derived class syntax.
- Draw the 3x3 Visibility Table clearly showing how
private,protected, andpublicmembers transform. - Explain how public derivation keeps members available to
main(). - Write out complete code defining
Person(base) andStudent(derived) using protected members.