Posted By:
3 Jul, 2013 12:55 pm PDT

Overloading functions in C++

Can functions be overloaded?
Attachment:None


In C++, two functions can have the same name if their parameter type or number is different. The property of the function to have same name is known as overloading. Hence functions can be overloaded if they either have a different parameter type or different number of parameters.

For example:

#include<iostream.h>

// 'volume' function with 3 parameters
int volume(int l,int b,int h)  

{

int vol;   

vol=l*b*h;

return vol;

}

// 'volume' function with 2 parameters
int volume(int b,int h)

{

int vol;                                                                       

vol=0.3*b*h;

return vol;

}

void main()

{

int v1,v2;

v1=volume(2,3,4);

v2=volume(2,4);

cout<<"\nVolume of rectangle:"<<v1;

cout<<"\nVolume of cone:"<<v2;

}
Was it useful?  




Please login to reply to this problem.