C++ Modes of Inheritance šŸŽÆ

beginner
10 min

C++ Modes of Inheritance šŸŽÆ

Welcome to the fascinating world of C++ Inheritance! In this lesson, we'll explore four fundamental modes of inheritance that will empower you to create powerful, reusable classes and objects.

Understanding Inheritance šŸ“

Inheritance is a mechanism that allows a new class (derived class) to acquire the properties and behaviors of an existing class (base class). It provides a means to create a hierarchy of classes, reducing code redundancy and promoting code reusability.

Base Class and Derived Class šŸ“

  • The class that is inherited is called the base class.
  • The class that does the inheriting is called the derived class.

Four Modes of Inheritance šŸ’”

C++ supports four modes of inheritance:

  1. Public Inheritance āœ…
  2. Protected Inheritance āœ…
  3. Private Inheritance āœ…
  4. Multiple Inheritance āœ…

Let's delve into each of these modes, understand their characteristics, and learn how to use them in practice.

Public Inheritance šŸ’”

Public inheritance is the default mode of inheritance in C++. It allows derived classes to access all the public and protected members of the base class.

Example of Public Inheritance

cpp
#include <iostream> using namespace std; // Base Class class Vehicle { public: string brand; int wheels; // Constructor Vehicle(string b, int w) : brand(b), wheels(w) { cout << "Vehicle: Created a " << brand << " with " << wheels << " wheels." << endl; } }; // Derived Class class Car : public Vehicle { public: string model; int seats; // Constructor Car(string b, string m, int w, int s) : Vehicle(b, w) { model = m; seats = s; cout << "Car: Created a " << model << " with " << seats << " seats." << endl; } }; int main() { Car myCar("Toyota", "Corolla", 4, 5); return 0; }

Output:

Vehicle: Created a Toyota with 4 wheels. Car: Created a Corolla with 5 seats.

Protected Inheritance šŸ’”

Protected inheritance is similar to public inheritance, but it allows the derived class to access only the protected members of the base class.

Example of Protected Inheritance

cpp
// Base Class class Vehicle { protected: string brand; int wheels; // Constructor Vehicle(string b, int w) : brand(b), wheels(w) { cout << "Vehicle: Created a " << brand << " with " << wheels << " wheels." << endl; } }; // Derived Class class Car : protected Vehicle { public: string model; int seats; // Constructor Car(string b, string m, int w, int s) : Vehicle(b, w) { model = m; seats = s; cout << "Car: Created a " << model << " with " << seats << " seats." << endl; } }; int main() { Car myCar("Toyota", "Corolla", 4, 5); return 0; }

Output:

Vehicle: Created a Toyota with 4 wheels. Car: Created a Corolla with 5 seats.

Private Inheritance šŸ’”

Private inheritance restricts the derived class from accessing the base class directly. Instead, the derived class can access the base class members indirectly via public or protected member functions of the base class.

Example of Private Inheritance

cpp
// Base Class class Vehicle { private: string brand; int wheels; // Constructor Vehicle(string b, int w) : brand(b), wheels(w) { cout << "Vehicle: Created a " << brand << " with " << wheels << " wheels." << endl; } public: void display() { cout << "Brand: " << brand << ", Wheels: " << wheels << endl; } }; // Derived Class class Car : private Vehicle { public: string model; int seats; // Constructor Car(string b, string m, int w, int s) : Vehicle(b, w) { model = m; seats = s; } // Function to display Car details void displayCar() { cout << "Car: Created a " << model << " with " << seats << " seats." << endl; display(); } }; int main() { Car myCar("Toyota", "Corolla", 4, 5); myCar.displayCar(); return 0; }

Output:

Vehicle: Created a Toyota with 4 wheels. Car: Created a Corolla with 5 seats. Brand: Toyota, Wheels: 4

Multiple Inheritance šŸ’”

Multiple inheritance allows a derived class to inherit from multiple base classes. This can be achieved by listing the base classes separated by commas in the derived class declaration.

Example of Multiple Inheritance

cpp
// Base Class 1 class Engine { public: string type; // Constructor Engine(string t) : type(t) { cout << "Engine: Created an " << type << " engine." << endl; } }; // Base Class 2 class Wheel { public: int count; // Constructor Wheel(int c) : count(c) { cout << "Wheel: Created " << count << " wheels." << endl; } }; // Derived Class class Car : public Engine, public Wheel { public: string model; int seats; // Constructor Car(string b, string m, string t, int w, int c, int s) : Engine(t), Wheel(w) { model = m; seats = s; cout << "Car: Created a " << model << " with " << seats << " seats and " << count << " wheels." << endl; } }; int main() { Car myCar("Toyota", "Corolla", "Gas", 4, 4, 5); return 0; }

Output:

Engine: Created a Gas engine. Wheel: Created 4 wheels. Car: Created a Corolla with 5 seats and 4 wheels.

Congratulations! You've now learned the four modes of inheritance in C++. Practice these concepts and apply them in your projects to create powerful, reusable classes and objects. Happy coding! šŸš€