Inheritance

1. Write a program to illustrate
A) Single inheritance.
B) Hierarchical inheritance.
C) Multiple inheritance.
D) Multilevel inheritance.
E) Hybrid inheritance.
2. Write a Program in C++ to illustrate the order of execution of constructors and destructors in inheritance.
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.

AIM: Write a Program in C++ to illustrate the order of execution of constructors and destructors in inheritance.
SOURCE CODE:
#include<iostream>
using namespace std;
class BaseClass
{
    public:
        BaseClass()
        {
            cout<<"Base Class constructor called."<<endl;
        }
        ~BaseClass()
        {
            cout<<"Base class destructor called."<<endl;
        }
};
class DerivedClass:public BaseClass
{
    public:
        DerivedClass()
        {
            cout<<"Derived Class constructor called."<<endl;
        }
        ~DerivedClass()
        {
            cout<<"Derived class destructor called."<<endl;
        }
};
int main()
{
    DerivedClass obj;
}
OUTPUT:
Base Class constructor called.
Derived Class constructor called.
Derived class destructor called.
Base class destructor called.

No comments:

Post a Comment