Simple Program for Function Overloading Using C++ Programming
Aim
To calculate the area of circle, rectangle and triangle using function overloading.
Simple Program for Function Overloading Algorithm/Steps:
- STEP 1: Start the program.
- STEP 2: Declare the class name as fn with data members and member functions.
- STEP 3: Read the choice from the user.
- STEP 4: Choice=1 then go to the step 5.
- STEP 5: The function area() to find area of circle with one integer argument.
- STEP 6: Choice=2 then go to the step 7.
- STEP 7: The function area() to find area of rectangle with two integer argument.
- STEP 8: Choice=3 then go to the step 9.
- STEP 9: The function area() to find area of triangle with three arguments, two as Integer and one as float.
- STEP 10: Choice=4 then stop the program.
Simple Program for Function Overloading
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14
class fn {
public:
void area(int); //circle
void area(int, int); //rectangle
void area(float, int, int); //triangle
};
void fn::area(int a) {
cout << "Area of Circle:" << pi * a*a;
}
void fn::area(int a, int b) {
cout << "Area of rectangle:" << a*b;
}
void fn::area(float t, int a, int b) {
cout << "Area of triangle:" << t * a*b;
}
void main() {
int ch;
int a, b, r;
clrscr();
fn obj;
cout << "\n\t\tFunction Overloading";
cout << "\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n:?;
cout << ?Enter your Choice : ";
cin>>ch;
switch (ch) {
case 1:
cout << "Enter Radious of the Circle:";
cin>>r;
obj.area(r);
break;
case 2:
cout << "Enter Sides of the Rectangle:";
cin >> a>>b;
obj.area(a, b);
break;
case 3:
cout << "Enter Sides of the Triangle:";
cin >> a>>b;
obj.area(0.5, a, b);
break;
case 4:
exit(0);
}
getch();
}
Sample Output
Function Overloading
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 2
Enter the Sides of the Rectangle: 5 5
Area of Rectangle is: 25
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 4
C++ Function Example Programs
- Simple Example Program for Function In C++
- Simple Example Program for Function Find Smallest Number In C++
- Simple Example Program for Function to Find Factorial In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Area Of Circle using Friend Function - C++Program
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming