0% found this document useful (0 votes)
177 views

Experiment No 7 - Function Overloading in C++

The document describes 5 programs that demonstrate function overloading in C++ to calculate the area of different shapes. Program 1 overloads the area function to calculate the area of a circle and rectangle. Program 2 enhances this by adding a triangle and using different parameter types. Program 3 declares the function prototypes before main. Program 4 adds a menu driven program. Program 5 defines a class with overloaded area functions for a circle and rectangle.

Uploaded by

vinayak kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views

Experiment No 7 - Function Overloading in C++

The document describes 5 programs that demonstrate function overloading in C++ to calculate the area of different shapes. Program 1 overloads the area function to calculate the area of a circle and rectangle. Program 2 enhances this by adding a triangle and using different parameter types. Program 3 declares the function prototypes before main. Program 4 adds a menu driven program. Program 5 defines a class with overloaded area functions for a circle and rectangle.

Uploaded by

vinayak kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment no 7: Function overloading

AIM7: Write a C++ program to find area of triangle, circle, and rectangle
using function overloading.

Requirements:

Theoretical Description:

Program 1:

#include<iostream.h>
#include<conio.h>
float area(float a)
{
return (3.14*a*a);
}
int area(int p,int q)
{
return(p*q);
}
void main()
{
clrscr();
cout<<"Area of circle:"<<area(6);
cout<<"Area of Rectangle:"<<area(5,6);
getch();

Outputs:

Program 2:

#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, 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”;
cin>>l>>b;
result=area(l,b);
cout<<“\nArea of Rectangle: “<<result<<endl;
getch();
}

Program 3:
#include<iostream>
using namespace std;
int area(int);
int area(int,int);
float area(float);
float area(float,float);
int main()
{
int s,l,b;
float r,bs,ht;
cout<<"Enter side of a square:";
cin>>s;
cout<<"Enter length and breadth of rectangle:";
cin>>l>>b;
cout<<"Enter radius of circle:";
cin>>r;
cout<<"Enter base and height of triangle:";
cin>>bs>>ht;
cout<<"Area of square is"<<area(s);
cout<<"\nArea of rectangle is "<<area(l,b);
cout<<"\nArea of circle is "<<area(r);
cout<<"\nArea of triangle is "<<area(bs,ht);
}
int area(int s)
{
return(s*s);
}
int area(int l,int b)
{
return(l*b);
}
float area(float r)
{
return(3.14*r*r);
}
float area(float bs,float ht)
{
return((bs*ht)/2);
}
Explanation:

Sample Input
Enter side of a square:2
Enter length and breadth of rectangle:3 6
Enter radius of circle:3
Enter base and height of triangle:4 4
Sample Output
Area of square is4
Area of rectangle is 18
Area of circle is 28.26
Area of triangle is 8

Program 4:
#include<iostream>
#include<cstdlib>
using namespace std;

float area(float r)
{
return(3.14 * r * r);
}
float area(float b,float h)
{
return(0.5 * b * h);
}
float area(float l,float b)
{
return (l * b);
}
int main()
{
float b,h,r,l;
int ch;

do
{
cout<<"\n\n *****Menu***** \n";
cout<<"\n 1. Area of Circle";
cout<<"\n 2. Area of Triangle";
cout<<"\n 3. Area of Rectangle";
cout<<"\n 4. Exit";
cout<<"\n\n Enter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"\n Enter the Radius of Circle : ";
cin>>r;
cout<<"\n Area of Circle : "<<area(r);
break;
}
case 2:
{
cout<<"\n Enter the Base & Height of Triangle : ";
cin>>b>>h;
cout<<"\n Area of Triangle : "<<area(b,h);
break;
}
case 3:
{
cout<<"\n Enter the Length & Bredth of Rectangle : ";
cin>>l>>b;
cout<<"\n Area of Rectangle : "<<area(l,b);
break;
}
case 4:
exit(0);
default:
cout<<"\n Invalid Choice... ";
}
}

while(ch!=4);
return 0;
}

Program 5:
#include
#include
class shape
{
float l,b,r,ac,ar;
public:
void getc(void)
{
cout<<"Enter radius of circle\n";
cin>>r;
cout<<"\n**********************************************************\n";
}
void area(void)
{
ac=3.14*r*r;
cout<<"\n**************************************************************\n";
cout<<"\nRadius of circle is "<<r;
cout<<"\nArea of circle is "<<ac;
}
void area(float x, float y)
{
l=x;
b=y;
ar=l*b;
cout<<"\nLength of rectangle is "<<l;
cout<<"\nBreadth of rectangle is "<<b;
cout<<"\nArea of rectangle is "<<ar;
}
};
void main(void)
{
float m,n;
clrscr();
class shape s1,s2;
s1.getc();
cout<<"\nEnter length and breadth of rectangle\n";
cin>>m>>n;
s1.area();
cout<<"\n*************************************************************\n";
s2.area(m,n);
getch();
}

You might also like