Basic Program for hierarchical inheritance
In this, we are going to see a Basic Program of Hierarchical Inheritance in C++ Programming Language.
 


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

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

// single base class
class A 
{
    public:
    int x, y;
    void getdata()
    {
        cout << "\n\nEnter value of x and y:\n";
        cin >> x >> y;
    }
};

class B : public A // B is derived from class base
{
    public:
    void product()
    {
        cout << "Product= " << x * y;
    }
};

class C : public A // C is also derived from class base
{
    public:
    void sum()
    {
        cout << "Sum= " << x + y << endl;
    }
};

void main()
{
    clrscr();
    B obj1; // object of derived class B
    C obj2; // object of derived class C
    obj1.getdata();
    obj1.product();
    obj2.getdata();
    obj2.sum();
    getch();
}

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

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

#include <iostream>
using namespace std;

// single base class
class A 
{
    public:
    int x, y;
    void getdata()
    {
        cout << "\n\nEnter value of x and y:\n";
        cin >> x >> y;
    }
};

class B : public A // B is derived from class base
{
    public:
    void product()
    {
        cout << "Product= " << x * y;
    }
};

class C : public A // C is also derived from class base
{
    public:
    void sum()
    {
        cout << "Sum= " << x + y << endl;
    }
};

int main()
{
    B obj1; // object of derived class B
    C obj2; // object of derived class C
    obj1.getdata();
    obj1.product();
    obj2.getdata();
    obj2.sum();
    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