DN
Program of Function Overloading in C++
#include<stdio.h>
using namespace std;
floar area(float r);
float area(float r,float h);
int main()
{
float radius,height,result1,result2;
cout<<"Enter the radius & height :\n";
cin>>radius>>height;
result1=area(radius);
result2=area(radius,height);
cout<<"\n Area of circle: "<<result1;
cout<<"\n Area of cylinder: "<<result2;
return 0;
}
float area(float r)
{
retrun(3.14*r*r);
}
float area(float r,float h)
{
retrun(2*3.14*r*h);
}
1
Function overloading Example
Lets take an example to understand function overloading in C++.
#include <iostream>
using namespace std;
class Addition {
public:
int sum(int num1,int num2) {
return num1+num2;
}
int sum(int num1,int num2, int num3) {
return num1+num2+num3;
}
};
int main(void) {
Addition obj;
cout<<obj.sum(20, 15)<<endl;
cout<<obj.sum(81, 100, 10);
return 0;
}
Output:
35
191
2
Write a C++ program to find area of triangle, circle,and rectangle
using function overloading.
#include<iostream.h>
#include<conio.h>
const float pi=3.14;
float area(float n,float b,float h)
{
float ar;
ar=n*b*h;
return ar;
}
float area(float r)
{
float ar;
ar=pi*r*r;
return ar;
}
float area(float l,float b)
{
float ar;
ar=l*b;
return ar;
}
void main()
{
float b,h,r,l;
float result;
clrscr();
cout<<“\nEnter the Base & Hieght of Triangle: \n”;
cin>>b>>h;
result=area(0.5,b,h);
cout<<“\nArea of Triangle: “<<result<<endl;
cout<<“\nEnter the Radius of Circle: \n”;
cin>>r;
result=area(r);
cout<<“\nArea of Circle: “<<result<<endl;
cout<<“\nEnter the Length & Bredth of Rectangle: \n”;
3
cin>>l>>b;
result=area(l,b);
cout<<“\nArea of Rectangle: “<<result<<endl;
getch();
}
Aim:
To write a program to find the area of a circle and rectangle using
function overloading.
Algorithm:
Step 1: Start
Step 2: Declare a class over with data members and member functions.
Step 3: Define and declare the function volume() to find the volume of
circle and rectangle.
Step 4: Create an object for the class over.
Step 5: Read the radius of the circle.
Step 6: Call the function volume() to find and print the volume of the
circle.
Step 7: Read the length and breadth of the rectangle.
Step 8: Call the function volume() to find and print the volume of the
rectangle.
Step 9: Stop
4
Program Code:
#include<iostream.h>
#include<conio.h>
class over
{
float l,b,r,area;
public:
void volume(float,float);
void volume(float);
};
void over::volume(float l, float b)
{
cout<<"Area of rectangle = "<<l*b;
void over::volume(float r)
{
cout<<"Area of circle = "<<3.14*r*r;
}
void main()
{
over o;
clrscre():
float r,l,b;
cout<<"\nEnter radius: ";
cin>>r;
o.volume(r);
cout<<"\n\nEnter lenght and breadth: ";
cin>>l>>b;
o.volume(l,b);
5
getch();
}
Output:
Enter Radius : 5
Area of circle = 78.5
Enter lenght and breadth : 3 4
Area of rectangle = 12
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
6
#include <iostream>
using namespace std;
/* Function arguments are of different data type */
long add(long, long);
float add(float, float);
int main()
{
long a, b, x;
float c, d, y;
cout << "Enter two integers\n";
cin >> a >> b;
x = add(a, b);
cout << "Sum of integers: " << x << endl;
cout << "Enter two floating point numbers\n";
cin >> c >> d;
y = add(c, d);
cout << "Sum of floats: " << y << endl;
return 0;
}
long add(long x, long y)
{
long sum;
sum = x + y;
return sum;
}
7
float add(float x, float y)
{
float sum;
sum = x + y;
return sum
}
Function overloading Example
Lets take an example to understand function overloading in C++.
#include <iostream>
using namespace std;
class Addition {
public:
int sum(int num1,int num2) {
return num1+num2;
}
int sum(int num1,int num2, int num3) {
return num1+num2+num3;
}
};
int main(void) {
Addition obj;
cout<<obj.sum(20, 15)<<endl;
cout<<obj.sum(81, 100, 10);
return 0;
}
Output:
35
191
8
#include<iostream>
using namespace std;
class measure
public:
void shape(int r);
void shape(int l,int b);
void shape(float t,int d,int e);
void shape(long a);
void shape(float c, long int g);
void shape(double j);
void shape(float h, double f);
};
void measure::shape(int r)
cout<<"area of the circle is "<<3.14*r*r;
void measure::shape(int l,int b)
9
cout<<"area of the rectangle is"<<l*b;
void measure::shape(float t,int d,int e)
cout<<"area of the triangle is"<<t*d*e;
void measure::shape(long a)
cout<<"area of the square is"<<a*a;
void measure::shape(float c, long int g)
cout<<"Volume of the cone is "<<(1/3)*3.14*c*c*g;
void measure::shape(double j)
cout<<"Volume of the sphere is "<<(4/3)*3.14*j*j*j;
void measure::shape(float h, double f)
10
{
cout<<"\nVolume of the Cylinder is "<<3.14*f*f*h;
int main()
int r,d,e,l,b;
float t,c,h;
long a;
int ch;
double j,f;
long int g;
measure obj;
cout<<"\tCALCULATION OF AREA AND VOLUME";
cout<<"\n\n1. area of circle";
cout<<"\n2. area of rectangle";
cout<<"\n3. area of triangle";
cout<<"\n4. area of square";
cout<<"\n5. Volume of the cone";
cout<<"\n6. Volume of the sphere";
11
cout<<"\n7. Volume of the cylinder";
cout<<"\n\tEnter your choice ";
cin>>ch;
switch(ch)
case 1:
cout<<"enter the value of radius of the circle \n";
cin>>r;
obj.shape(r);
break;
case 2:
cout<<"enter the sides of rectangle \n";
cin>>l>>b;
obj.shape(l,b);
break;
case 3:
cout<<"enter the sides of triangle \n";
cin>>d>>e;
obj.shape(0.5,d,e);
12
break;
case 4:
cout<<"enter the sides of square";
cin>>a;
obj.shape(a);
break;
case 5:
cout<<"\nEnter the radius of the cone";
cin>>c;
cout<<"\nEnter the height of the cone";
cin>>g;
obj.shape(c,g);
break;
case 6:
cout<<"\nEnter the radius";
cin>>b;
obj.shape(b);
break;
case 7:
13
cout<<"\nEnter the radius";
cin>>f;
cout<<"\nEnter the height";
cin>>h;
obj.shape(h,f);
break;
default:
cout<<"\nThe choice entered is a wrong choice";
return 0;
OUTPUT : :
C++
1 /* C++ Program to Find Area of Shapes using Function Overloading
2 */
14
4 CALCULATION OF AREA AND VOLUME
6 1. area of circle
7 2. area of rectangle
8 3. area of triangle
9 4. area of square
10 5. Volume of the cone
11 6. Volume of the sphere
12 7. Volume of the cylinder
13
14 Enter your choice 1
15 enter the value of radius of the circle 3
16
17 area of the circle is 28.26
18
Process returned 0
15