Sunday, October 2, 2022
HomeArtificial IntelligencePerform Overloading in C++ | What's Perform Overloading in C++

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


function overloading in c

What’s Perform Overloading in C++?   

Two or extra features can have the identical title however completely different parameters; such features are known as operate overloading in c++. The operate 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 operate names. If any class has a number of features with completely different parameters having the identical title, they’re mentioned to be overloaded. If we now have to carry out a single operation with completely different numbers or sorts of arguments, we have to overload the operate.   

If I say parameter checklist, it means the information sort and sequence of the parameters. For instance, the parameters checklist of a operate myfunction (int a, double b) is (int, double), which is completely different from the operate myfunction (double a, int b) parameter checklist (double, int). Perform overloading is a compile-time polymorphism. As we already know what a parameter checklist is, so let’s see the principles of overloading: we will have the next features in the identical scope.

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

The best option to bear in mind this rule is that the parameters ought to qualify any a number of than one of many following situations: 

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

To place it merely,

Perform overloading in c++ can have the identical title however completely different parameters

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

C++ has many options, and one of the vital vital options is operate overloading. It’s a code with multiple operate with the identical title having numerous sorts of argument lists. This argument checklist consists of the information sort of arguments and the sequence of the arguments.

Perform 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 essential()
{
    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 essential(void)
{
    Addition obj;
    cout<<obj.sum(20, 15)<<endl;
    cout<<obj.sum(81, 100, 10);
    return 0;
}

Output :
35
191

Why is operate overloading in C++ is used?

Perform overloading is just like polymorphism that helps us to get completely different habits, with the identical title of the operate. Perform overloading in c++ is used for code reusability and to avoid wasting reminiscence.

Guidelines of Perform Overloading in C++

Totally different parameters or three completely different situations :

1.  These  features have completely different parameter sort

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

2.   These features have a special variety of parameters

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

3.  These features 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 will have any variety of features, however do not forget that the parameter checklist should 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 varieties are completely different, it’s not legitimate.

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

Perform Overloading Perform Overriding
Definition When two or extra strategies in a category have distinct parameters however the identical methodology title, this is called operate overloading. One methodology is within the mother or father class and the opposite is within the baby class when a operate is overridden, however they’ve the identical parameters and methodology title.
Perform signature There must be a distinction within the quantity or sort of parameters. The operate signature mustn’t change.
Behaviour defines a number of strategies’ behaviours. adjustments the way in which the process behaves.
Scope of Perform They belong to the identical class. They’ve a definite vary.
Inheritance Even with out inheritance, it will probably occur. Solely when a category inherits from one other does it occur
Polymorphism Compile Time Run Time
Approach code enchancment methodology. methodology for changing code.

Perform overloading and ambiguity

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

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

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

Perform Overloading, a characteristic of C++, permits us to create features with the identical title however completely different datatypes or numbers of arguments equipped to them. Builders can outline features with the identical title inside the identical scope due to this functionality. The features which have a reputation additionally share the identical behaviour, enabling compile-time polymorphism. One good thing about operate overloading is that it makes the code simpler to learn.

Using arguments can be utilized in any option to create operate overloading. Utilization of parameters would possibly confer with a selected parameter sort, a parameter depend, or a parameter sequence. Consequently, a operate outlined as calc (float x, int y) is completely different from calc (int x, float y), which has separate parameters with a special datatype.

Kinds of operate overloading in c++

There are two sorts of operate overloading in c++-

  • Compile time overloading– In compile time overloading, features are overloaded utilizing completely different signatures. Signature of a operate consists of its return sort, variety of parameters and sorts of parameters.
  • Runtime overloading -In runtime overloading, features are overloaded utilizing completely different variety of parameters.

Causes of operate overloading in c++

  • Kind Conversion.
  • Perform with default arguments.
  • Perform with a move by reference.

1. Kind Conversion

Program :

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

Output :

output

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

2. Perform with Default Arguments

Program :

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

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

Output :

output

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

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

By calling the operate with solely two arguments.

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

3. Perform with a Go by Reference

Program :

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

int essential()
{
int x=10;
operate(x);
return 0;
}

Output :

output

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

Overloading utilizing several types of parameters

#embody <iostream>
utilizing namespace std;

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

int essential()
{
    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

Perform overloading might be carried out based mostly on the various sorts of arguments which are handed into the operate. Each non-member features and member features of a category might implement operate overloading.

Perform overloading by altering the variety of arguments

On the premise of the amount of parameters equipped into the operate, we will implement operate overloading. Each non-member features and member features of a category might implement operate 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 essential()
{
    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 approach of operate overloading, we outline two features of the identical sort however with a special variety of parameters with the identical names. For instance, in this system given under, we now have made two add() features to return the sum of two and three integers.

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

Right here add() operate 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() operate might be known as, relying on the variety of arguments.

int essential() 
{ 
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 essential()
{
add(20, 40);
add(40, 20, 30);
}
Output:
60 
90

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

Perform overloading by several types of arguments

On this methodology, we outline two or extra features with the parameters of various knowledge varieties however with the identical variety of parameters with the identical title. For instance, on this program, we now have three add() features; 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 essential()
{
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() operate thrice. 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() operate twice.

Benefits of Perform Overloading in c++

  • This system could be very easy and in addition simpler to know.
  • It saves the reminiscence area, maintains consistency, makes clear interface for strategies whatever the parameter’s sort and readability of this system.
  • Utilizing the operate overloading idea, we will develop multiple operate with the identical title, however the arguments handed must be of various varieties.
  • Perform overloading executes this system quicker.
  • Perform overloading is used for code reusability and to avoid wasting reminiscence.

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

Disadvantages of Perform Overloading in c++

  • Perform declarations that differ by their return sort can’t be overloaded with the operate overloading course of.
  • If any static member operate is said, then the identical parameters or the identical title varieties can’t be overloaded.

Distinction between Perform Overloading and Operator Overloading

Perform Overloading Operator Overloading
Perform 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’ll be able to overload the operate with the identical title however with completely different parameters. You’ll be able to overload (outline customized behaviour) for operators resembling ‘+’, ‘-‘, ‘()’, ‘[]’.
Perform overloading means utilizing a single title 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 upon the kind of its operands. When a operate is overloaded, the identical operate title has completely different interpretations relying on its signature, which is the checklist of argument varieties within the features parameter checklist.

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

Distinction between Perform Overloading and Perform Overriding

  Perform Overloading Perform Overriding
Definition In operate overloading, two or extra strategies in a single class have completely different parameters however the identical methodology title. In operate overriding, two strategies have the identical parameters, and methodology title, however one methodology is within the mother or father class and the opposite is within the baby class.
Perform Signature Both sort of parameters or the variety of parameters ought to differ. Perform signature ought to stay the identical.
Habits Defines a number of behaviors to a way. Modifications the habits of the strategy.
Scope of Perform They’re in the identical scope. They’re in numerous scope.
Inheritance It will probably 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.

Perform overloading in C++ FAQs

What’s operate overloading in C++?

Perform overloading refers to when two or extra features with the identical title however distinct parameters exist.
Perform overloading is without doubt one of the most vital traits of C++, amongst its many different options. There are a lot of features with the identical title on this code, every with a novel set of argument lists. The information sort and ordering of the arguments are additionally included on this argument checklist.
The C++ operate 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 operate names. Overloaded features are those who belong to a category however have multiple occasion with the identical title however completely different parameters. The operate should be overloaded if a single operation should be carried out with numerous numbers or sorts of parameters.
Perform overloading is known as a operate of polymorphism in OOP.

What’s operate overloading clarify?

Just like polymorphism, operate overloading allows us to acquire completely different behaviour whereas sustaining the operate’s title. In C++, operate overloading is used to cut back reminiscence utilization and enhance code reuse.

What’s runtime polymorphism C++?

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

What’s inheritance in C++?

By utilizing 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 sort x within the class specification of sort B.

What are some great benefits of operate overloading?

The elemental good thing about operate overloading is that it makes code simpler to learn and reuse.
Perform overloading is used to enhance consistency, readability, and reminiscence effectivity.
This system’s execution is accelerated by it.
Moreover, code maintenance turns into easy.
Perform overloading provides code flexibility.
The operate’s versatility avoids the necessity for a lot of operate names to conduct the identical set of features.

Conclusion

Perform overloading in C++ can be utilized in numerous methods to reinforce code readability. Whereas doing programming in C++, it helps to avoid wasting compile time and in addition saves reminiscence area. You additionally realized how comparable operations are carried out utilizing the operate overloading idea.

You’ll be able to improve your operate overloading in c++ ideas right here:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments