Skip to main content

C++ Code For Function Overloading

Example 1: Function Overloading by Number of Arguments

A C++ code having a Calculator class with function overloading (multiple add methods).
Here’s the full working program:

#include <iostream>
using namespace std;

// Calculator class demonstrating Function Overloading
class Calculator {
public:
    // Function to add two integers
    int add(int a, int b) {
        return a + b;
    }
    // Function to add three integers
    int add(int a, int b, int c) {
        return a + b + c;
    }
    // Function to add two doubles
    double add(double a, double b) {
        return a + b;
    }
};

int main() {
    Calculator calc;
    cout << "Sum of 2 and 3 (int): " << calc.add(2, 3) << endl;
    cout << "Sum of 2, 3 and 5 (int): " << calc.add(2, 3, 5) << endl;
    cout << "Sum of 2.5 and 3.5 (double): " << calc.add(2.5, 3.5) << endl;
    return 0;
}
Output : Sum of 2 and 3 (int): 5 
Sum of 2, 3 and 5 (int): 10 
Sum of 2.5 and 3.5 (double): 6

This is an ideal case of Function Overloading in C++ - the add function acts differently, depending on the arguments and quantity of the arguments.

Example 2: Function Overloading by Type of Arguments

A C++ code having Print class with function overloading (display function works for int, double, and string).

Here’s the full working program:
#include <iostream>
using namespace std;

class Print {
public:
    void display(int a) {
        cout << "Integer: " << a << endl;
    }
    void display(double d) {
        cout << "Double: " << d << endl;
    }
    void display(string s) {
        cout << "String: " << s << endl;
    }
};

int main() {
    Print p;

    p.display(10);        // Calls int version
    p.display(3.14);      // Calls double version
    p.display("Hello");   // Calls string version

    return 0;
}
Output:
Integer: 10
Double: 3.14
String: Hello

This is another good example of function overloading in C++.
In this case the name of the function display is the same, but the parameter type determines which one to call.

Thanks for reading 💗!


If you found this post useful:

⮕  Share it with others who might benefit.
⮕  Leave a comment with your thoughts or questions—I’d love to hear from you.
⮕  Follow/Subscribe to the blog for more helpful guides, tips, and insights.


Comments

Popular posts from this blog

Introduction to Machine Learning – A Beginner’s Guide

The world we live in is a technologically oriented one. Apps that suggest songs, recommend movies, translate languages, identify our faces and even drive cars automatically are used every day. Behind all this progress is something called  Machine Learning (ML) . This blog will describe what Machine Learning is, how it functions, the variations, and the real life examples of this which we can see in our everyday lives in the most simplistic manner. What is Machine Learning? Suppose you have to teach a child to identify fruits. You do not give them a list of rules such as: An apple is round. A banana is long. A mango is yellow. Rather, you  show numerous pictures  of apples, bananas, and mangoes. After enough examples, the child starts to recognize them on their own. Machine Learning is precisely this. It is a way of training  computers to learn from data and examples instead of hard-coded rules . Instead of coding each step we allow the computer to identify patterns...

Early vs Late Binding in C++ (Static vs Dynamic Binding) Explained with Examples

Two concepts you will come to hear frequently when learning OOP are binding and its types: early binding, late binding, static binding, and dynamic binding . The terminology may seem technical initially, but don’t worry — we will decompose them into easy-to-understand analogies and simple code samples (C++) combined with a comparison table. What is Binding in Programming? Binding means linking a function call to the actual function definition.  Example analogy: Imagine you’re calling your friend on the phone. If you know exactly which number to dial before calling → this is early/static binding . If you just say “Call my best friend” and your phone decides at runtime who that is → this is late/dynamic binding . 1. Early Binding (Compile-time Binding / Static Binding) Function calls are resolved at compile time . The compiler already knows which function to call. Faster, but less flexible. Example (C++ Function Overloading): Here, the compiler deci...

C++ Code for Function Overriding Without virtual

Example In C++, function overriding happens when a derived class defines a function with the same name as a base class function. Unless the base class method is specified as virtual, the compiler engages in early binding (compile-time decision). This implies that the base class version will be invoked even when the pointer points to an object of the derived classes. This leads to a lack of real-time polymorphism occurring. Code Example # include < iostream > using namespace std ; class Animal { public : void sound ( ) { // Not virtual cout < < " Animal makes a sound " < < endl ; } } ; class Dog : public Animal { public : void sound ( ) { cout < < " Dog barks " < < endl ; } } ; int main ( ) { Animal * a1 ; Dog d ; a1 = & d ; a1 - > sound ( ) ; // Calls Animal's version (not Dog's) return 0 ; } Output Animal make...