function overloading:
function overloading means using same function name to perform variety of task. But in function overloading we must consider two things. They are:
- 1. Either the number of arguments in function should be different (the number of input to function should vary).
- 2. if the number of arguments are same then the data type of the function arguments should be different number of parameter different.
Note: return type has no role in function overloading
int sum(int a, int b)
{
return (a+b);
}
int sum(int a1, int b1, int c1)
{
return(a1+b1+c1);
}
int sum(int d)
{
return (d+5);
}
void main()
{
cout<<"The sum of one variable"<<sum(10)<<endl;
cout<<"The sum of two variable"<<sum(10,20)<<endl;
cout<<"The sum of three vriable"<<sum(10,20,30)<<endl;
getch();
}
Q. write a program to calculate the area of rectangle and triangle using concept of function overloading.
type II
#include<iostream.h>
#include<conio.h>
using namespace std;
int area(int l, int b)
{
return (l*b)
}
float area(int b, float h)
{
return (0.5*b*h);
}
void main()
{
int l1,b1;
float h;
cout<<"Enter the value of l1,b1 & h1<<endl;
cin>>l1>>b1>>h1;
cout<<"The area of rectangle is <<area(l1,b1)<<endl;
cout<<"The area of triangle is<<area(l1,h1);
getch();
}
No comments:
Post a Comment