C++ program to illustrate Multilevel Inheritance and Hierarchical Inheritance using Constructor


//Base class Vehicle with data members: VNo, no_of_wheel and max_speed
//Derived class Passenger_Carrying_Vehicle with data members: no_of_passengers
//Derived classes from above derived class
//they are Bus (with route, fare_per_person and helper_name as private members)
//and Taxi (with fare_per_km as data member)

#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
class Vehicle
{
    protected:
        int vno,no_of_wheel,max_speed;
    public:
        Vehicle(int vn,int now,int ms)
        {
            vno=vn;
            no_of_wheel=now;
            max_speed=ms;
        }
        DisplayVehicle()
        {
            cout<<endl<<"Vehicle No.: "<<vno;
            cout<<endl<<"No. of Wheel: "<<no_of_wheel;
            cout<<endl<<"Maximum Speed: "<<max_speed;
        }
};
class Passenger_Carrying_Vehicle:public Vehicle
{
    protected:
        int no_of_passengers;
    public:
        Passenger_Carrying_Vehicle(int vn,int now,int ms,int nop):Vehicle(vn,now,ms)
        {
            no_of_passengers=nop;
        }
        DisplayPassenger()
        {
            cout<<endl<<"No. of Passengers: "<<no_of_passengers;
        }
};
class Bus:public Passenger_Carrying_Vehicle
{
    private:
        char route[50];
        float fare_per_person;
        char helper_name[25];
    public:
        Bus(int vn,int now,int ms,int nop,char r[],float fpp,char hn[]):Passenger_Carrying_Vehicle(vn,now,ms,nop)
        {
            strcpy(route,r);
            fare_per_person=fpp;
            strcpy(helper_name,hn);
        }
        DisplayBus()
        {
            cout<<endl<<"Route: "<<route;
            cout<<endl<<"Fare per Person: "<<fare_per_person;
            cout<<endl<<"Helper Name: "<<helper_name;
        }
};
class Taxi:public Passenger_Carrying_Vehicle
{
    private:
        int fare_per_km;
    public:
        Taxi(int vn,int now,int ms,int nop,int fpk):Passenger_Carrying_Vehicle(vn,now,ms,nop)
        {
            fare_per_km=fpk;
        }
        DisplayTaxi()
        {
            cout<<endl<<"Fare per KM: "<<fare_per_km;
        }
};
int main()
{
    Bus b(123,4,60,34,"Baneshwor-Ratnapark",15,"Ram Hari Khadka");
    Taxi t(21,4,80,4,30);
    cout<<"Displaying Bus:";
    b.DisplayVehicle();
    b.DisplayPassenger();
    b.DisplayBus();
    cout<<endl<<endl<<"Displaying Taxi:";
    t.DisplayVehicle();
    t.DisplayPassenger();
    t.DisplayTaxi();
    return 0;
}

No comments:

Post a Comment