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++ 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++.
iostream
library, include the iostream
header.#include <iostream>
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
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
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.
How to overload functions in C++:
#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; }
Overloading constructors and destructors in C++:
#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 }
Operator overloading in C++:
#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; }
Overloading member functions in C++ classes:
#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; }
Function overloading with default parameters in C++:
#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; }
Ambiguity resolution in function overloading in C++:
#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; }
Overloading functions for different data types in C++:
#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; }
Recursive function overloading in C++:
#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; }
Function overloading and inheritance in C++:
#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; }