Skip to content

C++ Program to Demonstrate Function Overloading

C++ Program to Demonstrate Function Overloading

Code:

/*C++ Program to Demonstrate Function Overloading*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define pi 3.14
class fn
{
 public:
  void area(int);
  void area(int,int);
  void area(float,int,int);

};
void fn::area(int a)
{
 cout<<” Area of Circle is”<<pi*a*a<<endl;
}
void fn::area(int a,int b)
{
 cout<<” Area of Rectangle is”<<a*b<<endl;
}
void fn::area(float t,int a,int b)
{
 cout<<” Area of Triangle is”<<t*a*b<<endl;
}
int main()
{
int ch;
clrscr();
fn obj;
cout<<” Function Overloading”<<endl;
cout<<“n 1.Circle n 2.Rectangle n 3.Triangle n 4.Exit :”;
cout<<“n Enter your Choice”<<endl;
cin>>ch;
switch (ch)
{
 case 1:
 {            int radius;
    cout<<“enter radius of circle:”<<endl;
    cin>> radius;
    obj.area(radius);
    break;
 }
  case 2:
 {            int len,brea;
    cout<<” Enter Length and Breath of Rectangle:”<<endl;
    cin>>len>>brea;
    obj.area(len,brea);
    break;
 }
  case 3:
 {            int hig,base;

    cout<<” Enter Height and Base of Triangle:”<<endl;
    cin>> hig>>base;
    obj.area(0.5,hig,base);
    break;
 }
  case 4:
 {
 exit(0);
 }

 default:
 {
 cout<<” Wrong Input”<<endl;
 }
}
getch();
return 0;
}

Sample Output:

Comment bellow for your Query and Feedback

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!