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
-
- #include < iostream >
- using namespace std;
- int Square(int x)
- {
- return x * x;
- }
- int Cube(int x)
- {
- return x * x * x;
- }
- main() {
- int x = 100;
- int choice;
- do {
- cout << "Enter 0 for Square value, 1 for Cube value :\n";
- cin >> choice;
- }
- while (choice < 0 || choice > 1);
- int( * ptr)(int);
- switch (choice) {
- case 0:
- ptr = Square;
- break;
- case 1:
- ptr = Cube;
- break;
- }
- cout << "The result is :" << ptr(x);
- return 0;
- }
Below Screenshot have program and output
Thank you for reading; I hope this blog washelpful for you. If you have any questions or feedback share with me.