Overloading In C++

Overloading in C++ comes in 2 ways function overloading and operator overloading. C++ overloading occurs when two or more members with the same name but differing numbers or types of parameters are created.

We can overload in C++:

  • Methods,
  • Constructors, and
  • Indexed Properties

This is due to the fact that these members-only have parameters.

In C++, there are several types of overloading:

  • Function overloading
  • Operator overloading
types of overloading
Type of overloading

Function Overloading In C++:

In C++, function overloading is described as the process of having two or more functions with the same name but distinct parameters.

The function is redefined by using different types of parameters or a different number of arguments in function overloading. Only these changes allow the compiler to distinguish between the functions.

The benefit of function overloading is that it improves programme readability by eliminating the need for multiple names for the same activity.

C++ Function Overloading Example:

Let’s see the simple example of function overloading where we are changing a number of arguments of add() method.

When the amount of parameters varies, then the function overloading run as:

#include <iostream>    
using namespace std;    
class Cal {    
    public:    
static int add(int a,int b){      
        return a + b;      
    }      
static int add(int a, int b, int c)      
    {      
        return a + b + c;      
    }      
};     
int main(void) {    
    Cal C;                                                    //     class object declaration.   
    cout<<C.add(10, 20)<<endl;      
    cout<<C.add(12, 20, 23);     
   return 0;    
}    

Output:

30
55

Let’s have a look at a basic example where the type of arguments varies.

#include<iostream>  
using namespace std;  
int mul(int,int);  
float mul(float,int);  
  
  
int mul(int a,int b)  
{  
    return a*b;  
}  
float mul(double x, int y)  
{  
    return x*y;  
}  
int main()  
{  
    int r1 = mul(6,7);  
    float r2 = mul(0.2,3);   
    std::cout << "r1 is : " <<r1<< std::endl;  
    std::cout <<"r2 is : "  <<r2<< std::endl;  
    return 0;  
}  

Output:

r1 is : 42
r2 is : 0.6

Function Overloading and Ambiguity:

Function overloading occurs when the compiler is unable to determine which function among the overloaded functions should be invoked.

When the compiler encounters an ambiguity error, the programme is not executed.

Causes Or Problems of Function Overloading Ambiguity:

  • Type Conversion.
  • Function with default arguments.
  • Function with pass by reference.
cause of ambiguity
Case Of Ambiguity

Type Conversion:

Let’s see an example:

#include<iostream>  
using namespace std;  
void fun(int);  
void fun(float);  
void fun(int i)  
{  
    std::cout << "Value of i is : " <<i<< std::endl;  
}  
void fun(float j)  
{  
    std::cout << "Value of j is : " <<j<< std::endl;  
}  
int main()  
{  
    fun(12);  
    fun(1.2);  
    return 0;  
}  

The error “call of overloaded ‘fun(double)’ is ambiguous” can be seen in the example above. The first function will be called by fun(10). According to our forecast, fun(1.2) calls the second function. However, this does not apply to any function because all floating-point constants in C++ are handled as doubles rather than floats. The programme works if we change float to double. As a result, this is a float to double type conversion.

Join Our Community

Join our WhatsApp Group To know more about Programming Language tips, tricks and knowledge about and how to start learning any programming language.

Function with Default Arguments:

Let’s see a simple example:

#include<iostream>  
using namespace std;  
void fun(int);  
void fun(int,int);  
void fun(int i)  
{  
    std::cout << "Value of i is : " <<i<< std::endl;  
}  
void fun(int a,int b=9)  
{  
    std::cout << "Value of a is : " <<a<< std::endl;  
    std::cout << "Value of b is : " <<b<< std::endl;  
}  
int main()  
{  
    fun(12);  
   
    return 0;  
}  

The error “call of overloaded ‘fun(int)’ is ambiguous” is visible in the example above. The function fun(int a, int b=9) can be called in two ways: one with one parameter, i.e. fun(12), and the other with two arguments, i.e. fun(12) (4,5). With only one argument, the fun(int I function is called. As a result, the compiler will be unable to choose between fun(int I and fun(int a,int b=9).

Function with pass by reference:

Let’s see a simple example.

#include <iostream>  
using namespace std;  
void fun(int);  
void fun(int &);   
int main()  
{  
  int a=10;  
  fun(a); // error, which f()?  
  return 0;  
}  
  void fun(int x)  
  {  
  	std::cout << "Value of x is : " <<x<< std::endl;  
  }  
  void fun(int &b)  
  {  
 	 std::cout << "Value of b is : " <<b<< std::endl;  
  }

The error “call of overloaded ‘fun(int&)’ is ambiguous” is visible in the example above. The first function takes an integer as an argument, while the second function takes a reference parameter. Because there is no syntactical distinction between fun(int) and fun(int), the compiler has no idea which function the user requires (int &).

Operators Overloading In C++:

Operator overloading is a compile-time polymorphism where the operator is overloaded to give the user-defined data type a special meaning. Most of the operators available in C++ are overloaded or redefined using operator overloading. It’s used to carry out operations on a user-defined data type. C++, for example, allows you to apply user-defined data type variables to built-in data types.

The benefit of operator overloading is that it allows you to perform several operations on the same operand.

Operators that cannot be overloaded are as follows:

  • Scope operator (::)
  • Sizeof
  • member selector(.)
  • member pointer selector(*)
  • ternary operator(?:)

Syntax of Operator Overloading:

return_type class_name  : : operator op(argument_list)  
{  
     // body of the function.  
}

The function’s return type is the type of value it returns.

The class’s name is a class name.

The operator op is an operator function in which op is the overloaded operator and the keyword is the operator.

Rules for Operator Overloading:

  • Existing operators can be overloaded only to a certain extent, whereas new operators cannot be overloaded at all.
  • At least one operand of the user-defined data type is present in the overloaded operator.
  • To overload particular operators, we can’t use the friend function. The member function, on the other hand, can be used to overload certain operators.
  • When unary operators are overloaded by a member function, they don’t take any explicit arguments; however, when they’re overloaded by a friend function, they do.
  • When binary operators are overloaded via a member function, one explicit parameter is required, whereas when they are overloaded via a friend function, two explicit arguments are required.

C++ Operators Overloading Example:

Let’s look at a simple operator overloading example in C++. The void operator ++ () operator function is defined in this example (inside Test class).

#include <iostream>    
using namespace std;    
class Test    
{    
   private:    
      int num;    
   public:    
       Test(): num(8){}    
       void operator ++()         {     
          num = num+2;     
       }    
       void Print() {     
           cout<<"The Count is: "<<num;     
       }    
};    
int main()    
{    
    Test tt;    
    ++tt;  // calling of a function "void operator ++()"    
    tt.Print();    
    return 0;    
}    

Output:

The Count is: 10

Let’s see a simple example of overloading the binary operators.

#include <iostream>  
using namespace std;  
class A  
{  
    
    int x;  
      public:  
      A(){}  
    A(int i)  
    {  
       x=i;  
    }  
    void operator+(A);  
    void display();  
};  
  
void A :: operator+(A a)  
{  
     
    int m = x+a.x;  
    cout<<"The result of the addition of two objects is : "<<m;  
  
}  
int main()  
{  
    A a1(5);  
    A a2(4);  
    a1+a2;  
    return 0;  
}  

Output:

The result of the addition of two objects is : 9 
Default image
Lingaraj Senapati
Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.
Articles: 217

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply