EXERCISE - 9

A) Write a program to illustrate single inheritance.
B) Write a program to illustrate hierarchical inheritance.
C) Write a program to illustrate multiple inheritance.
D) Write a program to illustrate multilevel inheritance.
E) Write a program to illustrate hybrid inheritance.
F) Write a program to show Virtual Base Class.
A) AIM: Write a program to illustrate single inheritance.
SOURCE CODE:
#include<iostream>
using namespace std;
class vehicle
{
    public:
        vehicle()
        {
            cout<<"\nThis is vehicle class.";
        }
};
class car:public vehicle
{
    public:
        car()
        {
            cout<<"\nThis is a car.";
        }
};
int main()
{
    car c;
}
OUTPUT:
This is vehicle class.
This is a car.

B) AIM: Write a program to illustrate hierarchical inheritance.
SOURCE CODE:
#include<iostream>
using namespace std;
class vehicle
{
    public:
        vehicle()
        {
            cout<<"\nVehicles are:";
        }
};
class bike:public vehicle
{
    public:
    bike()
    {
        cout<<"\nBike is a 2 wheeler vehicle.";
    }
};
class car:public vehicle
{
    public:
        car()
        {
            cout<<"\nCar is a 4 wheeler vehicle.";
        }
};
int main()
{
    bike b;
    car c;
}
OUTPUT:
Vehicles are:
Bike is a 2 wheeler vehicle.
Vehicles are:
Car is a 4 wheeler vehicle.

C) AIM: Write a program to illustrate multiple inheritance.
SOURCE CODE:
#include<iostream>
using namespace std;
class vehicle
{
    public:
        vehicle()
        {
            cout<<"\nThis is vehicle class.";
        }
};
class fourwheeler
{
    public:
        fourwheeler()
        {
            cout<<"\nThis is a 4 wheeler vehicle.";
        }
};
class car:public vehicle,fourwheeler
{
    public:
        car()
        {
            cout<<"\nThis is a car.";
        }
};
int main()
{
    car c;
}
OUTPUT:
This is vehicle class.
This is a 4 wheeler vehicle.
This is a car.

D) AIM: Write a program to illustrate multilevel inheritance.
SOURCE CODE:
#include<iostream>
using namespace std;
class vehicle
{
    public:
        vehicle()
        {
            cout<<"\nThis is vehicle class.";
        }
};
class fourwheeler:public vehicle
{
    public:
        fourwheeler()
        {
            cout<<"\nThis is a 4 wheeler vehicle.";
        }
};
class car:public fourwheeler
{
    public:
        car()
        {
            cout<<"\nThis is a car.";
        }
};
int main()
{
    car c;
}
OUTPUT:
This is vehicle class.
This is a 4 wheeler vehicle.
This is a car.

E) AIM: Write a program to illustrate hybrid inheritance.
SOURCE CODE:
#include<iostream>
using namespace std;
class vehicle
{
    public:
        vehicle()
        {
            cout<<"\nThis is vehicle class.";
        }
};
class farevehicle
{
    public:
        farevehicle()
        {
            cout<<"\nFare vehicle.";
        }
};
class car:public vehicle
{
};
class bus:public vehicle,farevehicle
{
};
int main()
{
    car c;
    bus b;
}

OUTPUT:
This is vehicle class.
This is vehicle class.
Fare vehicle.

F) AIM: Write a program to show Virtual BaseClass.
SOURCE CODE:
#include<iostream>
using namespace std;
class A
{
    protected:
        int a;
};
class B:virtual public A
{
    protected:
        int b;
};
class C:virtual public A
{
    protected:
        int c;
};
class D:public B,C
{
    protected:
        int d;
    public:
        void getdata()
        {
            cout<<"Enter a,b,c,d values:"<<endl;
            cin>>a>>b>>c>>d;
        }
        void putdata()
        {
            cout<<"Values are:"<<endl;
            cout<<"a="<<a<<"\nb="<<b<<"\nc="<<c<<"\nd="<<d;
        }
};
int main()
{
    D d;
    d.getdata();
    d.putdata();
}
OUTPUT:
Enter a,b,c,d values:
2
6
4
3
Values are:
a=2
b=6
c=4
d=3