Basic Program to show Hybrid Inheritance using Constructor
In this, we are going to see a basic program for Hybrid Inheritance with constructors in C++ Programming Language. 
 


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

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

// base class
class ParentClass1
{
    public:
    ParentClass1()
    {
        cout << "Constructor of Parent1 Class\n";
    }
};

// base class
class ParentClass2
{
    public:
    ParentClass2()
    {
        cout << "Constructor of Parent2 Class\n";
    }
};

// first sub class
class ChildClass1 : public ParentClass1, public ParentClass2
{
    public:
    ChildClass1()
    {
        cout << "Constructor of Child1 Class\n\n";
    }
};

// second sub class
class ChildClass2 : public ParentClass1, public ParentClass2
{
    public:
    ChildClass2()
    {
        cout << "Constructor of Child2 Class\n";
    }
};

// main function
void main()
{
    clrscr();
    ChildClass1 obj1;
    ChildClass2 obj2;
    getch();
}

//.......Coded by SHREYA IDATE


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

#include <iostream>
using namespace std;

// base class
class ParentClass1
{
    public:
    ParentClass1()
    {
        cout << "Constructor of Parent1 Class\n";
    }
};

// base class
class ParentClass2
{
    public:
    ParentClass2()
    {
        cout << "Constructor of Parent2 Class\n";
    }
};

// first sub class
class ChildClass1 : public ParentClass1, public ParentClass2
{
    public:
    ChildClass1()
    {
        cout << "Constructor of Child1 Class\n\n";
    }
};

// second sub class
class ChildClass2 : public ParentClass1, public ParentClass2
{
    public:
    ChildClass2()
    {
        cout << "Constructor of Child2 Class\n";
    }
};

// main function
int main()
{
    ChildClass1 obj1;
    ChildClass2 obj2;
    return 0;
}

//.......Coded by SHREYA IDATE


#ENJOY CODING

Post a Comment

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

Previous Post Next Post