C++ Tutorial

Class and Object

Reference

Inheritance and Derivation

Polymorphism and Virtual Functions

Operator Overloading

Template

Exception

Object Oriented Advanced

Input/Output Stream

File Operations

Function Overloading in C++

Function overloading in C++ is a feature that allows multiple functions with the same name but with different parameter lists (different number or types of parameters). The appropriate function is chosen by the compiler based on the arguments passed during the function call.

In this tutorial, we'll cover the basics of function overloading in C++.

  • Include the required headers: To use the iostream library, include the iostream header.
#include <iostream>
  • Function Overloading with Different Number of Parameters:

You can create overloaded functions with a different number of parameters.

Example:

#include <iostream>

void display(int a) {
    std::cout << "Displaying integer: " << a << std::endl;
}

void display(int a, int b) {
    std::cout << "Displaying two integers: " << a << ", " << b << std::endl;
}

int main() {
    int num1 = 5;
    int num2 = 10;

    display(num1);
    display(num1, num2);

    return 0;
}

Output:

Displaying integer: 5
Displaying two integers: 5, 10
  • Function Overloading with Different Parameter Types:

You can also create overloaded functions with different parameter types.

Example:

#include <iostream>

void display(int a) {
    std::cout << "Displaying integer: " << a << std::endl;
}

void display(double d) {
    std::cout << "Displaying double: " << d << std::endl;
}

int main() {
    int num1 = 5;
    double num2 = 5.5;

    display(num1);
    display(num2);

    return 0;
}

Output:

Displaying integer: 5
Displaying double: 5.5
  • Function Overloading with Different Order of Parameters:

You can also create overloaded functions with a different order of parameters.

Example:

#include <iostream>

void display(int a, double b) {
    std::cout << "Displaying integer and double: " << a << ", " << b << std::endl;
}

void display(double a, int b) {
    std::cout << "Displaying double and integer: " << a << ", " << b << std::endl;
}

int main() {
    int num1 = 5;
    double num2 = 5.5;

    display(num1, num2);
    display(num2, num1);

    return 0;
}

Output:

Displaying integer and double: 5, 5.5
Displaying double and integer: 5.5, 5

These examples demonstrate the basics of function overloading in C++. By using function overloading, you can write more expressive and readable code, making it easier to understand and maintain.

  1. How to overload functions in C++:

    • Description: Introduces the concept of function overloading, where multiple functions with the same name are defined, differing in the number or types of parameters.
    • Example Code:
      #include <iostream>
      
      // Function overloading
      int add(int a, int b) {
          return a + b;
      }
      
      double add(double a, double b) {
          return a + b;
      }
      
      int main() {
          std::cout << add(1, 2) << std::endl;        // Calls the int version
          std::cout << add(1.5, 2.5) << std::endl;    // Calls the double version
          return 0;
      }
      
  2. Overloading constructors and destructors in C++:

    • Description: Explores overloading constructors and destructors to provide multiple ways to initialize and clean up class objects.
    • Example Code:
      #include <iostream>
      
      class MyClass {
          private:
              int value;
      
          public:
              // Overloaded constructors
              MyClass() : value(0) {}
              MyClass(int v) : value(v) {}
      
              // Overloaded destructor
              ~MyClass() {
                  std::cout << "Destructor called for value: " << value << std::endl;
              }
      };
      
      int main() {
          MyClass obj1;       // Calls the default constructor
          MyClass obj2(42);   // Calls the parameterized constructor
          return 0;           // Calls the destructor for obj2, then obj1
      }
      
  3. Operator overloading in C++:

    • Description: Demonstrates how operators can be overloaded for user-defined types, allowing custom behavior for operations.
    • Example Code:
      #include <iostream>
      
      class ComplexNumber {
          private:
              double real;
              double imaginary;
      
          public:
              ComplexNumber(double r, double i) : real(r), imaginary(i) {}
      
              // Overloading the + operator
              ComplexNumber operator+(const ComplexNumber& other) const {
                  return ComplexNumber(real + other.real, imaginary + other.imaginary);
              }
      
              // Overloading the << operator for printing
              friend std::ostream& operator<<(std::ostream& os, const ComplexNumber& obj);
      };
      
      std::ostream& operator<<(std::ostream& os, const ComplexNumber& obj) {
          os << obj.real << " + " << obj.imaginary << "i";
          return os;
      }
      
      int main() {
          ComplexNumber num1(1, 2);
          ComplexNumber num2(3, 4);
      
          ComplexNumber result = num1 + num2;
      
          std::cout << "Sum of complex numbers: " << result << std::endl;
      
          return 0;
      }
      
  4. Overloading member functions in C++ classes:

    • Description: Demonstrates how member functions of a class can be overloaded based on different parameters.
    • Example Code:
      #include <iostream>
      
      class MyClass {
          public:
              // Overloaded member functions
              void display(int value) {
                  std::cout << "Displaying integer: " << value << std::endl;
              }
      
              void display(double value) {
                  std::cout << "Displaying double: " << value << std::endl;
              }
      };
      
      int main() {
          MyClass obj;
          obj.display(42);       // Calls the int version
          obj.display(3.14);     // Calls the double version
          return 0;
      }
      
  5. Function overloading with default parameters in C++:

    • Description: Shows how function overloading can be combined with default parameters to provide flexibility in function calls.
    • Example Code:
      #include <iostream>
      
      // Overloaded function with default parameters
      void display(int a, int b = 0) {
          std::cout << "Values: " << a << " and " << b << std::endl;
      }
      
      int main() {
          display(42);       // Calls the function with default parameter
          display(1, 2);     // Calls the function without default parameter
          return 0;
      }
      
  6. Ambiguity resolution in function overloading in C++:

    • Description: Discusses how to resolve ambiguity issues when multiple overloaded functions match a given function call.
    • Example Code:
      #include <iostream>
      
      // Ambiguous function declarations
      void printValue(int a);
      void printValue(double b);
      
      int main() {
          // Ambiguous function call (uncommenting any one of the declarations resolves the ambiguity)
          // printValue(42.5);
          return 0;
      }
      
  7. Overloading functions for different data types in C++:

    • Description: Illustrates how functions can be overloaded to handle different data types, providing versatility.
    • Example Code:
      #include <iostream>
      
      // Overloaded function for different data types
      template <typename T>
      void display(T value) {
          std::cout << "Value: " << value << std::endl;
      }
      
      int main() {
          display(42);        // Calls the function with int
          display(3.14);      // Calls the function with double
          display("Hello");   // Calls the function with const char*
          return 0;
      }
      
  8. Recursive function overloading in C++:

    • Description: Demonstrates recursive function overloading, where a function calls another version of itself with modified parameters.
    • Example Code:
      #include <iostream>
      
      // Recursive function overloading
      int factorial(int n) {
          if (n == 0 || n == 1) {
              return 1;
          } else {
              return n * factorial(n - 1);
          }
      }
      
      int main() {
          std::cout << "Factorial of 5: " << factorial(5) << std::endl;
          return 0;
      }
      
  9. Function overloading and inheritance in C++:

    • Description: Explores how function overloading interacts with inheritance, allowing derived classes to override or overload base class functions.
    • Example Code:
      #include <iostream>
      
      // Base class
      class Shape {
          public:
              // Function to display shape information
              void display() const {
                  std::cout << "Generic shape" << std::endl;
              }
      };
      
      // Derived class with function overloading
      class Circle : public Shape {
          public:
              // Overloaded function to display circle information
              void display(double radius) const {
                  std::cout << "Circle with radius: " << radius << std::endl;
              }
      };
      
      int main() {
          Circle circleObj;
          circleObj.display();        // Calls the base class function
          circleObj.display(3.0);    // Calls the derived class function
          return 0;
      }