daily pastebin goal
85%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * "Subject"
  3.  */
  4. class IMath {
  5. public:
  6.     virtual double add(double x, double y) = 0;
  7.     virtual double sub(double x, double y) = 0;
  8.     virtual double mul(double x, double y) = 0;
  9.     virtual double div(double x, double y) = 0;
  10. };
  11.  
  12. /**
  13.  * "Real Subject"
  14.  */
  15. class Math : public IMath {
  16. public:
  17.     double add(double x, double y) {
  18.         return x + y;
  19.     }
  20.  
  21.     double sub(double x, double y) {
  22.         return x - y;
  23.     }
  24.  
  25.     double mul(double x, double y) {
  26.         return x * y;
  27.     }
  28.  
  29.     double div(double x, double y) {
  30.         return x / y;
  31.     }
  32. };
  33.  
  34. /**
  35.  * "Proxy Object"
  36.  */
  37. class MathProxy : public IMath {
  38. public:
  39.     double add(double x, double y) {
  40.         return math.add(x, y);
  41.     }
  42.  
  43.     double sub(double x, double y) {
  44.         return math.sub(x, y);
  45.     }
  46.  
  47.     double mul(double x, double y) {
  48.         return math.mul(x, y);
  49.     }
  50.  
  51.     double div(double x, double y) {
  52.         return math.div(x, y);
  53.     }
  54.  
  55. private:
  56.     Math math;
  57. };
  58.  
  59. #include <iostream>
  60.  
  61. using std::cout;
  62. using std::endl;
  63.  
  64. int main() {
  65.  
  66.     // Create math proxy
  67.     MathProxy p;
  68.  
  69.     // Do the math
  70.     cout << "4 + 2 = " << p.add(4, 2) << endl;
  71.     cout << "4 - 2 = " << p.sub(4, 2) << endl;
  72.     cout << "4 * 2 = " << p.mul(4, 2) << endl;
  73.     cout << "4 / 2 = " << p.div(4, 2) << endl;
  74.  
  75.     return 0;
  76. }
RAW Paste Data
Top