Sunday, 15 April 2012

How static typing is achieved by dynamic binding in C++? | C++

Pointers to base class objects can also be used to point to derived class objects (although you cannot reference members in the derived class that are not in the base class when you use a base class pointer). As one pointer can point polymorphism. For example, a fruit* class that actually points to an Apple object.
There are two types of pointers:
1. Static pointer means the actual type of pointer of the base class; fruit, in this case
2. Dynamic pointer means the type of the object to be pointed; apple, in this case
Static typing ensures that the execution at run-time will only get values that are of the right type for the program. This typing detects error in the early stages of a program, minimizing the possibility of error occurrence at the run time. This helps in improving the performance of the program.
Dynamic binding ensures that the function calls are resolved at run time; thereby, providing users the flexibility to alter a call without having to modify the code. For a programmer, efficiency and performance would probably be a primary concern but to a user, flexibility and maintainability are much more important. Dynamic binding represents dynamic polymorphism. Dynamic binding is based on overriding principles that are based on inheritance. The dynamic binding is used when a function call is matched with the correct function definition at run time. You can declare a function with the virtual keyword, if you want the compiler to use dynamic binding for the specific function.

No comments: