In this, we are going to see program for Area and Volume of Sphere/Cuboid using Function Overloading in Single Inheritance in C++ Programming Language.
The Code given below can be used in TURBO C++ Compilers: -
#include<iostream.h>
#include<conio.h>
class Ar
{
public:
//method overloading
void Area(int r)
{
cout << "Area of sphere = " << (float)4 * 3.14 * r * r;
}
void Area(int l, int b, int h)
{
cout << "Total Surface Area of Cuboid = " << 2 * ((l * b) + (b * h) + (h * l)) << "\n";
cout << "\nLateral Surface Area of Cuboid = " << 2 * h * (l + b);
}
};
class Vol : public Ar
{
public:
//method overloading
void Volume(int r)
{
cout << "\nVolume of sphere = " << (float)4 * 3.14 * r * r * r / 3;
}
void Volume(int l, int b, int h)
{
cout << "\nVolume of Cuboid = " << l * b * h;
}
};
void main()
{
clrscr();
Vol obj;
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;
obj.Area(r);
obj.Volume(r);
break;
case 'C':
int l, b, h;
cout << "Enter lenght, breadth and height of the Cuboid respectively: ";
cin >> l >> b >> h;
obj.Area(l, b, h);
obj.Volume(l, b, h);
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;
class Ar
{
public:
//method overloading
void Area(int r)
{
cout << "Area of sphere = " << (float)4 * 3.14 * r * r;
}
void Area(int l, int b, int h)
{
cout << "Total Surface Area of Cuboid = " << 2 * ((l * b) + (b * h) + (h * l)) << "\n";
cout << "\nLateral Surface Area of Cuboid = " << 2 * h * (l + b);
}
};
class Vol : public Ar
{
public:
//method overloading
void Volume(int r)
{
cout << "\nVolume of sphere = " << (float)4 * 3.14 * r * r * r / 3;
}
void Volume(int l, int b, int h)
{
cout << "\nVolume of Cuboid = " << l * b * h;
}
};
int main()
{
Vol obj;
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;
obj.Area(r);
obj.Volume(r);
break;
case 'C':
int l, b, h;
cout << "Enter lenght, breadth and height of the Cuboid respectively: ";
cin >> l >> b >> h;
obj.Area(l, b, h);
obj.Volume(l, b, h);
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