OOPs Dynamic Binding Program Using C++

In this blog we will learn how to Create C++ programs to find squares and cubes. We will also learn what dynamic binding in OOPS is.

Dynamic binding refers to linking a procedure call to code that will execute only once. The code associated with the procedure is not known until the program is executed, which is also known as late binding.

Examples

  1. //Dynamic Binding program in c++  
  2. #include < iostream >   
  3. using namespace std;  
  4. int Square(int x) //Square is =x*x;  
  5. {  
  6.     return x * x;  
  7. }  
  8. int Cube(int x) // Cube is =x*x*x;  
  9. {  
  10.     return x * x * x;  
  11. }  
  12. main() {  
  13.     int x = 100;  
  14.     int choice;  
  15.     do {  
  16.         cout << "Enter 0 for Square value, 1 for Cube value :\n";  
  17.         cin >> choice;  
  18.     }  
  19.     while (choice < 0 || choice > 1);  
  20.     int( * ptr)(int);  
  21.     switch (choice) {  
  22.         case 0:  
  23.             ptr = Square;  
  24.             break;  
  25.         case 1:  
  26.             ptr = Cube;  
  27.             break;  
  28.     }  
  29.     cout << "The result is :" << ptr(x);  
  30.     return 0;  
  31. }  
Below Screenshot have program and output

output

Thank you for reading; I hope this blog washelpful for you. If you have any questions or feedback share with me.
Ebook Download
View all
Learn
View all