#include <iostream>
using namespace std;
// Base class
class Vehicle {
public:
Vehicle() {
cout << "This is a vehicle." << endl;
}
};
// Derived class 1
class Car : public Vehicle {
public:
Car() {
cout << "This is a car." << endl;
}
};
// Derived class 2
class Sedan : public Car {
public:
Sedan() {
cout << "This is a sedan." << endl;
}
};
// Main function
int main() {
Sedan sedanObj;
return 0;
}
In this example, we have a base class called Vehicle that is inherited by a derived class called Car, and then Car is further inherited by another derived class called Sedan.
When we create an object of the Sedan class, it will call the constructors of all three classes in order: first Vehicle, then Car, and finally Sedan. This is an example of multilevel inheritance.
The output of running the above code would be:
This is a vehicle.
This is a car.
This is a sedan.
This demonstrates how the derived class Sedan inherits the properties and behavior of both its parent classes, Car and Vehicle.