Monday, 6 May 2013

Early and Late Binding:

Early and Late Binding: 
When a program is compiled, the compiler converts each statement in your C++ program into one or more lines of machine language. Each line of machine language is given it’s own unique sequential address. This is no different for functions — when a function is encountered, it is converted into machine language and given the next available address. Thus, each function ends up with a unique machine language address. 

Binding refers to the process that is used to convert identifiers (such as variable and function names) into machine language addresses. Although binding is used for both variables and functions. 


Early Binding:
Early binding (also called static binding) means the compiler is able to directly associate the identifier name (such as a function or variable name) with a machine address.

When the compiler encounters a function call, it replaces the function call with a machine language instruction that tells the CPU to jump to the address of the function.

Those decisions taken at compile time saves execution time, but such programs are not flexible.

Function overloading and operator overloading are example of early binding.

Late Binding:
In some programs, it is not possible to know which function will be called until runtime (when the program is run). This is known as late binding (or dynamic binding). 
In C++, one way to get late binding is to use function pointers. 

Those decisions taken at run time are taking extra execution time, but provide greater flexibility to the user.

Virtual function is an example of late binding.

No comments:

Post a Comment