Introduction to C++ | Function Overloading | Operator Overloading

C++ Function Overloading

Function Overloading in C++ allows multiple functions to have the same name but different parameters. It enables developers to define multiple behaviors for a function based on the number or type of arguments provided.

What is Function Overloading?

Function overloading is a feature of polymorphism in object-oriented programming. It allows the same function name to perform different tasks based on the arguments passed to it. The compiler differentiates between functions by checking their function signatures (the number and type of arguments).

Key Features of Function Overloading

  • Functions must have the same name but different parameters.
  • Overloading is achieved through different:
    • Number of parameters
    • Types of parameters
    • Order of parameters
  • Return type is not considered for overloading.

Syntax of Function Overloading

Syntax Example


            void functionName(int a);
            void functionName(double a);
            void functionName(int a, int b);
                        

Example of Function Overloading

Here’s an example where a function named add performs addition on integers, doubles, and three integers:

Code Example


            #include <iostream>
            using namespace std;
            
            // Overloaded functions
            int add(int a, int b) {
                return a + b;
            }
            
            double add(double a, double b) {
                return a + b;
            }
            
            int add(int a, int b, int c) {
                return a + b + c;
            }
            
            int main() {
                cout << "Addition of two integers: " << add(5, 10) << endl;
                cout << "Addition of two doubles: " << add(5.5, 10.5) << endl;
                cout << "Addition of three integers: " << add(5, 10, 15) << endl;
                return 0;
            }
                        

Output

Addition of two integers: 15
Addition of two doubles: 16
Addition of three integers: 30

Advantages of Function Overloading

  • Improves code readability and usability.
  • Allows the same function name to handle different data types or tasks.
  • Helps in reducing the number of function names required in a program.

Function Overloading with Default Parameters

Function overloading can also be combined with default parameters to provide greater flexibility:

Code Example with Default Parameters


            #include <iostream>
            using namespace std;
            
            // Overloaded functions with default parameters
            void greet() {
                cout << "Hello!" << endl;
            }
            
            void greet(string name) {
                cout << "Hello, " << name << "!" << endl;
            }
            
            void greet(string name, string title = "Mr./Ms.") {
                cout << "Hello, " << title << " " << name << "!" << endl;
            }
            
            int main() {
                greet();
                greet("Alice");
                greet("Alice", "Dr.");
                return 0;
            }
                        

Output

Hello!
Hello, Alice!
Hello, Dr. Alice!

C++ Operator Overloading

What is Operator Overloading?

Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type. For example, C++ provides the ability to add the variables of the user-defined data type that is applied to the built-in data types.

Operator that cannot be overloaded are as follows:

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

Syntax of Operator Overloading

Syntax Example


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

Example of Opeartor Overloading

Let's see the simple example of operator overloading in C++. In this example, void operator ++ () operator function is defined (inside Test class).

Code Example


                #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

Rules for Operator Overloading

  • Existing operators can only be overloaded, but the new operators cannot be overloaded.
  • The overloaded operator contains atleast one operand of the user-defined data type.
  • We cannot use friend function to overload certain operators. However, the member function can be used to overload those operators.
  • When unary operators are overloaded through a member function take no explicit arguments, but, if they are overloaded by a friend function, takes one argument.
  • When binary operators are overloaded through a member function takes one explicit argument, and if they are overloaded through a friend function takes two explicit arguments.

Common Mistakes with Overloading

  • Relying on return type for differentiation (return type alone is not sufficient).
  • Using ambiguous function signatures that lead to conflicts during compilation.
  • Overusing function overloading, which can make code harder to maintain.

Pro Tip:

💡 Pro Tip

Use function overloading when functions perform similar tasks but operate on different types or numbers of inputs. Keep the signatures distinct to avoid ambiguity, and avoid excessive overloading to maintain readability.