Both Java and C++ programming languages support method and function overloading, respectively. Function overloading is simply having more than one function, which is differentiated either by having differences in the number of arguments or by the difference in the data types passed as arguments or parameters. The real question is how the compiler of java and c++ programming languages differentiates one function from the other.
The simple answer to a complex problem, the compiler adds more information from its end. It saves the methods or parameters to its stack memory during execution. The c++ compilers don't have any specific technique different from the others, and it uses the same name-mangling concept to solve the problem 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