Implementation of Function Overloading
Below is the implementation of Function Overloading:
#include<iostream>
using namespace std;
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(int i, double f) {
cout << "Printing int and float: " << i << ',' << f << endl;
}
void print(double f, int i) {
cout << "Printing float and int: " << f << ',' << i << endl;
}
int main() {
print(10);
print(10.10);
print(10, 10.10);
print(10.10, 10);
return 0;
}
public class Main {
static void print(int i) {
System.out.println("Printing int: " + i);
}
static void print(double f) {
System.out.println("Printing double: " + f);
}
static void print(int i, double f) {
System.out.println("Printing int and double: " + i + ", " + f);
}
static void print(double f, int i) {
System.out.println("Printing double and int: " + f + ", " + i);
}
public static void main(String[] args) {
print(10);
print(10.10);
print(10, 10.10);
print(10.10, 10);
}
}
def print_value(*args):
if len(args) == 1:
if isinstance(args[0], int):
print(f"Printing int: {args[0]}")
elif isinstance(args[0], float):
print(f"Printing float: {args[0]}")
elif len(args) == 2:
if isinstance(args[0], int) and isinstance(args[1], float):
print(f"Printing int and float: {args[0]},{args[1]}")
elif isinstance(args[0], float) and isinstance(args[1], int):
print(f"Printing float and int: {args[0]},{args[1]}")
else:
print("Unsupported argument types")
else:
print("Unsupported number of arguments")
def main():
print_value(10)
print_value(10.10)
print_value(10, 10.10)
print_value(10.10, 10)
if __name__ == "__main__":
main()
# Note: In Python, function overloading as seen in C++ isn't directly supported due to
# its dynamic typing system. However, we can achieve similar functionality using default
# arguments, type checking, or argument lists
// Function to print based on argument types and counts
function print() {
if (arguments.length === 1) {
if (typeof arguments[0] === 'number' && Number.isInteger(arguments[0])) {
console.log("Printing int: " + arguments[0]);
} else if (typeof arguments[0] === 'number') {
console.log("Printing float: " + arguments[0]);
}
} else if (arguments.length === 2) {
if (typeof arguments[0] === 'number' && Number.isInteger(arguments[0]) && typeof arguments[1] === 'number') {
console.log("Printing int and float: " + arguments[0] + ',' + arguments[1]);
} else if (typeof arguments[0] === 'number' && typeof arguments[1] === 'number' && Number.isInteger(arguments[1])) {
console.log("Printing float and int: " + arguments[0] + ',' + arguments[1]);
}
}
}
// Main function calls
print(10); // Printing int: 10
print(10.10); // Printing float: 10.1
print(10, 10.10); // Printing int and float: 10,10.1
print(10.10, 10); // Printing float and int: 10.1,10
// Note: JavaScript does not support function overloading in the same way
//C++ does. However, you can achieve similar behavior using different
//techniques, such as checking the types and number of arguments within a single function.
Output
Printing int: 10 Printing float: 10.1 Printing int and float: 10,10.1
Function Overloading in Programming
Function Overloading in programming allows multiple functions to have the same name but with different parameters. It lets a function perform different tasks based on the input parameters. This increases the flexibility and readability of the code. Languages like C++, Java, and C# have function overloading. A programmer can provide different implementations for one function name. The number and types of parameters differentiate them. This enhances code readability and usability. Conceptually similar tasks are handled by function variants.
Table of Content
- What is Function Overloading?
- How Function Overloading Works?
- Implementation of Function Overloading
- Constructors and Destructors Overloading
- Constructor Overloading
- Destructor Overloading:
- Operator Overloading:
- Function Overloading in C
- Function Overloading in C++
- Function Overloading in Java
- Function Overloading in Python
- Advantages of Function Overloading
- Disadvantages of Function Overloading
- Use cases of Function Overloading