Program for Area and Volume of Sphere/Cuboid using Function Overloading in Multilevel Inheritance
In this, we are going to see a program for area and volume of Sphere/Cuboid using Function Overloading in Multilevel Inheritance using C++ Programming Language.
 


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

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

class Ar
{
    public:
    int larea, tarea;
    float area, vol;
    //method overloading
    void Area(int r)
    {
        area = (float)4 * 3.14 * r * r;
    }
    void Area(int l, int b, int h)
    {
        tarea = 2 * ((l * b) + (b * h) + (h * l));
        larea = 2 * h * (l + b);
    }
};

class Vol : public Ar
{
    public:
    //method overloading
    void Volume(int r)
    {
        vol = (float)4 * 3.14 * r * r * r / 3;
    }
    void Volume(int l, int b, int h)
    {
        vol = l * b * h;
    }
};

class Display : public Vol
{
    public:
    void sdisplay()
    {
        cout << "Area of sphere = " << area;
        cout << "\nVolume of sphere = " << vol;
    }
    void cdisplay()
    {
        cout << "Total Surface Area of Cuboid = " << tarea;
        cout << "\nLateral Surface Area of Cuboid = " << larea;
        cout << "\nVolume of Cuboid = " << vol;
    }
};

void main()
{
    clrscr();
    Display obj;
    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: ";
        cin >> r;
        obj.Area(r);
        obj.Volume(r);
        obj.sdisplay();
        break;

    case 'C':
    case 'c':
        int l, b, h;
        cout << "Enter length, breadth and height of the Cuboid respectively: ";
        cin >> l >> b >> h;
        obj.Area(l, b, h);
        obj.Volume(l, b, h);
        obj.cdisplay();
        break;
        
    default:
        cout << "Not a valid choice.";
    }
    getch();
}

//...........Coded by Sahil Shaikh


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

#include <iostream>
using namespace std;

class Ar
{
    public:
    int larea, tarea;
    float area, vol;
    //method overloading
    void Area(int r)
    {
        area = (float)4 * 3.14 * r * r;
    }
    void Area(int l, int b, int h)
    {
        tarea = 2 * ((l * b) + (b * h) + (h * l));
        larea = 2 * h * (l + b);
    }
};

class Vol : public Ar
{
    public:
    //method overloading
    void Volume(int r)
    {
        vol = (float)4 * 3.14 * r * r * r / 3;
    }
    void Volume(int l, int b, int h)
    {
        vol = l * b * h;
    }
};

class Display : public Vol
{
    public:
    void sdisplay()
    {
        cout << "Area of sphere = " << area;
        cout << "\nVolume of sphere = " << vol;
    }
    void cdisplay()
    {
        cout << "Total Surface Area of Cuboid = " << tarea;
        cout << "\nLateral Surface Area of Cuboid = " << larea;
        cout << "\nVolume of Cuboid = " << vol;
    }
};

int main()
{
    Display obj;
    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: ";
        cin >> r;
        obj.Area(r);
        obj.Volume(r);
        obj.sdisplay();
        break;

    case 'C':
    case 'c':
        int l, b, h;
        cout << "Enter length, breadth and height of the Cuboid respectively: ";
        cin >> l >> b >> h;
        obj.Area(l, b, h);
        obj.Volume(l, b, h);
        obj.cdisplay();
        break;
        
    default:
        cout << "Not a valid choice.";
    }
    return 0;
}

//...........Coded by Sahil Shaikh


#ENJOY CODING

Post a Comment

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

Previous Post Next Post