Function Overloading in C++

If any class have multiple functions with same names but dissimilar parameters then they are supposed to be overloaded. Function overloading lets you to use the same name for different functions, to do, either same or else different functions in the same class.

Function overloading is generally used to improve the readability of the program. If you have to perform one single operation but with different number or elseforms of arguments, then you can only overload the function.

In C++ programming, two functions can have same name if number and/or kind of arguments passed are unalike.

These functions having different number or type of parameters are identified as overloaded functions.

Here is an Example:

int test() { }
int test(int a) { }
float test(double a) { }
int test(int a, double b) { }

Here, all 4 functions are overloaded functions because argument(s) passed to these functions are different.
Notice that, the return type of all these 4 functions are not same. Overloaded functions could havedissimilar return type but it should have different argument(s).

// Error code
int test(int a) { }
double test(int b){ }

The number and type of arguments delivered to these two functions are similar even though the return type is different. Henceforth, the compiler will throw error.

Static Polymorphism 

Static polymorphism raises to an entity present in different physical forms concurrently. Static polymorphism includes binding of functions based on the number, type, in addition to sequence of arguments. The several types of parameters are indicated in the function declaration. the function can be callED at compile time.

 Function Overloading (Static polymorphism)

  • The determination of a function call is founded on number, type, also sequence of arguments stated for each form of the function.
  • Consider the subsequent function declaration:

void add(int , int);

void add(float, float);

When the add() function is invoked, the parameters passed to it will fix which version of the function will be achieved. This resolution is done at compile time.Which method is to be named is absolute at compile-time only.

 Function Overriding (Dynamic polymorphism)

  • Dynamic polymorphismmentions to an entity changing its form depending on the conditions. A function is supposed to show dynamic polymorphism when it occurs in more than one form.also It calls to its numerous forms are determined dynamically when the program is executed.
  • Run time Polymorphism also recognized as method overriding. In this Mechanism by which a call to an overridden function is set at a Run-Time if a base Class covers a method that is overridden.

Here memory allocation will be at the run time for that specific method.

Friend function

  • If a function is well-defined as a friend function then, the private and protected data of a class can be retrieved using the function.
  • The complier distinguishes a given function is a friend function via the use of the keyword friend.
  • For to gain access to the data, the declaration of a friend function should be through inside the body of the classbeginning with keyword friend.

Declaration :

class class_name
    ... .. ...
    friend return_type function_name(argument/s);
    ... .. ...

Here, you can describe the friend function as a normal function to access the data of the class. No friend keyword is in the definition.

class className
    ... .. ...
    friend return_type functionName(argument/s);
    ... .. ...

return_type functionName(argument/s)
    ... .. ...
    // Private and protected data of className can be accessed from
    // this function because it is a friend function of className.
    ... .. ...

Example 1: Working of friend Function


Here, friend function addFive() is stated inside Distance class. Thus, the private data meter can be retrieved from this function.

Inline function

  • Inline function is one of the important feature of C++. When the program implements the function call instruction the CPU stocks the memory address of the instruction following the function call, copies the arguments of the function on the stack and lastly transfers control to the definite function.
  • The CPU then executes the function code, supplies the function return value in a predefined memory location/register besides returns control to the calling function.
  • For functions that are large and/or do complex tasks, the overhead of the function call is generally insignificant related to the amount of time the function takes to run.
  • Though, for small, commonly-used functions, the time desirable to make the function call is often a lot more than the time required to really execute the function’s code.
  • When the inline function is baptizedentire code of the inline function gets inserted or else substituted at the point of inline function call.

syntax :

inline return-type function-name(parameters)
    // function code

  • inlining is simply a request to the compiler, not a command. Compiler can overlook the request for inlining.
  • It protects the overhead of push/pop variables on the stack when function is called.
  • It likewise saves overhead of a return call from a function.
  • When you inline a function, you may allow compiler to make context precise optimization on the body of function. Next program proves the use  inline function.



Leave A Comment

Your email address will not be published. Required fields are marked *