Program for Area and Volume of SphereCuboid using Function Overloading in Hybrid Inheritance
In this program, we are going to calculate Area and Volume of Sphere/Cuboid using Function Overloading in Hybrid 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 Radius
{
    public:
    int radius;
    int getRadius()
    {
        cin >> radius;
        return radius;
    }
};

// base class
class LBH
{
    public:
    int l, b, h;
    void getLBH()
    {
        cin >> l >> b >> h;
    }
};

// first sub class
class Areas : public Radius, public LBH
{
    public:
    void Area(int r)
    {
        cout << "Area of sphere = " << (float)4 * 3.14 * r * r;
    }
    void Area()
    {
        cout << "Total Surface Area of Cuboid = " << 2 * ((l * b) + (b * h) + (h * l));
        cout << "\nLateral Surface Area of Cuboid = " << 2 * h * (l + b);
    }
};

// second sub class
class Volumes : public Radius, public LBH
{
    public:
    void Volume(int r)
    {
        cout << "\nVolume of sphere = " << (float)4 * 3.14 * r * r * r / 3;
    }
    void Volume()
    {
        cout << "\nVolume of Cuboid = " << l * b * h;
    }
};

// main function
void main()
{
    clrscr();
    Areas obj1;
    Volumes obj2;
    char ch;
    cout << "Enter choice: Sphere[S] / Cuboid[C] ";
    cin >> ch;
    switch (ch)
    {
    case 'S':
    case 's':
        int r;
        cout << "Enter radius of the sphere: ";
        r = obj1.getRadius();
        obj1.Area(r);
        obj2.radius = r;
        obj2.Volume(r);
        break;
    case 'C':
    case 'c':
        cout << "Enter lenght, breadth and height of the Cuboid respectively: ";
        obj1.getLBH();
        obj2.l = obj1.l;
        obj2.b = obj1.b;
        obj2.h = obj1.h;
        obj1.Area();
        obj2.Volume();
        break;
    default:
        cout << "Not a valid choice.";
    }
    getch();
}

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


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

#include <iostream>
using namespace std;

// base class
class Radius
{
    public:
    int radius;
    int getRadius()
    {
        cin >> radius;
        return radius;
    }
};

// base class
class LBH
{
    public:
    int l, b, h;
    void getLBH()
    {
        cin >> l >> b >> h;
    }
};

// first sub class
class Areas : public Radius, public LBH
{
    public:
    void Area(int r)
    {
        cout << "Area of sphere = " << (float)4 * 3.14 * r * r;
    }
    void Area()
    {
        cout << "Total Surface Area of Cuboid = " << 2 * ((l * b) + (b * h) + (h * l));
        cout << "\nLateral Surface Area of Cuboid = " << 2 * h * (l + b);
    }
};

// second sub class
class Volumes : public Radius, public LBH
{
    public:
    void Volume(int r)
    {
        cout << "\nVolume of sphere = " << (float)4 * 3.14 * r * r * r / 3;
    }
    void Volume()
    {
        cout << "\nVolume of Cuboid = " << l * b * h;
    }
};

// main function
int main()
{
    Areas obj1;
    Volumes obj2;
    char ch;
    cout << "Enter choice: Sphere[S] / Cuboid[C] ";
    cin >> ch;
    switch (ch)
    {
    case 'S':
    case 's':
        int r;
        cout << "Enter radius of the sphere: ";
        r = obj1.getRadius();
        obj1.Area(r);
        obj2.radius = r;
        obj2.Volume(r);
        break;
    case 'C':
    case 'c':
        cout << "Enter lenght, breadth and height of the Cuboid respectively: ";
        obj1.getLBH();
        obj2.l = obj1.l;
        obj2.b = obj1.b;
        obj2.h = obj1.h;
        obj1.Area();
        obj2.Volume();
        break;
    default:
        cout << "Not a valid choice.";
    }
    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