Both Java and C++ programming languages provide support for method and function overloading, respectively. Method overloading refers to the ability to have multiple functions with the same name within a class, differentiated by variations in the number or data types of their parameters. The key question lies in understanding how the compilers of Java and C++ distinguish between these overloaded functions.
The straightforward solution to a intricate issue, the compiler augments additional details from its perspective. It stores the functions or arguments in its stack memory while running. C++ compilers do not employ a unique method compared to others, relying on the identical name-mangling principle to address the challenge of function overloading.
Function Overloading in C++ -1
// Here we are writing down the C++ programming language code to
// demonstrate the concept of function overloading with its relevant code
// and output supported by valid syntax wherever necessary
#include <iostream>
using namespace std;
// function add in its avatar of adding two variables here of integer data
void add(int a, int b)
{
cout << "sum of two integer variables is= " << (a + b);
}
// function add in its avatar of adding two variables here of double data
void add(double a, double b)
{
cout << endl << "sum of two double variables is= " << (a + b);
}
// the main driver code functionality starts from here
int main()
{
add(130, 92);
add(85.3, 36.2);
return 0;
// the main driver code functionality ends from here
}
Output:
The sum of two integer variables is= 222
The sum of the two double variables is= 121.5
Function Overloading in C++ -2
// Here we are writing down the C++ programming language code to
// demonstrate the concept of function overloading with its relevant code
// and output supported by valid syntax wherever necessary
#include <iostream>
using namespace std;
// function add in its avatar of adding two variables here of integer data
void add(int a, int b)
{
cout << "sum of two integer variables is= " << (a + b);
}
// function add in its avatar of adding three variables here of integer data
void add(int a, int b, int c)
{
cout << endl << "sum of three integer variables is= " << (a + b + c);
}
// the main driver code functionality starts from here
int main()
{
add(410, 20);
add(55, 66, 44);
return 0;
// the main driver code functionality ends from here
}
Output:
The sum of two integer variables is= 430
The sum of three integer variables is= 165
Function Overloading in C++ -3
// Here we are writing down the C++ programming language code to
// demonstrate the concept of function overloading with its relevant code
// and output supported by valid syntax wherever necessary
#include <iostream>
using namespace std;
void print(int i) {
cout << " Here we have the integer " << i << endl;
}
void print(double f) {
cout << " Here we have the float " << f << endl;
}
void print(char const *c) {
cout << " Here we have the character " << c << endl;
}
// the main driver code functionality starts from here
int main() {
print(100);
print(100.100);
print("hundred");
return 0;
// the main driver code functionality ends from here
}
Output:
Here we have the integer 100
Here we have the float 100.1
Here we have the character hundred
Name Mangling and Extern 'C' in C++
Demonstration code
// Here we are writing down the C++ programming language code to
// demonstrate the concept of name mangling with its relevant code
int __f_v(void) { return 1; }
int __f_i(int) { return 0; }
void __g_v(void) { int i = __f_v(), j = __f_i(0); }
Output:
/usr/bin/ld: cannot open output file a.out: Permission denied
collect2: error: ld returned 1 exit status
C++ code
// Here we are writing down the C++ programming language code to
// demonstrate the concept of name mangling with its relevant code
// showing C code doesn't support name mangling
int printf(const char* format,...);
// The main driver code functionality starts from here
int main()
{
printf("javaCppTutorial");
return 0;
// The main driver code functionality ends from here
}
Output:
/usr/bin/ld: cannot open output file a.out: Permission denied
collect2: error: ld returned 1 exit status
Extern C
// Here we are writing down the C++ programming language code to
// demonstrate the concept of name mangling with its relevant code
// C++ code to demonstrate Extern "C"
extern "C" {
int printf(const char* format,...);
}
// The main driver code functionality starts from here
int main()
{
printf("javaCppTutorial");
return 0;
// The main driver code functionality ends from here
}
Output:
javaCppTutorial