function overloading, method overloading, overloading c++, constructor overloading function overloading in c++ function overloading function overloading in java what is function overloading what is function overloading in c++ function overloading in python function overloading and overridingFunction overloading , Method overloading by interviewspreparation.com

Function overloading or method overloading is indeed a crucial topic to address when preparing for an interview. Fear not, for I shall provide a comprehensive explanation of what function overloading entails, accompanied by fundamental illustrations. By delving into this, you will gain insights into overloading methods in C++, C#, JAVA, or Python.

Firstly, I would like to understand what comes to your mind when you hear the word ‘overload’. Please share your thoughts. In general terms, ‘overload’ implies filling something more to its actual capacity, meaning overloading, overfilling, or overflowing, am I correct?

Question posed on Quora. Join us there; it’s free to sign up and ask questions, and the community typically responds within 24 hours. Or do check our beginner and intermedia level interview programs.

What Is Method Overloading

Allow me to clarify: in programming, ‘method’ and ‘function’ refer to the same concept. Please don’t be confused if I use ‘method’ instead of ‘function’.

Now that you have a clear understanding of the term ‘overload‘, allow me to proceed to the programming aspect. In languages such as C++, Java, Python, and C#, or when working with object-oriented architectures to handle objects, we utilize method overloading or function overloading.

function overloading example, method overloading example, overloading example,
function overloading example in c++
function overloading example
function overloading example in java
what is function overloading
what is function overloading in c++
function overloading in python
function overloading and overriding
Function-overloading-method-overloading-examples-by-interviewspreparation.com

Function Overloading in C++

Let’s implement function overloading in the C++ manner based on the example above. We’ll create multiple methods with the same name, ‘CreateSuperHero’. However, upon doing so, we utilize subprogram overloading, where we define multiple functions with the same name but different parameter lists.


//program by interviewsprepartion.com

#include <iostream>
#include <vector>
#include <array>

using namespace std;

#define newline '\n';

void CreateSuperHero()
{
	cout << "created basic super hero" << newline;
}

void CreateSuperHero(int speed)
{
	cout << "created super hero with " << speed << " speed" << newline;
}
void CreateSuperHero(string name)
{
	cout << "created super hero with " << name<< " named" << newline;
}
void CreateSuperHero(int speed, int strength)
{
	cout << "created super hero with " << speed << " speed and " << strength << " strength"<<newline;
}

void CreateSuperHero(int speed,string name, int strength)
{
	cout << "created super hero with " << speed << " speed and "<<name <<"named" << strength << " strength"<<newline;
}


int main() 
{
	CreateSuperHero();
	CreateSuperHero(100);
  CreateSuperHero("You");
	CreateSuperHero(200, 1000);
	CreateSuperHero(250, "You", 1200);
}

Method Overloading In Python

In Python, you can achieve function overloading in a slightly different manner compared to languages like C++. Python does not support traditional function overloading based on different parameter types or numbers of parameters. However, you can simulate routine overloading using default parameter values and variable-length argument lists. Here’s an example


# program by interviewspreparation.com

def create_superhero(speed=None, name=None, strength=None):
    if speed is None and name is None and strength is None:
        print("created basic super hero")
    elif speed is not None and name is None and strength is None:
        print(f"created super hero with {speed} speed")
    elif speed is not None and name is None and strength is not None:
        print(f"created super hero with {speed} speed and {strength} strength")
    elif speed is not None and name is not None and strength is not None:
        print(f"created super hero with {speed} speed, {name} name, and {strength} strength")

# Testing the functions
create_superhero()  
create_superhero(100)  
create_superhero(200, 1000)  
create_superhero(250, "You", 1200)

Function Overloading In C#

In C#, function overloading is directly supported. You can define multiple functions with the same name but with different parameter lists. Here’s an example of achieving function overloading in C#


// program by interviewspreparation.com

using System;

class Program
{
    static void CreateSuperHero()
    {
        Console.WriteLine("created basic super hero");
    }

    static void CreateSuperHero(int speed)
    {
        Console.WriteLine($"created super hero with {speed} speed");
    }

    static void CreateSuperHero(string name)
    {
        Console.WriteLine($"created super hero named {name}");
    }

    static void CreateSuperHero(int speed, int strength)
    {
        Console.WriteLine($"created super hero with {speed} speed and {strength} strength");
    }

    static void CreateSuperHero(int speed, string name, int strength)    {
        Console.WriteLine($"created super hero with {speed} speed, {name} named, and {strength} strength");
    }

    static void Main(string[] args)
    {
        CreateSuperHero();
        CreateSuperHero(100);
        CreateSuperHero(200, 1000);
        CreateSuperHero(250, "You", 1200);
    }
}

Method Overloading In Java


class Main {
    static void CreateSuperHero() {
        System.out.println("created basic super hero");
    }

    static void CreateSuperHero(int speed) {
        System.out.println("created super hero with " + speed + " speed");
    }

    static void CreateSuperHero(String name) {
        System.out.println("created super hero named " + name);
    }

    static void CreateSuperHero(int speed, int strength) {
        System.out.println("created super hero with " + speed + " speed and " + strength + " strength");
    }

    static void CreateSuperHero(int speed, String name, int strength) {
        System.out.println("created super hero with " + speed + " speed, " + name + " named, and " + strength + " strength");
    }

    public static void main(String[] args) {
        CreateSuperHero();
        CreateSuperHero(100);
        CreateSuperHero(200, 1000);
        CreateSuperHero(250, "You", 1200);
    }
}

Summarizing Function Overloading

Function overloading is a fundamental concept in programming, particularly in languages like C++, Java, Python, and C#. It allows us to define multiple functions with the same name but different parameter lists within a program. Drawing an analogy to the versatility of a superhero possessing various powers, method overloading enables us to create methods such as ‘createSuperhero’ with different sets of parameters, catering to diverse data types or quantities.

Leave a Reply

Your email address will not be published. Required fields are marked *