0% found this document useful (0 votes)
11 views

Overloaded Subprograms Implementation

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Overloaded Subprograms Implementation

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Overloaded Subprograms Implementation

Overloading occurs when multiple subprograms (functions or methods) share the same name
but differ in the number or types of parameters. It allows the same function name to perform
different tasks based on the input arguments.

Overloaded subprograms are implemented by the compiler through function signatures


(function name + parameter types). The compiler distinguishes between the overloaded
versions during compilation.

Example of Overloaded Subprograms (C++)

In C++, overloaded functions are implemented using different parameter types or numbers of
parameters.

#include <iostream>
using namespace std;

// Function to add two integers


int add(int a, int b) {
return a + b;
}

// Overloaded function to add two floating-point numbers


float add(float a, float b) {
return a + b;
}

// Overloaded function to concatenate two strings


string add(string a, string b) {
return a + b;
}

int main() {
cout << "Adding integers: " << add(5, 10) << endl; // Calls int version
cout << "Adding floats: " << add(5.5f, 10.2f) << endl; // Calls float version
cout << "Adding strings: " << add("Hello, ", "World!") << endl; // Calls string version
return 0;
}
Output
Adding integers: 15
Adding floats: 15.7
Adding strings: Hello, World!

How Overloading Works

1. Compile-Time Resolution:

○The compiler selects the appropriate function based on the arguments provided
in the function call.
○ The selection is based on parameter types and number of parameters.
2. Signature Matching:

○ Each overloaded version has a unique signature (name + parameter types). For
example:
■ add(int, int)
■ add(float, float)
■ add(string, string)
3. Function Dispatch:

○ The compiler generates separate code for each overloaded version and ensures
the correct one is called at runtime.

Key Points

1. Rules for Overloading:

○ Functions must differ in the number or types of parameters.


○ Overloading cannot be based on the return type alone.
2. Advantages:

○ Simplifies code by allowing the same function name to handle different data
types or operations.
○ Improves code readability and maintainability.
3. Limitations:

○ In some cases, ambiguities can arise when the compiler cannot determine which
function to call.
Overloaded Subprograms in Other Languages
Java: Java also supports method overloading with similar rules.

public class OverloadingExample {


public static int add(int a, int b) {
return a + b;
}
public static double add(double a, double b) {
return a + b;
}
public static String add(String a, String b) {
return a + b;
}
public static void main(String[] args) {
System.out.println("Adding integers: " + add(5, 10)); // Calls int version
System.out.println("Adding doubles: " + add(5.5, 10.2)); // Calls double version
System.out.println("Adding strings: " + add("Hello, ", "World!")); // Calls string version
}
}

You might also like