Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
Blog · C++ · 04 Jul 2026 · Hindi + English

C++ में Function Overloading Examples और Rules के साथ

Function overloading से same नाम लेकिन अलग parameters वाले कई functions लिख सकते हैं — compiler सही वाला चुनता है. Examples, rules और return-type trap.

The receptionist analogy

Think of a school receptionist named "enquiry". A parent asks about fees — she answers about fees. A student asks about the timetable — she answers about the timetable. Same name, different response depending on what you bring to her. Function overloading is exactly this: several functions share one name, and the compiler picks the right one by looking at the arguments you pass.

Basic example — one name, three jobs

#include <iostream>
using namespace std;

int add(int a, int b)            { return a + b; }
int add(int a, int b, int c)     { return a + b + c; }     // more params
double add(double a, double b)   { return a + b; }          // diff types

int main() {
    cout << add(2, 3)        << endl;   // matches (int, int)
    cout << add(2, 3, 4)     << endl;   // matches (int, int, int)
    cout << add(2.5, 3.5)    << endl;   // matches (double, double)
    return 0;
}
5 9 6.0

The decision happens at compile time: the compiler reads the arguments, finds the matching version, and hard-wires the call. That is why overloading is called compile-time (static) polymorphism.

What makes two functions "different enough"?

ChangeValid overload?Example
Number of parameters✅ Yesadd(a,b) vs add(a,b,c)
Type of parameters✅ Yesadd(int,int) vs add(double,double)
Order of types✅ Yesf(int,char) vs f(char,int)
Only return type❌ NO — compile errorint f(int) vs double f(int)
Only parameter names❌ NO — same functionf(int a) vs f(int b)
The return-type trap: why can't return type alone distinguish? Because the compiler decides by the call — and a call like f(5); (result ignored) gives no clue which return type was wanted. Ambiguity = forbidden. This "why" is the follow-up interviewers love.

The ambiguity error you will definitely meet

void show(int x)    { cout << "int\n"; }
void show(float x)  { cout << "float\n"; }

show(5);       // OK: exactly int
show(5.5);     // ERROR on many compilers: 5.5 is a DOUBLE literal -
               // converting to int or to float are equally good matches!
show(5.5f);    // OK: the f suffix makes it exactly float

Fix: use exact-type literals (5.5f) or add a double overload. Understanding this error message once saves hours later.

Real-world use: constructors and print functions

class Student {
public:
    Student()                       { }              // no info yet
    Student(string n)               { }              // name only
    Student(string n, int marks)    { }              // full info
};   // constructor overloading: create objects in whichever way suits

void print(int n)      { cout << "Number: "  << n; }
void print(string s)   { cout << "Text: "    << s; }
void print(bool b)     { cout << (b ? "Yes" : "No"); }

This is why cout itself can print an int, a string, or a double with the same << — the entire iostream library is built on overloading.

Receptionist वाली analogy

School की receptionist को सोचिए जिसका नाम है "enquiry". Parent fees पूछता है — वह fees बताती है. Student timetable पूछता है — वह timetable बताती है. Same नाम, अलग जवाब — इस पर depend कि आप क्या लेकर आए. Function overloading बिल्कुल यही है: कई functions एक नाम share करते हैं, और compiler आपके pass किए arguments देखकर सही वाला चुनता है.

Basic example — एक नाम, तीन काम

#include <iostream>
using namespace std;

int add(int a, int b)            { return a + b; }
int add(int a, int b, int c)     { return a + b + c; }     // ज़्यादा params
double add(double a, double b)   { return a + b; }          // अलग types

int main() {
    cout << add(2, 3)        << endl;   // (int, int) से match
    cout << add(2, 3, 4)     << endl;   // (int, int, int) से match
    cout << add(2.5, 3.5)    << endl;   // (double, double) से match
    return 0;
}
5 9 6.0

Decision compile time पर होता है: compiler arguments पढ़ता है, matching version ढूंढता है, और call fix कर देता है. इसीलिए overloading को compile-time (static) polymorphism कहते हैं.

दो functions "काफी अलग" कब माने जाते हैं?

बदलावValid overload?Example
Parameters की संख्या✅ हांadd(a,b) vs add(a,b,c)
Parameters के types✅ हांadd(int,int) vs add(double,double)
Types का order✅ हांf(int,char) vs f(char,int)
सिर्फ return type❌ नहीं — compile errorint f(int) vs double f(int)
सिर्फ parameter names❌ नहीं — same functionf(int a) vs f(int b)
Return-type trap: सिर्फ return type से अलग क्यों नहीं हो सकते? क्योंकि compiler call से decide करता है — और f(5); जैसी call (result ignore) से कोई clue नहीं मिलता कि कौन-सा return type चाहिए था. Ambiguity = मना. यही "क्यों" वाला follow-up interviewers को पसंद है.

वह ambiguity error जो आपको ज़रूर मिलेगा

void show(int x)    { cout << "int\n"; }
void show(float x)  { cout << "float\n"; }

show(5);       // OK: exactly int
show(5.5);     // कई compilers पर ERROR: 5.5 DOUBLE literal है -
               // int में बदलना या float में - दोनों बराबर अच्छे matches!
show(5.5f);    // OK: f suffix इसे exactly float बना देता है

Fix: exact-type literals use करें (5.5f) या double overload जोड़ें. यह error एक बार समझ ली तो बाद में घंटों बचेंगे.

Real-world use: constructors और print functions

class Student {
public:
    Student()                       { }              // अभी कोई info नहीं
    Student(string n)               { }              // सिर्फ name
    Student(string n, int marks)    { }              // पूरी info
};   // constructor overloading: जिस तरह सुविधाजनक हो object बनाओ

void print(int n)      { cout << "Number: "  << n; }
void print(string s)   { cout << "Text: "    << s; }
void print(bool b)     { cout << (b ? "Yes" : "No"); }

इसीलिए cout खुद int, string या double को same << से print कर पाता है — पूरी iostream library overloading पर ही बनी है.

Frequently Asked Questions

C++ में function overloading क्या है?

Function overloading से कई functions अलग parameter lists के साथ same नाम share करते हैं; compiler arguments देखकर compile time पर सही version चुनता है — यही compile-time polymorphism है.

क्या सिर्फ return type से functions overload हो सकते हैं?

नहीं, यह compile error है — compiler calls को सिर्फ arguments से resolve करता है, और result ignore करने वाली call से पता ही नहीं चलता कौन-सा return type चाहिए था.

Function overloading और overriding में क्या अंतर है?

Overloading same scope में अलग parameters के साथ same नाम है, compile time पर resolve; overriding derived class द्वारा same signature से virtual function redefine करना है, runtime पर resolve.

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें