In this video let us understand Function Overloading. Which is also an example of compile-time polymorphism. What are the ways we can follow to achieve function overloading?
seen from China
seen from Singapore
seen from Switzerland

seen from Malaysia
seen from China
seen from United States
seen from United States
seen from Malaysia
seen from Singapore
seen from China
seen from Philippines

seen from Malaysia
seen from United States
seen from Russia

seen from Malaysia
seen from China
seen from China
seen from United States

seen from Australia
seen from Morocco
In this video let us understand Function Overloading. Which is also an example of compile-time polymorphism. What are the ways we can follow to achieve function overloading?
Polymorphism and Dynamic Binding in Java
Polymorphism and Dynamic Binding in Java
Polymorphism is an Object-Oriented-Programming concept. Whether you are new to Java programming or a person who has worked with Java for years, you should know what polymorphism is in Java and how it works. Most developers claim that they know the topic well but when it comes to other complex features like static and dynamic binding, they seem underconfident. A good understanding of polymorphism…
View On WordPress
Polymorphism in c++
The term “Polymorphism” is the combination of “poly” + “morphs” which means many forms. It is a greek word. In object-oriented programming, we use 3 main concepts: inheritance, encapsulation, and polymorphism. In C++ polymorphism is mainly divided into two types: Compile time PolymorphismRuntime Polymorphism Compile Time Polymorphism This type of polymorphism is achieved by function…
View On WordPress
Function Overload on Return type in C++
In C++ you can’t overload function on return type and there is a reason of it. Suppose C++ allow you to overload function on return type and we overload one function on the basis of return type in such a way that one function return integer and other return character. And when you try to assign return value in character variable then the character return type function is called and when the value…
View On WordPress
Function in C++
FUNCTION in C++
A function in c++ is a group of statement that together performs a particular task. Every C++ program contains a function, that is the main function. Main () is the entry point of the program. At this point, the execution of the program is started.
Syntax:-
void main() { ---------- ---------- }
What is reference?
Reference is an inter pointer. Reference looks like ordinary…
View On WordPress
Why doesn’t C support function overloading?
I have heard this question over and over - Why doesn’t C support function overloading? Rather than quoting C standard back to you, I thought I shall take a practical approach.
Let’s write a simple C program.
#include <stdio.h>
void func() {
printf(”Hello World!\n”);
}
int main() {
func();
return 0;
}
Compile it, and get the binary. Those who are familiar with Linux, shall know the nm command. Lets apply that on the binary, and we get the below output.
0000000000601040 B __bss_start 0000000000601040 b completed.6335 0000000000601030 D __data_start 0000000000601030 W data_start 00000000004004a0 t deregister_tm_clones 0000000000400510 t __do_global_dtors_aux 0000000000600e08 t __do_global_dtors_aux_fini_array_entry 0000000000601038 D __dso_handle 0000000000600e18 d _DYNAMIC 0000000000601040 D _edata 0000000000601048 B _end 0000000000400604 T _fini 0000000000400530 t frame_dummy 0000000000600e00 t __frame_dummy_init_array_entry 0000000000400768 r __FRAME_END__ 000000000040055d T func 0000000000601000 d _GLOBAL_OFFSET_TABLE_ w __gmon_start__ 0000000000400408 T _init 0000000000600e08 t __init_array_end 0000000000600e00 t __init_array_start 0000000000400610 R _IO_stdin_used w _ITM_deregisterTMCloneTable w _ITM_registerTMCloneTable 0000000000600e10 d __JCR_END__ 0000000000600e10 d __JCR_LIST__ w _Jv_RegisterClasses 0000000000400600 T __libc_csu_fini 0000000000400590 T __libc_csu_init U __libc_start_main@@GLIBC_2.2.5 000000000040056d T main U puts@@GLIBC_2.2.5 00000000004004d0 t register_tm_clones 0000000000400470 T _start 0000000000601040 D __TMC_END__
Please keep in mind that it can vary with compiler.
If you look through the above output, you shall see the func function.
Now, let’s compile the same program using G++ compiler, and apply the nm command on the new binary.
0000000000601040 B __bss_start 0000000000601040 b completed.6335 0000000000601030 D __data_start 0000000000601030 W data_start 0000000000400560 t deregister_tm_clones 00000000004005d0 t __do_global_dtors_aux 0000000000600dd8 t __do_global_dtors_aux_fini_array_entry 0000000000601038 D __dso_handle 0000000000600de8 d _DYNAMIC 0000000000601040 D _edata 0000000000601048 B _end 00000000004006b4 T _fini 00000000004005f0 t frame_dummy 0000000000600dd0 t __frame_dummy_init_array_entry 0000000000400818 r __FRAME_END__ 0000000000601000 d _GLOBAL_OFFSET_TABLE_ w __gmon_start__ 00000000004004d0 T _init 0000000000600dd8 t __init_array_end 0000000000600dd0 t __init_array_start 00000000004006c0 R _IO_stdin_used w _ITM_deregisterTMCloneTable w _ITM_registerTMCloneTable 0000000000600de0 d __JCR_END__ 0000000000600de0 d __JCR_LIST__ w _Jv_RegisterClasses 00000000004006b0 T __libc_csu_fini 0000000000400640 T __libc_csu_init U __libc_start_main@@GLIBC_2.2.5 000000000040062d T main U puts@@GLIBC_2.2.5 0000000000400590 t register_tm_clones 0000000000400530 T _start 0000000000601040 D __TMC_END__ 000000000040061d T _Z4funcv
There is no func function! What happened really is that the G++ compiler changed the name of func symbol to _Z4funcv! So when you overload your functions in your C++ code, even though they have the same name in the source code, they have different symbol names in the binary. Its called name mangling.
So if you write a C++ code having a func() function, make it into a library, and try calling it from C, you shall get a “undefined reference to func()”. The reason being that in the C++ library, instead of func, its _Z4funcv. But you can solve this issue by putting the C++ code inside “extern C” block.
#include <stdio.h>
extern “C” {
void func() {
printf(”Hello World!\n”);
}
}
int main() {
func();
return 0;
}
Lets compile it using G++ compiler and see the nm output.
0000000000601040 B __bss_start 0000000000601040 b completed.6335 0000000000601030 D __data_start 0000000000601030 W data_start 0000000000400560 t deregister_tm_clones 00000000004005d0 t __do_global_dtors_aux 0000000000600dd8 t __do_global_dtors_aux_fini_array_entry 0000000000601038 D __dso_handle 0000000000600de8 d _DYNAMIC 0000000000601040 D _edata 0000000000601048 B _end 00000000004006b4 T _fini 00000000004005f0 t frame_dummy 0000000000600dd0 t __frame_dummy_init_array_entry 0000000000400818 r __FRAME_END__ 000000000040061d T func 0000000000601000 d _GLOBAL_OFFSET_TABLE_ w __gmon_start__ 00000000004004d0 T _init 0000000000600dd8 t __init_array_end 0000000000600dd0 t __init_array_start 00000000004006c0 R _IO_stdin_used w _ITM_deregisterTMCloneTable w _ITM_registerTMCloneTable 0000000000600de0 d __JCR_END__ 0000000000600de0 d __JCR_LIST__ w _Jv_RegisterClasses 00000000004006b0 T __libc_csu_fini 0000000000400640 T __libc_csu_init U __libc_start_main@@GLIBC_2.2.5 000000000040062d T main U puts@@GLIBC_2.2.5 0000000000400590 t register_tm_clones 0000000000400530 T _start 0000000000601040 D __TMC_END__
Yes! We now have the func symbol in the binary! We can now make it into a C++ library and shall have no problem calling it from C code.
Any function declared/defined within extern C block shall have C linkage, meaning that you can’t have overloaded functions within extern C block (hope you can figure that out yourselves!)
If you look through some C++ libraries, you shall see code like the below.
#ifdef __cplusplus
extern “C” {
#endif
// All functions declared here.
#ifdef __cplusplus
}
#endif
__cplusplus macro is defined by every C++ compiler, and hence the “extern C” block shall become visible only when compiled using a C++ compiler.
The interesting fact is that C++ standard doesn’t mention how the name mangling should be done or what algorithm to follow. Its left entirely upto the compiler designers.
Best Obscure JavaScript Posts of 2013
Since I have been very busy lately with a product launch coinciding with the holidays, I thought it would be a good idea to select what I think have been the five best posts of my blog for this year (in chronological order);
Recursion In JavaScript Without Making A Recursive Call Sounds impossible, but it actually is not.
Multiple Return Values in JavaScript The Easy Way With a tiny bit of functional programming, multiple return values are much easier to deal with. Something I actually use whenever I have multiple returns now.
Overloading Functions In JavaScript Overloading functions in present in many lower level languages than JavaScript, but with a little work, is actually possible in JavaScript.
Erlang In JavaScript This takes a little bit of Erlang code and implements a close JavaScript equivalent. It really shows how flexible JavaScript can be.
Batman, Arrays And JavaScript Collide Exploiting array instantiation, NaN and Array.slice to produce a funny result.
Overloading Functions in JavaScript
My previous post used function overloading by different sent in statements to a when function, appended at the end of every function that needed to be overridden. This same approach applies to making functions overridable by the number of defined arguments:
var sum = (function sum(a){ return a; }).overload(); var sum = (function sum(a, b){ return a + b; }).overload(); console.log(sum(1));// 1 console.log(sum(1, 2));// 3
Basically, the implementation is nearly the same but simpler. Instead of having to evaluate a set of statements to determine which callback applies I just check the number of arguments sent in and call the corresponding callback. More details in the comments:
Function.prototype.overload = function overload(){ // Like last time a naive approach to get the function name for example purposes var funcString = this.toString(); var funcName = funcString.split('function')[1].split('(')[0].replace(' ', ''); // Same storage approach as last time but I will use the overload function to // store values instead of an external variable. Arguably this is cleaner. // Remember 'this' is the original callback and this.length gives the length of // arguments of a function. overload[funcName] = overload[funcName] || []; overload[funcName][this.length] = this; // Run the appropriate argument length constructor return function(){ if (overload[funcName][arguments.length]) return overload[funcName][arguments.length].apply(this, arguments); throw('Error: There is no defined version of function ' + funcName + ' for ' + arguments.length + ' arguments.'); }; }
Also, I should mention, although I take a very different approach, I was initially inspired by an old blog post from John Resig.
Github Location: https://github.com/Jacob-Friesen/obscurejs/blob/master/2013/overloadingFunctions.js