Polymorphism and virtual functions
Polymorphism
The word Polymorphism is a combination of two words i.e poly and morphism. Poly means many and morphism mean form. In object-oriented-programming, Polymorphism is the cability of objects of different types two reply to the functions of the same name.The user not have to know the exact type of the object in advance. The action of the object can be apply at the run time. It is called Late-Binding or Dynamic binding. Polymorphism is implemented by using virtual functions.
Pointer to objects
A pointer can also refer to an object of a class. The member of an object can be accessed through pointers by using the symbol - >. The symbol is known as member access operator.
Syntax
The syntax of referencing an object with pointer is as follow:
ptr-> member.
Virtual Function
Virtual means existing effect but not in reality. A type of function that appears to existing some part of a program but does not exist really is called virtual function. Virtual functions are used to apply polymorphism. They enable the user to execute completly different by same function call.
A virtual function is defined in the parent class and can overridden is child classes. It is defined by using the key word virtual.
Early Binding
The assignment of types to variables and expressions at compilation time is known as early binding.It is also called static binding. The early binding occurs when every thing require to call a function is known at compile time. Early binding enables the the compiler to know exactly which function will be called when a certain statement is executed.
When a program is compiled, the compiler cheaks the functions call and decides which function is to be executed. This process takes place during compilation process in normal programs with functions.It is an example of early binding.
Late Binding
The assignment of types to variables and expression at execution time is known as late binding. It is also known as dynamic binding. The late binding occurs when some information to call a function is decided at execution time. The compiler does not know at compile time which function will be executed.It provide more flexibility.
The use of virtual function to implement polymorphism is an example of late binding. In virtual functions, the compiler does not know at compile time which object is reffered by the pointer. The compiler executes the function depending on the contents of the pointer rather then the type of compiler.
0 comments:
Post a Comment