I) Overloading refers to the use of the same thing for different purposes. This means that we can use the same function name to create functions that perform a variety of different tasks. This is known as function polymorphism in OOPs.

II)The function would perform different operations depending on the argument list in the function call. The correct function to be invoked is determined by checking the number & type of the arguments but not on the function type.

III) Example-

// declarations ñ

    int add(int a, int b); //declaration1

    int add(int a, int b, int c); //declaration2

    double add (double x, double y); //declaration3

    double add (int p, double q); //declaration4

// function calls ñ

    cout << add (5, 10); // uses declaration1

    cout << add (5, 10.3); // uses declaration4

    cout << add (2, 3, 4); // uses declaration2

    cout << add (2.3, 5.4); // uses declaration3

IV) A function call first matches the prototype having the same number and type of arguments and then calls the appropriate function for execution. The function selection involves following steps. 

1. The compiler first tries to find an exact match in which the types of actual arguments are the same & use that function. 

2. If an exact match is not found, the compiler uses the integral promotions to the actual argument, such as. 

            char to int 

            float to double to find a match. 

3. When either of them fails, the compiler tries to use built in conversions to the actual arguments & then uses the functions whose match is unique. If the conversion is possible to have multiple matches, then the compiler will generate an error. 

Ex - long add(long n)
double add(double m)
A function call such as. 
will cause an error because int argument can be converted either long or double. 
4) For example - following program Illustrates function overloading. 
int volume(int);
double volume(double, int);
long volume(long, int, int);
cout <<volume(10) << "\n";
cout << volume (2.5, 8) << "\n".
cout << volume (100 L, 75, 15); 

int volume(int s) 

        return (s * s * s); 

double volume (double r, int h) 

        return (3.14 * r * r * h); 

long volume (long l, int b, int h) 

        return (l * b * h); 

The output of program 


i) The function assigns a default value to the parameter which does not have a matching argument in the function call. Default values are specified when the function is declared. The compiler looks at the prototype to see how many arguments a function uses & alerts the program for possible default values. 

ii) For example
    float amount (float principal, int period. float rate = 0.15); 
    declares a default value to argument rate A function call like 
        Value = amount (4000, 8); 
Passes the value of 4000 to principal and 8 to period and then lets the function use default value of 0.15 for rate. 
The call 
value = amount (3000, 5, 0, 12); 
passes an explicit value of 0.12 to rate. 

iii) A default argument is checked for type at the time of call. Only the trailing arguments can have default values. 

iv) We must add defaults from right to left we cannot provide a default value to a particular argument in the middle of an argument list. 

v) For example- 

# include 

        float amount; 
        float value(float p, int , float r = 0.15); 
        void printline (char ch = ë*í,int len = 40); 

float value(float p, int n, float r) 

        int year = 1; 
        float sum = p; 
        while(year <= n) 
                sum= sum * (1 + r);
                year = year + 1; 
void printline(char ch, int len) 

        for (int i =1 ; i < len; i++) 
                printf (ì%cî, ch); 

The output is - 


Final Value = 10056.7861


Overloading means assigning different meaning to an operation depending on the context. C++ permits overloading of operators, thus allowing us to assign multiple meanings to the operators. 


The input/output operators << and >> are good examples of operator overloading although the built in definition of the << operator is for shifting of bits. It is also used for displaying the values of various data types. 

We can overload all the C++ operators except the following, 

1. Class member access operators (., . *)

2. Scope resolution operator (: :) 

3. Size operator (sizeOof) 

4. Conditional operator (?:) 

Defining operator overloading 

i) To define an additional task to an operator, operator function is used. 

The general form is -

Return type classname :: operator op (anglist) 

            Function body //task defined 

Where return type is the type of value returned by the specified operation and op is the operator being overloaded. Operator op is the function name. 

ii) Operator functions must be either member functions or friend function. The difference is a friend function will have only one argument for unary operators and two for binary operators. While a member function has no arguments for unary operators and only one for binary operators. This is because the object used to invoke the member function is passed implicitly and therefore is available for the member function. This is not case with friend function, because arguments may be passed either by value or by reference.

iii) Example - 

    Vector operator ñ ( ) ; // unary minus 
    Vector operator + (vector); // vector addition 
    Friend vector operator + (vector, vector) // vector addition 
    Friend vector operator ñ (vector); // unary minus 
    Vector operator ñ (vector & a); // subtraction 
    Int operator == (vector); // comparision 
    Friend int operator == (vector,vector) //comparision 

iv) The process of overloading involves - 

  • Create a class that defines the data type that is to be used in the overloading operation. 
  • Declare the operator function operator op () in the public part of the class. It may be either a member or friend function. 
  • Define the operator function to implement the required operation. 

v) Overloaded operator functions can be invoked by expressions such as. 

        Op x or x op 

For unary operators and 

        x op y 

for binary operators. 
And op x would be interpreted as 
operator op (x) for friend functions and x op y would be interpreted as 
x.operator op (y) 
in member function and 
operator op (x,y) in friend function.


"function overloading in C++"

"example of function overloading"

"operator overloading in C++ example"

"advantages of function overloading in C++"

"what is function overloading in C++"

"function overriding"

"difference between function overloading and operator overloading in C++"

"operator overloading in C++"

"operator overloading in C++ example"

"unary operator overloading in C++"

"assignment operator overloading in C++"

"logical operator overloading in C++"

"operator overloading example"

"operator overloading in C++ using friend function"