Monday, May 6, 2013

Function Overloading in C++

Overloading refers to the use of the same thing for different purposes.Function overloading means use of same function name to create functions that perform variety of different tasks.We can design a family of functions with one function name but different argument lists.The function will perform different operation depending on the argument list of the function.Here is a program in which i used two function with same name but one function will add 2 numbers other will add 3 numbers.

#include<iostream.h>
#include<conio.h>
void add(int x,int y,int z);
void add(int x,int y);
void main(){
int x=5,y=6,z=7;
add(x,y);
add(x,y,z);
getch();
}
void add(int x,int y){
int s;
s=x+y;
cout<<"the sum is\n"<<s;
}
void add(int x,int y,int z){
int s;
s=x+y+z;
cout<<"\nthe sum is\n"<<s;
 

Output:





similarly you do addition and multiplication with functions of same name,but different numbers of arguments. 

No comments:

Post a Comment