Inheritance is one of the core concepts of object-oriented programming (OOP). It allows a new class to inherit properties and behaviors (i.e., attributes and methods) from an existing class. The class that is inherited from is known as the base class (or parent class, superclass), and the class that inherits from the base class is known as the derived class (or child class, subclass).
Inheritance enables the creation of a hierarchical relationship between classes, promoting code reusability, extensibility, and maintainability.
Car is a Vehicle, meaning that Car inherits from Vehicle.Single Inheritance: A class inherits from a single base class.
Example: A Dog class inherits from an Animal class.
class Animal {
public:
void eat() { cout << "Eating" << endl; }
};
class Dog : public Animal {
public:
void bark() { cout << "Barking" << endl; }
};
Multiple Inheritance: A class can inherit from more than one base class.
Example: A FlyingCar class could inherit from both Car and FlyingMachine.
class Car {
public:
void drive() { cout << "Driving" << endl; }
};
class FlyingMachine {
public:
void fly() { cout << "Flying" << endl; }
};
class FlyingCar : public Car, public FlyingMachine {
};
Multilevel Inheritance: A class inherits from a derived class, creating a multi-level chain.
Example: Grandchild inherits from Child, which inherits from Parent.
class Parent {
public:
void speak() { cout << "Speaking from Parent" << endl; }
};
class Child : public Parent {
public:
void play() { cout << "Playing from Child" << endl; }
};
class Grandchild : public Child {
public:
void dance() { cout << "Dancing from Grandchild" << endl; }
};
Hierarchical Inheritance: Multiple derived classes inherit from a single base class.
Example: Both Dog and Cat inherit from the Animal class.
class Animal {
public:
void eat() { cout << "Eating" << endl; }
};
class Dog : public Animal {
public:
void bark() { cout << "Barking" << endl; }
};
class Cat : public Animal {
public:
void meow() { cout << "Meowing" << endl; }
};
Hybrid Inheritance: A combination of two or more of the above types of inheritance.
Car inherits from Vehicle, which might also have a Truck and Bus as subclasses.Let's look at an example where a Car class inherits from a more general Vehicle class.
#include <iostream>
using namespace std;
class Vehicle {
public:
string brand;
int year;
// Constructor
Vehicle(string b, int y) : brand(b), year(y) {}
// Method
void displayInfo() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
// Derived class from Vehicle
class Car : public Vehicle {
public:
int numDoors;
// Constructor for Car
Car(string b, int y, int doors) : Vehicle(b, y), numDoors(doors) {}
// Method specific to Car
void displayCarInfo() {
displayInfo(); // Call base class method
cout << "Number of doors: " << numDoors << endl;
}
};
int main() {
// Creating an object of the derived class Car
Car myCar("Toyota", 2020, 4);
// Displaying car information
myCar.displayCarInfo();
return 0;
}
Base Class (Vehicle): The Vehicle class has attributes brand and year, and a method displayInfo() to display the basic details of the vehicle.
Derived Class (Car): The Car class inherits from Vehicle and adds an additional attribute numDoors. It also has a method displayCarInfo() that calls the base class method displayInfo() and adds its own functionality.
Constructor Inheritance: The constructor of Car calls the constructor of Vehicle to initialize the brand and year attributes.
Method Inheritance: The derived class Car inherits the displayInfo() method from Vehicle and uses it in displayCarInfo().
Brand: Toyota, Year: 2020
Number of doors: 4
In C++, inheritance can be classified based on the access specifiers used:
Public Inheritance (public):
class Derived : public Base {
// Public members of Base become public in Derived
// Protected members of Base become protected in Derived
};
Protected Inheritance (protected):
class Derived : protected Base {
// Public and protected members of Base become protected in Derived
};
Private Inheritance (private):
class Derived : private Base {
// Public and protected members of Base become private in Derived
};
Open this section to load past papers