C++ 20: Function Overloading

Function Overloading
    ☛ Defining a function in a program with same name but different number of 
        arguments or different datatypes of argument is called as function overloading
    ☛ In such case an appropriate function is called as per the value passed while 
        calling the function
Ex 
#include <iostream>
using namespace std;
main()
{
    
    return 0;
}void display(int n)
{
    cout << "Value of n is " << n << endl;
}
void display(double n)
{
    cout << "Value of n is " << n << endl;
}
void display(char n[])
{
    cout << "Value of n[] is " << n << endl;
}
int main()
{
    display(10);
    display(99.45);
    display("ayush");
    return 0;
}

Output:

Value of n is 10
Value of n is 99.45
Value of n[] is ayush


WAP to overload add function which performs addition:

#include <iostream>
using namespace std;
int add(int a, int b, int c, int d)
{
    return (a + b + c + d);
}
int add(int a, int b, int c)
{
    return (a + b + c);
}
int add(int a, int b)
{
    return (a + b);
}

main()
{

    int x = add(20304010);
    cout << "20+30+40+10 = x = " << x << endl;

    int y = add(506070);
    cout << "50+60+70 = y  =  " << y << endl;
    return 0;
}

Output:

20+30+40+10 = x = 100
50+60+70 = y  =  180


Function Prototype:

1)If a function call before its definition then we need to define the
 prototype of that unction before its call
2)Syntax of prototype
Syntax- return_type function_name(datatype_of _arg1,data_tye_of_arg2,...);

Example:

#include <iostream>
using namespace std;
int add(int,int,int= 0,int= 0);//func prototype
main(){

    int a;

    a = add(2,4);
    cout<<"Addition is : "<<a<<endl;
}

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

Output:

Enter your age: 56
You can vote


Function with Default Argument

If a function contains default value then in some cases it avoids 

the need to overload the function resulting to reduce the length 
of program the function with default arguments can be call by passing 
different number of arguments.
Note: Default value must be specified with right to left value Function with default
 arguments.

#include <iostream>
using namespace std;
int add(int a, int b, int c = 0int d = 0)
{
    return (a + b + c + d);
}
int main()
{
    int x = add(10203040);
    cout << "Value of x is " << x << endl;

    int y = add(102030);
    cout << "Value of x is " << y << endl;

    int z = add(1020);
    cout << "VAlue of z is " << z << endl;
}

Output:

Value of x is 100
Value of x is 60
VAlue of z is 30

Function with default Arguments:
#include <iostream>
using namespace std;
product(int a,int b,int c=1,int d=1){
    return(a*b*c*d);
}

int main()
{
    int x,y,z;

    x = product(1,2,3,4);
    cout<<"Product is : x : "<<x<<endl;

    y = product(1,2,3);
    cout<<"Product is : x : "<<y<<endl;

    z = product(1,2);
    cout<<"Product is : x : "<<z<<endl;
}

Output:

Product is : x : 24
Product is : x : 6
Product is : x : 2


Comments

Popular posts from this blog

C++ 38: Visibility Modes Public, Private and Protected