Function overloading with examples in C++

"If a function name is overloaded with different arguments then It is called function overloading." It is a special feature of C++. In which, we can create family of function where each function shares a same function name. Here, every overloaded function is unique on the basis of different type and number of arguments. Function Overloading shows compile time / static polymorphism.
"यदि एक ही नाम के फंक्शन को भिन्न-भिन्न आर्गुमेंट प्रदान कर, ओवरलोड किया जाता है तब इसे फंक्शन ओवरलोडिंग कहा जाता है। यह C++ का एक विशेष फीचर है जिसमे हम फंक्शन की फॅमिली तैयार कर सकते है 
जहाँ प्रत्येक फंक्शन एक ही नाम को शेयर करता है। यहाँ प्रत्येक ओवरलोडेड फंक्शन अपने भिन्न प्रकार के डाटा टाइप एवं आर्गुमेंट की संख्या के आधार पर अद्वितीय होता है। फंक्शन ओवरलोडिंग द्वारा कम्पाइल टाइम / स्टेटिक पालीमोर्फिसम को प्रदर्शित किया जाता है।

Following steps are taken for the selection of any overloaded function:-
किसी ओवरलोडेड फंक्शन को चुनने के लिए निम्न चरणों का अनुसरण किया जाता है:- 

1. Firstly compiler selects exact match as per given actual arguments and bound it with the object.
सर्वप्रथम कम्पाइलर दिए गए आर्गुमेंट के आधार पर बिलकुल सही(सटीक) मैच चुनता है एवं उसे ऑब्जेक्ट के साथ जोड़ता है 
2. If exact match is not found then compiler selects function of bigger data type.
यदि सटीक मैच प्राप्त नहीं होता है तब कम्पाइलर बड़े डाटा टाइप वाले फंक्शन को चुनता है  
3. If both above matches are not found then compiler finds unique match by implicit conversion but if two or more matches are found then error occurred.
यदि उपरोक्त दोनों मैच प्राप्त नहीं होते है तब कम्पाइलर इम्प्लिसिट कन्वर्शन के द्वारा एक अद्वितीय मैच खोजता है यदि दो या अधिक मैच प्राप्त होते है तब एरर उत्पन्न होती है 

Rules for function overloading:-
फंक्शन ओवरलोडिंग के नियम:-
1. Overloaded function should have different number of arguments and data type from other function’s in family.
प्रत्येक ओवरलोडेड फंक्शन अपने भिन्न प्रकार के डाटा टाइप एवं आर्गुमेंट की संख्या के आधार पर फॅमिली अन्य फंक्शन से अलग होना चाहिए
2. Same function name should be used in function calling.
फंक्शन कालिंग में, एक ही फंक्शन नाम का प्रयोग किया जाना चाहिए
3. In static polymorphism function call should be resolved by best match technique.
स्टेटिक पालीमोर्फिसम में फंक्शन कॉल को बेस्ट मैच युक्ति के द्वारा हल किया जाना चाहिए 
 Examples:-

1.) C++ program for volume function overloading.
#include<iostream.h>
#include<conio.h>
class vol{
public:
int volume(int s){
return (s*s*s);
}
float volume(float l,float b,float h){
return (l*b*h);
}
float volume(float r,float h){
return (3.14*r*r*h);
}
};
void main(){
vol v;
clrscr();
cout<<"Volume of Cube = "<<v.volume(5)<<endl<<"Volume of Cuboid = "<<v.volume(3.3,4.7,5.8)<<endl<<"Volume of Cylinder = "<<v.volume(5.5,4.0)<<endl;
getch();
}

2.) C++ program for add function overloading.
#include<iostream.h>
#include<conio.h>
class addition{
public:
int add(int x){
return (x+x);
}
int add(char ch1,char ch2){
return (ch1+ch2);
}
float add(float l,float b,float h){
return (l+b+h);
}
};
void main(){
addition v;
clrscr();
cout<<"Addition of an Integer to itself= "<<v.add(5)<<endl;
cout<<"Addition of Two Character = "<<v.add('A','Z')<<endl<<"Addition of Three floats = "<<v.add(5.2,4. 5,6.5)<<endl;
getch();
}

3.) C++ program for area function overloading.
#include<iostream.h>
#include<conio.h>
class ar{
public:
float area(float s){
return (s*s);
}
float area(float l,float b){
return (l*b);
}
float area(float l,float b,float h){
return (2*(l*b+b*h+h*l));
}
};
void main(){
ar a;
clrscr();
cout<<"Area of Cube = "<<a.area(5)<<endl<<"Area of Cuboid = "<<a.area(3,4,5)<<endl<<"Area of Rectangle = "<<a.area(5,4)<<endl;
getch();
}

No comments:

Post a Comment