What is dynamic binding ? Explain with the help of an example.
Dynamic binding, also known as late binding or runtime polymorphism, refers to the process in which the determination of which function or method to invoke is made during the program's execution, rather than at compile time. It enables the selection of the appropriate function implementation based on the actual object type or instance involved in the call.
In C++, dynamic binding is primarily achieved through the use of virtual functions and inheritance. When a base class contains virtual functions, and these functions are overridden in derived classes, the decision of which function to call is deferred until runtime.
Here's an example to illustrate dynamic binding:
```cpp
#include <iostream>
class Shape {
public:
// Virtual function
virtual void draw() {
std::cout << "Drawing a shape." << std::endl;
______ __________ _______ ________ ________.
_________ ___ _____ ____ ___ ________ ______.
___ ________ __________ _______ __________.
___ __________ _______ _________ _________ _______ ______ _________ ___ ______.
____ __________ ____ ______ ___ _____ _____.
_______ ________ _______ ________ ______ _____ _____ ____.
_______ _______ ______ _________ _________.
_____ ________ __________ __________ _________ ___ _______ ___ ______ ___.
_________ __________ ____ ___ ____ __________ ____ ______.
___ __________ __________ ___ _________ _______ _____ ________.
____ ______ ______ ___ ______ __________ ___ _______ ____ _____ ___.
_________ ___ _______ ___ ________ _______ ________ _________ _______ __________.
________ _____ ________ ________ _________ ____ _________.
______ ____ _________ ____ ____ _______ ________ _________ ______ __________ __________ ____.
_______ _________ _________ _____ ____ _______ ___ ____ ________ __________.
______ ____ ________ _________ ________ __________ __________.
______ ____ ___ _____ __________ ______ ______ _____ ________ ___.
______ _____ _________ ________ _______ _______ _________ ________ __________ ______ _________.
___ _____ _________ _____ ______ _________ __________ ___ _______ ______.
_____ ______ _______ _________ _________ ___ __________ ______ _______.
_____ ________ ____ _________ ______.
___ ________ _____ ______ ______ ________ _______.
____ __________ _______ ___ ______ ______ ___ _________ _____ ______.
_____ ___ _____ __________ _______ ______ _____ ________ _________ ______.
_____ _______ _______ ___ ___ ___ ________.
- Solved Assignments
- Study Notes
- Guess Papers
- Question Papers
✓ Unlock up to 60% of the answer
✓ No payment required