An Overview of Function Overloading in C++

Function overloading is the process of having two or more functions with the same names but different parameters. It enables one to use the same name for different functions in the same class. Some rules must be considered when overloading a function.

Function Overloading Rules

  • Overloading functions must have the same name.
  • The number of arguments must not be the same.
  • Data types of the arguments must not be the same.
  • The order of arguments can be different.
  • The return type can also be different.

More like this:

Ways to Overload a Function in C++

There are various ways of overloading a function. They include,

  • Altering the number of arguments.
  • Having separate types of arguments.

Contrasting number of arguments

Two functions are defined using the same names but different parameters of the same type. For instance, a sum() function can illustrate the concept of overloading by having two definitions, one that accepts three arguments and another that accepts four arguments. An illustration is shown below.

Function 1

int main()
int sum(int a, int b, int c) {
  cout << a + b + c;
}

Function 2

int sum(int a, int b, int c, int d) {
  cout << a + b + c + d;
}

Separate types of arguments

Multiple functions are defined using the same name and number of parameters. However, the type of parameters used is different. Below is an illustration that uses the sum() function, one that obtains integer arguments and obtains double arguments.

#include<iostream>
#include<conio.h>

// Function 1
int sum(int a, int b) {
  cout << a + b;
}

// Function 2
double sum(double a, double b) {
  cout << a + b;
}

int main() {
  sum(50, 70)
  sum(50.5, 70.5)
}

Constructor Overloading in C++

A constructor is considered to be special due to the following reasons.

  • It has the same name as the class.
  • It does not have any return value.
  • It is declared in the public part of the class.
  • It initializes the scope for the objects involved.

Operator Overloading

Operators are overloaded to carry out special operations about the classes created. They can be overloaded by creating operator functions. By using the keyword operator, an operator function is created. Operator functions often return objects of the class they operate on.

Restrictions to operator overloading

  • The pre-eminence of an operator cannot be changed.
  • The number of operands taken by an operator cannot be altered.
  • They lack default arguments apart from the function call operator.

Over-loadable operators

+ - * / % ^
& | ~ ! , =
< > <= >= ++ --
<< >> == != && ||
+= -= /= %= ^= &=
|= *= <<= >>= []
-> ->* new new [] delete delete []

Non-overloadable operators

:: .* . ?:

Operator overloading syntax

Returntype classname::operator operatorsymbol(argument list) {
  Statement(s);
}

Rules regarding operator overloading

  • Overloading can only be done to existing members.
  • The basic meaning of an operator cannot be altered.
  • Not all operators can be overloaded.
  • The left-hand operand should be an object of the related class when a binary operator is overloaded through a member function.
  • For an operator to be overloaded, it must have at least one operand of a user-defined type.

Leave a Reply

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