In this lesson, you will learn
Before diving into the details of inheritance, let’s clarify some essential terminology:
Base Class (Parent Class or Super Class): The class whose properties and behaviors are inherited by another class.
Derived Class (Child Class or Sub Class): The class that inherits properties and behaviors from a base class.
In C++, there are several types of inheritance, each serving different purposes:
A derived class inherits from a single base class.
A derived class is created from another derived class.
A derived class inherits from multiple base classes.
Multiple derived classes inherit from a single base class.
A combination of different types of inheritance.
The syntax for inheriting from a base class is as follows:
class BaseClass {
// Base class members
};
class DerivedClass : access_specifier BaseClass {
// Derived class members
};
‘access_specifier
can be either public
, protected
, or private
and determines the visibility of the base class members in the derived class.
In C++, when a class is inherited in private mode, it affects the accessibility of the base class members in the derived class. Here’s how it works:
class Derived: private Base //Private derivation
{
// Members of derived class
}
Private Members of the Base Class: These are never accessible directly in the derived class, regardless of the inheritance mode (public, protected, or private).
Protected Members of the Base Class: When inherited in private mode, protected members of the base class become private in the derived class. This means they can only be accessed by the derived class itself, not by further derived classes.
Public Members of the Base Class: When inherited in private mode, public members of the base class also become private in the derived class. They can be accessed within the derived class but are hidden from any further derived classes or from external code.
#include<iostream>
using namespace std;
class Base {
public:
int publicVar;
protected:
int protectedVar;
private:
int privateVar;
};
class Derived : private Base {
public:
void display() {
publicVar = 10; // Accessible but treated as private in Derived
protectedVar = 20; // Accessible but treated as private in Derived
// privateVar = 30; // Not accessible, private in Base
cout << "Public Var: " << publicVar << endl;
cout << "Protected Var: " << protectedVar << endl;
}
};
int main() {
Derived obj;
obj.display();
// obj.publicVar = 40; // Error: publicVar is private in Derived
return 0;
}
Public Var: 10
Protected Var: 20
In C++, when a class is inherited in public mode, it maintains the accessibility of the base class members in the derived class. Here’s how the members of the base class behave when inherited publicly:
class Derived: public Base //Public derivation
{
// Members of derived class
}
Private Members of the Base Class: These are never accessible directly in the derived class, regardless of the inheritance mode (public, protected, or private).
Protected Members of the Base Class: When inherited in public mode, protected members of the base class remain protected in the derived class. This means they can be accessed by the derived class and any further derived classes but not by external code.
Public Members of the Base Class: When inherited in public mode, public members of the base class remain public in the derived class. This means they can be accessed both by external code and by any further derived classes.
#include<iostream>
using namespace std;
class Base {
public:
int publicVar;
protected:
int protectedVar;
private:
int privateVar;
};
class Derived : public Base {
public:
void display() {
publicVar = 10; // Accessible, remains public in Derived
protectedVar = 20; // Accessible, remains protected in Derived
// privateVar = 30; // Not accessible, private in Base
cout << "Public Var: " << publicVar << endl;
cout << "Protected Var: " << protectedVar << endl;
}
};
int main() {
Derived obj;
obj.display();
obj.publicVar = 40; // Accessible, remains public in Derived
// obj.protectedVar = 50; // Error: protectedVar is protected in Derived
return 0;
}
Public Var: 10
Protected Var: 20
amazing experience
You must be logged in to submit a review.