Jumat, 08 Juli 2011

To calculate the area of circle, rectangle and triangle using function overloading. July 8, 2011

ALGORITHM:
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.

PROGRAM:
01#include<iostream.h>
02#include<stdlib.h>
03#include<conio.h>
04#define pi 3.14
05class fn
06{
07      public:
08        void area(int); //circle
09        void area(int,int); //rectangle
10        void area(float ,int,int);  //triangle
11};
12 
13void fn::area(int a)
14{
15      cout<<"Area of Circle:"<<pi*a*a;
16}
17void fn::area(int a,int b)
18{
19      cout<<"Area of rectangle:"<<a*b;
20}
21void fn::area(float t,int a,int b)
22{
23      cout<<"Area of triangle:"<<t*a*b;
24}
25 
26void main()
27{
28     int ch;
29     int a,b,r;
30     clrscr();
31     fn obj;
32     cout<<"\n\t\tFunction Overloading";
33     cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n:”;
34     cout<<”Enter your Choice:";
35     cin>>ch;
36 
37     switch(ch)
38     {
39              case 1:
40                cout<<"Enter Radious of the Circle:";
41                cin>>r;
42                obj.area(r);
43                break;
44              case 2:
45                cout<<"Enter Sides of the Rectangle:";
46                cin>>a>>b;
47                obj.area(a,b);
48                break;
49              case 3:
50                cout<<"Enter Sides of the Triangle:";
51                cin>>a>>b;
52                obj.area(0.5,a,b);
53                break;
54              case 4:
55                exit(0);
56     }
57getch();
58}      

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

Tidak ada komentar:

Posting Komentar