Lab No.
07
Objective: To implement and understand the working of Multiple Inheritance
Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit from more
than one classes. i.e one sub class is inherited from more than one base classes.
Syntax:
class subclass_name : access_mode base_class1, access_mode base_class2, ....
{
// body of subclass
};
Here, the number of base classes will be separated by a comma (‘, ‘) and access mode for every base
class must be specified.
// C++ program to explain
// multiple inheritance
#include<iostream>
using namespace std;
// first base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
// second base class
class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle\n";
}
};
// sub class derived from two base classes
class Car : public Vehicle, public FourWheeler {
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
Output
This is a Vehicle
This is a 4 wheeler Vehicle
Lab Task
[Link] and execute above c++ code.
02. Make a class named flower with a data member to calculate the number of flowers in a basket.
Create two other class named Rose and Jasmine to calculate the number of rose and jasmine in the
basket. Print the number of flowers of each type and the total number of flowers in the basket.