Working of the default constructor in Hierarchical Inheritance
In this, we are going to see how a Default Constructor works in Hierarchical Inheritance  in C++ Programming Language.
 


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

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

// base class
class university
{
public:
    // default constructor of the base clas
    university()
    {
        cout << "\nI am the default constructor of the base class.\n";
    }
};

// derived class 1 inheriting the base class
class student : public university
{
};

// derived class 2 inheriting the base class
class faculty : public university
{
};

void main()
{
    clrscr();
    // create the objects of the derived classes
    student obj1; // constructor of base class will be called
    faculty obj2; // constructor of the base class will be called
    getch();
}

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

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

#include <iostream>
using namespace std;

// base class
class university
{
public:
    // default constructor of the base clas
    university()
    {
        cout << "\nI am the default constructor of the base class.\n";
    }
};

// derived class 1 inheriting the base class
class student : public university
{
};

// derived class 2 inheriting the base class
class faculty : public university
{
};

int main()
{
    // create the objects of the derived classes
    student obj1; // constructor of base class will be called
    faculty obj2; // constructor of the base class will be called
    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