🔵 Core C++  ·  Lesson 30

Function Overloading

What is function overloading?

Function overloading lets you give several functions the same name, as long as their parameter lists differ — a different number of parameters, or different types. C++ figures out which one you meant from the arguments you pass.

Why overload?

Without overloading you would need clumsy names for the same idea: addInts, addDoubles, addThree. Overloading lets one clear name — add — serve them all, so callers do not have to remember variants.

A working example

C++
#include <iostream>
using namespace std;

int add(int a, int b)          { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c)   { return a + b + c; }

int main() {
    cout << add(2, 3) << "\n";        // int version
    cout << add(2.5, 3.5) << "\n";    // double version
    cout << add(1, 2, 3) << "\n";     // three-arg version
    return 0;
}
Output:
5
6
6

How the compiler chooses

The compiler matches your call to the version whose parameters best fit the argument types and count — a process called overload resolution. In the example, add(2, 3) matches the two-int version, add(2.5, 3.5) matches the two-double version, and so on.

What counts as different

DifferenceValid overload?
Different number of parametersYes
Different parameter typesYes
Different order of typesYes (e.g. (int,double) vs (double,int))
Only different return typeNo — not allowed
Only different parameter namesNo — names do not count

Common mistakes

  • Trying to overload by return type only — not allowed.
  • Writing two versions that are ambiguous, so no single best match exists.
  • Thinking different parameter names make an overload — only types/count matter.
  • Confusing overloading (compile-time) with overriding (run-time, via virtual functions).
🏋️ Practice

Write an overloaded area function: one for a square (one side), one for a rectangle (length and width), and one for a circle (radius, returning a double). Call each from main.

Summary

  • Overloading = several functions with the same name, different parameters.
  • The compiler picks the version that best matches your arguments.
  • Overloads must differ in parameter count, types or order.
  • You cannot overload by return type or parameter names alone.
  • Overloading is compile-time; overriding is a separate run-time concept.

Function overloading क्या है?

Function overloading आपको कई functions को एक ही नाम देने देता है, बशर्ते उनकी parameter सूचियाँ भिन्न हों — अलग संख्या में parameters, या अलग types। C++ आपके भेजे arguments से पता कर लेता है कि आपका मतलब कौन-सा था।

Overload क्यों करें?

Overloading के बिना आपको एक ही विचार के लिए भद्दे नाम चाहिए: addInts, addDoubles, addThree। Overloading एक साफ़ नाम — add — को इन सबकी सेवा करने देता है, तो callers को variants याद नहीं रखने पड़ते।

एक चलता उदाहरण

C++
#include <iostream>
using namespace std;

int add(int a, int b)          { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c)   { return a + b + c; }

int main() {
    cout << add(2, 3) << "\n";        // int version
    cout << add(2.5, 3.5) << "\n";    // double version
    cout << add(1, 2, 3) << "\n";     // teen-arg version
    return 0;
}
Output:
5
6
6

Compiler कैसे चुनता है

Compiler आपके call को उस version से मिलाता है जिसके parameters argument types और संख्या से सबसे अच्छे फिट हों — इस प्रक्रिया को overload resolution कहते हैं। उदाहरण में, add(2, 3) दो-int version से मेल खाता है, add(2.5, 3.5) दो-double version से, और ऐसे ही।

अलग क्या माना जाता है

अंतरमान्य overload?
Parameters की अलग संख्याहाँ
अलग parameter typesहाँ
Types का अलग क्रमहाँ (जैसे (int,double) बनाम (double,int))
केवल अलग return typeनहीं — अनुमति नहीं
केवल अलग parameter नामनहीं — नाम मायने नहीं रखते

आम गलतियाँ

  • केवल return type से overload करने की कोशिश — अनुमति नहीं।
  • दो ऐसे versions लिखना जो ambiguous हों, तो कोई एकल सर्वोत्तम मेल न हो।
  • यह सोचना कि अलग parameter नाम overload बनाते हैं — केवल types/संख्या मायने रखते हैं।
  • Overloading (compile-time) को overriding (run-time, virtual functions से) से भ्रमित करना।
🏋️ अभ्यास

एक overloaded area function लिखें: एक square के लिए (एक भुजा), एक rectangle के लिए (लंबाई और चौड़ाई), और एक circle के लिए (त्रिज्या, double लौटाते हुए)। हर एक को main से call करें।

सारांश

  • Overloading = एक ही नाम वाले कई functions, अलग parameters।
  • Compiler वह version चुनता है जो आपके arguments से सबसे अच्छा मेल खाए।
  • Overloads को parameter संख्या, types या क्रम में भिन्न होना चाहिए।
  • आप केवल return type या parameter नामों से overload नहीं कर सकते।
  • Overloading compile-time है; overriding एक अलग run-time अवधारणा है।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में function overloading क्या है?
Function overloading का मतलब है एक ही नाम पर कई functions define करना पर अलग parameter सूचियों के साथ — अलग संख्या में parameters या अलग types। Compiler आपके भेजे arguments के आधार पर सही version चुनता है, तो एक नाम कई तरह के input संभाल सकता है।
Function overloading क्यों उपयोगी है?
यह संबंधित संक्रियाओं को addInt, addDouble और addThree जैसे नाम गढ़ने के बजाय एक सहज नाम साझा करने देता है। इससे code साफ़ और पढ़ने में आसान होता है, क्योंकि add(...) काम करता है चाहे आप दो ints, दो doubles या तीन numbers भेजें।
Compiler कैसे तय करता है कि कौन-सा overloaded function call करना है?
Compiler आपके call में arguments की संख्या और types देखता है और उन्हें सबसे उपयुक्त version से मिलाता है। इसे overload resolution कहते हैं। अगर कोई version फिट न हो, या दो समान रूप से फिट हों, compiler error देता है।
क्या C++ में functions को केवल return type से overload किया जा सकता है?
नहीं। दो functions केवल return type से भिन्न नहीं हो सकते — parameter सूचियाँ भिन्न होनी चाहिए। चूँकि caller को return value इस्तेमाल करना ज़रूरी नहीं, compiler versions में फ़र्क नहीं बता पाता, तो C++ केवल return type पर overloading की अनुमति नहीं देता।
C++ में overloading और overriding में क्या अंतर है?
Overloading में अलग parameters वाले कई समान-नाम functions होते हैं, compile time पर हल होते हैं। Overriding में derived class में base class के virtual function को उसी signature से फिर से define करना है, run time पर polymorphism के ज़रिए हल होता है। समान लगने के बावजूद ये अलग तंत्र हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.