Program to implement Hierarchical Inheritance using Constructor
In this, we are going to see how to implement Hierarchical Inheritance using Constructor in C++ Programming Language.

 

The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include<conio.h>

// base class
class Vehicle
{
    public:
    Vehicle()
    {
        cout << "This is a Vehicle" << endl;
    }
};

// first sub class
class Car : public Vehicle
{
};

// second sub class
class Bus : public Vehicle
{
};

// main function
void main()
{
    clrscr();
    // creating object of sub class will
    // invoke the constructor of base class
    Car obj1;
    Bus obj2;
    getch();
}

//.............Coded by RISHAB NAIR

The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

// base class
class Vehicle
{
    public:
    Vehicle()
    {
        cout << "This is a Vehicle" << endl;
    }
};

// first sub class
class Car : public Vehicle
{
};

// second sub class
class Bus : public Vehicle
{
};

// main function
int main()
{
    // creating object of sub class will
    // invoke the constructor of base class
    Car obj1;
    Bus obj2;
    return 0;
}

//.............Coded by RISHAB NAIR

#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post