Advertisement
aircampro

method override and pointer to function

Nov 17th, 2021
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <cassert>
  3. #include <stdexcept>
  4.  
  5. using namespace std;
  6.  
  7. // function override example
  8. //
  9. struct A
  10. {
  11.     int foo() { return 1; }
  12.     // virtual function method is overriden with the override declaration in the next structure.
  13.     virtual int bar() { return 1; }
  14.     virtual int divy(int a, int b) { return a/b; }
  15. };
  16.  
  17. struct B : A
  18. {
  19.     int foo() { return 2; }
  20.     // override declaration makes it the master
  21.     int bar() override { return 2; }
  22.     int divy(int a, int b) override { return b/(2*a); }
  23. };
  24.  
  25. // pointer to function example
  26. //
  27. int arithmetic(int, int, int (*)(int, int));
  28.     // Take 3 arguments, 2 int's and a function pointer
  29.     //   int (*)(int, int), which takes two int's and return an int
  30. int add(int, int);
  31. int sub(int, int);
  32.  
  33. int add(int n1, int n2) { return n1 + n2; }
  34. int sub(int n1, int n2) { return n1 - n2; }
  35.  
  36. int arithmetic(int n1, int n2, int (*operation) (int, int)) {
  37.    return (*operation)(n1, n2);
  38. }
  39.  
  40. int main() {
  41.     B b;
  42.     A *a = &b;
  43.     // it selects the A method hence value
  44.     int v1 = a->foo();
  45.     // override here selects the B method and hence its value
  46.     //
  47.     int v2 = a->bar();
  48.     std::cout << " value v1 " << v1 << " value v2 " << v2 << std::endl;
  49.     // add
  50.     std::cout << arithmetic(v1, v2, add) << std::endl;
  51.     // subtract
  52.     std::cout << arithmetic(v1, v2, sub) << std::endl;
  53.     assert(v1 == 1);
  54.     assert(v2 == 2);
  55.  
  56.     A a2;
  57.     a = &a2;
  58.     // now its like type A only as it we set it to the address of A2
  59.     v1 = a->foo();
  60.     v2 = a->bar();
  61.     std::cout << " value v1 " << v1 << " value v2 " << v2 << std::endl;    
  62.     assert(v1 == 1);
  63.     assert(v2 == 1);
  64.  
  65.     // add
  66.     std::cout << arithmetic(v1, v2, add) << std::endl;
  67.     // subtract
  68.     std::cout << arithmetic(v1, v2, sub) << std::endl;
  69.  
  70.     a = &b;
  71.  
  72.     // now we have addressed B but the return variable is a const declaration
  73.     try {
  74.       int v3 = a->divy(v2,0);
  75.       std::cout << "v3 " << v3 << std::endl;
  76.     } catch (const std::exception& e) { std::cout << e.what() << std::endl; }
  77.     try {
  78.        throw 20;                       // before we divide bv 0 throw it   
  79.        int v4 = a->divy(0,v2);
  80.        std::cout << "v4 " << v4 << std::endl;
  81.     }
  82.     catch (int e)
  83.     {
  84.        cout << "divide by zero found first. " << e << '\n';
  85.     }
  86.     catch (const std::exception& e) { std::cout << e.what() << std::endl; }
  87.  
  88.  
  89.     a = &a2;
  90.     try {
  91.       int v3 = a->divy(0,v2);
  92.       std::cout << "v3 " << v3 << std::endl;
  93.     } catch (const std::exception& e) { std::cout << e.what() << std::endl; }
  94.     try {
  95.       int v4 = a->divy(v2,0);
  96.       throw std::runtime_error("Div zero stopped");
  97.       std::cout << "v4 " << v4 << std::endl;
  98.     } catch (const std::exception& e) { std::cout << e.what() << std::endl; }
  99.  
  100.     std::cout << " ended " << std::endl;
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement