In this, we are going to see a program for area and volume of Sphere/Cuboid using Function Overloading in Multiple Inheritance using C++ Programming Language.Â
The Code given below can be used in TURBO C++ Compilers: -
#include<iostream.h>
#include<conio.h>
class calculate_area
{
public:
float a;
float l_area;
//function overloading
void area(int r)
{
a = 4 * 3.14 * r * r;
}
void area(int l, int b, int h)
{
a = 2 * ((l * b) + (b * h) + (h * l));
l_area = 2 * h * (l + b);
}
};
class calculate_volume
{
public:
float v;
//function overloading
void volume(int r)
{
v = 4 * 3.14 * r * r * r / 3;
}
void volume(int l, int b, int h)
{
v = l * b * h;
}
};
class display : public calculate_area, public calculate_volume
{
public:
void disp_sphere()
{
cout << "Area of sphere = " << a << endl;
cout << "Volume of sphere = " << v << endl;
}
void disp_cuboid()
{
cout << "Total Surface Area of Cuboid = " << a << endl;
cout << "Lateral Surface Area of Cuboid = " << l_area << endl;
cout << "Volume of Cuboid = " << v << endl;
}
};
void main()
{
clrscr();
display obj1;
char ch;
cout << "Enter choice: Sphere[S] / Cuboid[C]: ";
cin >> ch;
switch (ch)
{
case 'S':
int r;
cout << "Enter radius of the sphere: ";
cin >> r;
obj1.area(r);
obj1.volume(r);
obj1.disp_sphere();
break;
case 'C':
int l, b, h;
cout << "Enter length, breadth and height of the Cuboid respectively: ";
cin >> l >> b >> h;
obj1.area(l, b, h);
obj1.volume(l, b, h);
obj1.disp_cuboid();
break;
default:
cout << "Invalid choice.";
}
getch();
}
//...........Coded by Ananya Vidyadharan
The Code given below can be used in gcc/g++ Compilers: -
#include <iostream>
using namespace std;
class calculate_area
{
public:
float a;
float l_area;
//function overloading
void area(int r)
{
a = 4 * 3.14 * r * r;
}
void area(int l, int b, int h)
{
a = 2 * ((l * b) + (b * h) + (h * l));
l_area = 2 * h * (l + b);
}
};
class calculate_volume
{
public:
float v;
//function overloading
void volume(int r)
{
v = 4 * 3.14 * r * r * r / 3;
}
void volume(int l, int b, int h)
{
v = l * b * h;
}
};
class display : public calculate_area, public calculate_volume
{
public:
void disp_sphere()
{
cout << "Area of sphere = " << a << endl;
cout << "Volume of sphere = " << v << endl;
}
void disp_cuboid()
{
cout << "Total Surface Area of Cuboid = " << a << endl;
cout << "Lateral Surface Area of Cuboid = " << l_area << endl;
cout << "Volume of Cuboid = " << v << endl;
}
};
int main()
{
display obj1;
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;
obj1.area(r);
obj1.volume(r);
obj1.disp_sphere();
break;
case 'C':
case 'c':
int l, b, h;
cout << "Enter length, breadth and height of the Cuboid respectively: ";
cin >> l >> b >> h;
obj1.area(l, b, h);
obj1.volume(l, b, h);
obj1.disp_cuboid();
break;
default:
cout << "Invalid choice.";
}
return 0;
}
//...........Coded by Ananya Vidyadharan
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP