Operate Overloading in C++ | What’s Operate Overloading in C++


function overloading in c

What’s Operate Overloading in C++?   

Two or extra capabilities can have the identical identify however completely different parameters; such capabilities are known as perform overloading in c++. The perform overloading within the c++ characteristic is used to enhance the readability of the code. It’s used in order that the programmer doesn’t have to recollect numerous perform names. If any class has a number of capabilities with completely different parameters having the identical identify, they’re stated to be overloaded. If we’ve got to carry out a single operation with completely different numbers or varieties of arguments, we have to overload the perform.   

If I say parameter checklist, it means the info kind and sequence of the parameters. For instance, the parameters checklist of a perform myfunction (int a, double b) is (int, double), which is completely different from the perform myfunction (double a, int b) parameter checklist (double, int). Operate overloading is a compile-time polymorphism. As we already know what a parameter checklist is, so let’s see the foundations of overloading: we are able to have the next capabilities in the identical scope.

sum(int a, int b)
sum(int a, int b, int c)

The simplest method to keep in mind this rule is that the parameters ought to qualify any a number of than one of many following circumstances: 

  • They need to have a special kind
  • They need to have a special quantity 
  • They need to have a special sequence of parameters.

To place it merely,

Operate overloading in c++ can have the identical identify however completely different parameters

  1. What’s Operate Overloading in c++?
  2. Operate Overloading in C++Instance
  3. Why is perform overloading in c++ is used?
  4. Guidelines of Operate Overloading in C++
  5. What’s the distinction between perform overloading and overriding in C++?
  6. Causes of perform overloading
  7. Benefits of Operate Overloading in c++
  8. Disadvantages of Operate Overloading in c++
  9. Distinction between Operate Overloading and Operator Overloading
  10. Distinction between Operate Overloading and Operate Overriding
  11. Operate overloading in C++ FAQs
  12. Conclusion

C++ has many options, and one of the vital options is perform overloading. It’s a code with multiple perform with the identical identify having numerous varieties of argument lists. This argument checklist contains the info kind of arguments and the sequence of the arguments.

Operate Overloading in C++ Instance

Instance 1:

#embody <iostream>
utilizing namespace std;

void SumNum(int A, int B);
void SumNum(int A, int B, int C);
void SumNum(int A, int B, int C, int D);

int primary()
{
    SumNum(1,2);
    SumNum(1,2,3);
    SumNum(1,2,3,4);
    
    return 0;
}

void SumNum(int A, int B)
{
     cout<< endl << "SUMNUM is : "<< A+B;     
}

void SumNum(int A, int B, int C)
{
     cout<< endl << "SUMNUM is : "<< A+B+C;  
}

void SumNum(int A, int B, int C, int D)
{
     cout<< endl << "SUMNUM is : "<< A+B+C+D;     
}
Output:
SumNum is 3
SumNum is 6
SumNum is 10

Instance 2:

#embody <iostream>
utilizing namespace std;
class Addition
 {
    public:
    int sum(int a,int b) 
    {
        return a+b;
    }
    int sum(int a,int b, int c) 
    {
       return a+b+c;
    }
 };
int primary(void)
{
    Addition obj;
    cout<<obj.sum(20, 15)<<endl;
    cout<<obj.sum(81, 100, 10);
    return 0;
}

Output :
35
191

Why is perform overloading in C++ is used?

Operate overloading is much like polymorphism that helps us to get completely different conduct, with the identical identify of the perform. Operate overloading in c++ is used for code reusability and to avoid wasting reminiscence.

Guidelines of Operate Overloading in C++

Totally different parameters or three completely different circumstances :

1.  These  capabilities have completely different parameter kind

sum(int a, int b)
sum(double a, double b)

2.   These capabilities have a special variety of parameters

sum(int a, int b)
sum(int a, int b, int c)

3.  These capabilities have a special sequence of parameters

sum(int a, double b)
sum(double a, int b)

           

The above three instances are legitimate instances of overloading. We are able to have any variety of capabilities, however keep in mind that the parameter checklist have to be completely different. For instance:

int mul(int, int)
double mul(int, int)

Because the parameter checklist is identical, this isn’t allowed. Though their return sorts are completely different, it’s not legitimate.

What’s the distinction between perform overloading and overriding in C++?

Operate Overloading Operate Overriding
Definition When two or extra strategies in a category have distinct parameters however the identical technique identify, this is named perform overloading. One technique is within the dad or mum class and the opposite is within the youngster class when a perform is overridden, however they’ve the identical parameters and technique identify.
Operate signature There ought to be a distinction within the quantity or type of parameters. The perform signature mustn’t change.
Behaviour defines a number of strategies’ behaviours. adjustments the way in which the process behaves.
Scope of Operate They belong to the identical class. They’ve a definite vary.
Inheritance Even with out inheritance, it may possibly occur. Solely when a category inherits from one other does it occur
Polymorphism Compile Time Run Time
Approach code enchancment technique. technique for changing code.

Operate overloading and ambiguity

The C++ programming language gives an overloading characteristic that allows an overload of two or extra strategies with the identical identify however distinct parameters with a view to create compile-time polymorphism. Operate and operator overloading can be utilized to hold it out. Whereas operator overloading overloads operators to offer user-defined knowledge sorts with explicit that means, perform overloading overloads two or extra capabilities with the identical identify however distinct parameters.

Utilizing built-in operators on user-defined sorts is made doable by this characteristic. Operator overloading makes the code simpler to know by redefining the performance to fulfill consumer wants. Each perform overloading and operator overloading shall be mentioned intimately in addition to how they’re utilized in C++ on this article.

Utilizing the Overloading thought, C++ allows the creation of versatile and comprehensible code. It allows including completely different performance to the prevailing code with a minimal variety of adjustments, therefore minimising the necessity for duplicate code. Virtually talking, C++ permits two fundamental varieties of overloading.

Operate Overloading, a characteristic of C++, permits us to create capabilities with the identical identify however completely different datatypes or numbers of arguments provided to them. Builders can outline capabilities with the identical identify inside the similar scope because of this functionality. The capabilities which have a reputation additionally share the identical behaviour, enabling compile-time polymorphism. One advantage of perform overloading is that it makes the code simpler to learn.

Using arguments can be utilized in any method to create perform overloading. Utilization of parameters may check with a specific parameter kind, a parameter rely, or a parameter sequence. Because of this, a perform outlined as calc (float x, int y) is completely different from calc (int x, float y), which has separate parameters with a special datatype.

Varieties of perform overloading in c++

There are two varieties of perform overloading in c++-

  • Compile time overloading– In compile time overloading, capabilities are overloaded utilizing completely different signatures. Signature of a perform contains its return kind, variety of parameters and varieties of parameters.
  • Runtime overloading -In runtime overloading, capabilities are overloaded utilizing completely different variety of parameters.

Causes of perform overloading in c++

  • Kind Conversion.
  • Operate with default arguments.
  • Operate with a go by reference.

1. Kind Conversion

Program :

#embody<iostream>
utilizing namespace std;
void perform(float);
void perform(int);
void perform(float x)
{
std::cout << "Worth of x is : " <<x<< std::endl;
}
void perform(int y)
{
std::cout << "Worth of y is : " <<y<< std::endl;
}
int primary()
{
perform(3.4);
perform(34);
return 0;
}

Output :

output

This instance exhibits an error “name of overloaded ‘perform’ is ambiguous”. Based on our prediction, perform (3.4) will name the primary perform, and performance (34) will name the second perform. However this isn’t what occurs as a result of, in C++, all of the floating-point constants will not be handled as float; as an alternative, they’re handled as double.  If we exchange the float variable with a double variable, this system will work correctly. Thus we name it a kind conversion error from float to double.

2. Operate with Default Arguments

Program :

#embody<iostream>
utilizing namespace std;
void perform(int);
void perform(int,int);

void perform(int x)
{
std::cout << "Worth of x is : " <<x<< std::endl;
}
void perform(int y,int z=12)
{
std::cout << "Worth of y is : " <<y<< std::endl;
std::cout << "Worth of z is : " <<z<< std::endl;
}
int primary()
{
perform(12);
return 0;
}

Output :

output

The above instance exhibits an error saying “name of overloaded ‘enjoyable(int)’ is ambiguous”, it’s because perform(int y, int z=12) might be known as in two methods:

By calling the perform with just one argument (and it’ll mechanically take the worth of z = 12)

By calling the perform with solely two arguments.

Once you name the perform: perform(12), we full fill the situation of each perform(int) and performance(int, int); due to this fact, the compiler will get into an ambiguity that offers an error.

3. Operate with a Go by Reference

Program :

#embody <iostream>
utilizing namespace std;
void perform(int);
void perform(int &);
void perform(int a)
{
std::cout << "Worth of a is : " <<a<< std::endl;
}
void perform(int &b)
{
std::cout << "Worth of b is : " <<b<< std::endl;
}

int primary()
{
int x=10;
perform(x);
return 0;
}

Output :

output

The above program exhibits an error saying “name of overloaded ‘enjoyable(int&)’ is ambiguous”. As we see, the primary perform takes one integer argument, and the second perform takes a reference parameter as an argument. On this case, the compiler can’t perceive which perform is required by the consumer as there isn’t any syntactic distinction between the enjoyable(int) and enjoyable(int &); due to this fact, it shoots an error of ambiguity.

Overloading utilizing various kinds of parameters

#embody <iostream>
utilizing namespace std;

void printValue(int   A);
void printValue(char  A);
void printValue(float A);

int primary()
{
    printValue(10     );
    printValue('@'    );
    printValue(3.14f  );

    return 0;
}

void printValue(int   A)
{
     cout<< endl << "Worth of A : "<< A;     
}

void printValue(char  A)
{
     cout<< endl << "Worth of A : "<< A;     
}

void printValue(float A)
{
     cout<< endl << "Worth of A : "<< A;     
}
Output:
Worth of A : 10
Worth of A : @
Worth of A : 3.14

Operate overloading might be carried out primarily based on the numerous varieties of arguments which might be handed into the perform. Each non-member capabilities and member capabilities of a category might implement perform overloading.

Operate overloading by altering the variety of arguments

On the idea of the amount of parameters provided into the perform, we are able to implement perform overloading. Each non-member capabilities and member capabilities of a category might implement perform overloading.

#embody <iostream>
utilizing namespace std;

void SumNum(int A, int B);
void SumNum(int A, int B, int C);
void SumNum(int A, int B, int C, int D);

int primary()
{
    SumNum(1,2);
    SumNum(1,2,3);
    SumNum(1,2,3,4);
    
    return 0;
}

void SumNum(int A, int B)
{
     cout<< endl << "SUMNUM is : "<< A+B;     
}

void SumNum(int A, int B, int C)
{
     cout<< endl << "SUMNUM is : "<< A+B+C;     
}

void SumNum(int A, int B, int C, int D)
{
     cout<< endl << "SUMNUM is : "<< A+B+C+D;     
}
Output:
SumNum is 3
SumNum is 6
SumNum is 10

On this method of perform overloading, we outline two capabilities of the identical kind however with a special variety of parameters with the identical names. For instance, in this system given under, we’ve got made two add() capabilities to return the sum of two and three integers.

// first perform definition
int add(int a, int b)
{
cout << a+b;
}
// second overloaded perform definition
int add(int a, int b, int c)
{
cout << a+b+c;
}

Right here add() perform is claimed to be overloaded, because it has two definitions, one that may settle for two arguments and one other which might settle for three arguments. Which add() perform shall be known as, relying on the variety of arguments.

int primary() 
{ 
add(10, 20); // add() with 2 parameter     
add(10, 20, 30); //sum() with 3 parameter 
}

Program ( By altering the variety of arguments):

#embody <iostream>
utilizing namespace std;
int add(int a, int b)
{
cout << a+b <<endl;
return 0;
}
int add(int a, int b, int c)
{
cout << a+b+c <<endl;
return 0;
}
int primary()
{
add(20, 40);
add(40, 20, 30);
}
Output:
60 
90

Within the above instance, we overload an add() perform by altering the variety of arguments. First, we outline an add() perform with the 2 parameters, after which we are able to overload it by once more defining an add() perform however this time with three parameters.

Operate overloading by various kinds of arguments

On this technique, we outline two or extra capabilities with the parameters of various knowledge sorts however with the identical variety of parameters with the identical identify. For instance, on this program, we’ve got three add() capabilities; the primary one will get two integer arguments, the second will get two float arguments, and the third one will get two double arguments.

Program :

#embody <iostream>
utilizing namespace std;
int add(int x, int y) // first definition
{
cout<< x+y << endl;
return 0;
}
float add(float a, float b)
{
cout << a+b << endl;
return 0;
}
double add(double x, double y)
{
cout << x+y << endl;
return 0;
}
int primary()
{
add(20, 40);
add(23.45f, 34.5f);
add(40.24, 20.433);
}
Output :
60
57.95
60.673

Within the above instance, we outline an add() perform 3 times. First, utilizing integers as parameters, secondly, utilizing float as parameters, and third utilizing double as a parameter.

Due to this fact we override the add() perform twice.

Benefits of Operate Overloading in c++

  • This system could be very easy and in addition simpler to know.
  • It saves the reminiscence house, maintains consistency, makes clear interface for strategies whatever the parameter’s kind and readability of this system.
  • Utilizing the perform overloading idea, we are able to develop multiple perform with the identical identify, however the arguments handed ought to be of various sorts.
  • Operate overloading executes this system quicker.
  • Operate overloading is used for code reusability and to avoid wasting reminiscence.

To grasp perform overloading in C++ clearly, you have to be well-versed in C++. In case you want to improve your abilities, you possibly can take up the Introduction to C++ free course at Nice Studying Academy and study the essential ideas and fundamentals of C++ that will help you construct experience within the topic. 

Disadvantages of Operate Overloading in c++

  • Operate declarations that differ by their return kind can’t be overloaded with the perform overloading course of.
  • If any static member perform is asserted, then the identical parameters or the identical identify sorts can’t be overloaded.

Distinction between Operate Overloading and Operator Overloading

Operate Overloading Operator Overloading
Operate overloading permits us to name it in a number of methods. Operator overloading permits operators to have their extending that means past its predefined operational that means.
You may overload the perform with the identical identify however with completely different parameters. You may overload (outline customized behaviour) for operators resembling ‘+’, ‘-‘, ‘()’, ‘[]’.
Operate overloading means utilizing a single identify and giving extra performance to it. Operator overloading means including additional performance for a sure operator.
When an operator is overloaded, the operator has completely different meanings, which rely on the kind of its operands. When a perform is overloaded, the identical perform identify has completely different interpretations relying on its signature, which is the checklist of argument sorts within the capabilities parameter checklist.

If you wish to study extra about operator overloading go to Operator Overloading in C++.

Distinction between Operate Overloading and Operate Overriding

  Operate Overloading Operate Overriding
Definition In perform overloading, two or extra strategies in a single class have completely different parameters however the identical technique identify. In perform overriding, two strategies have the identical parameters, and technique identify, however one technique is within the dad or mum class and the opposite is within the youngster class.
Operate Signature Both kind of parameters or the variety of parameters ought to differ. Operate signature ought to stay the identical.
Habits Defines a number of behaviors to a technique. Modifications the conduct of the tactic.
Scope of Operate They’re in the identical scope. They’re in numerous scope.
Inheritance It could happen with out inheritance. It happens solely when one class is inherited from one other class.
Polymorphism Compile Time Run Time
Approach Code refinement method Code substitute method
No. of Lessons Just one class is required. Minimal two courses are required.

Operate overloading in C++ FAQs

What’s perform overloading in C++?

Operate overloading refers to when two or extra capabilities with the identical identify however distinct parameters exist.
Operate overloading is without doubt one of the most vital traits of C++, amongst its many different options. There are a lot of capabilities with the identical identify on this code, every with a singular set of argument lists. The info kind and ordering of the arguments are additionally included on this argument checklist.
The C++ perform overloading characteristic is used to make the code simpler to learn. It’s used to avoid wasting the programmer from having to memorise completely different perform names. Overloaded capabilities are people who belong to a category however have multiple occasion with the identical identify however completely different parameters. The perform have to be overloaded if a single operation have to be carried out with numerous numbers or varieties of parameters.
Operate overloading is known as a perform of polymorphism in OOP.

What’s perform overloading clarify?

Much like polymorphism, perform overloading allows us to acquire completely different behaviour whereas sustaining the perform’s identify. In C++, perform overloading is used to scale back reminiscence utilization and enhance code reuse.

What’s runtime polymorphism C++?

Late binding and dynamic polymorphism are different names for runtime polymorphism. The perform name is resolved at runtime in runtime polymorphism. In distinction, with compile time or static polymorphism, the compiler determines which perform name to bind to the item after deducing it at runtime.

What’s inheritance in C++?

Through the use of inheritance, present courses might be prolonged and reused with out being modified, creating hierarchical hyperlinks between them. A category might be embedded into an object through inheritance. Let’s say you embody a category A object of kind x within the class specification of kind B.

What are some great benefits of perform overloading?

The basic advantage of perform overloading is that it makes code simpler to learn and reuse.
Operate overloading is used to enhance consistency, readability, and reminiscence effectivity.
This system’s execution is accelerated by it.
Moreover, code maintenance turns into easy.
Operate overloading offers code flexibility.
The perform’s versatility avoids the necessity for a lot of perform names to conduct the identical set of capabilities.

Conclusion

Operate overloading in C++ can be utilized in numerous methods to boost code readability. Whereas doing programming in C++, it helps to avoid wasting compile time and in addition saves reminiscence house. You additionally realized how related operations are carried out utilizing the perform overloading idea.

You may improve your perform overloading in c++ ideas right here:

Leave a Reply