fork download
  1. //start//
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. class IPay
  8. {
  9. private:
  10.  
  11. public:
  12.  
  13. virtual void input_money(int a) = 0;
  14.  
  15. virtual void print_receipt() = 0;
  16.  
  17. };
  18.  
  19. class CashPay //: public IPay
  20. {
  21. private:
  22.  
  23. int money_recieved;
  24.  
  25. public:
  26.  
  27. CashPay() {};
  28.  
  29. void input_money(int a) //override
  30. {
  31. this->money_recieved = a;
  32.  
  33. }
  34.  
  35. void print_receipt() //override
  36. {
  37. cout << "You paid " << this->money_recieved << " bucks in cash!" << endl;
  38. }
  39. };
  40.  
  41. class CardPay //: public IPay
  42. {
  43. private:
  44.  
  45. int money_recieved;
  46.  
  47. public:
  48.  
  49. CardPay() {};
  50.  
  51. void input_money(int a) //override
  52. {
  53. this->money_recieved = a;
  54. }
  55.  
  56. void print_receipt() //override
  57. {
  58. cout << "\nProcessing...\n" << endl;
  59. cout << "You paid " << this->money_recieved << " bucks by card!" << endl;
  60. }
  61. };
  62.  
  63. // context object
  64. class PaymentPoint
  65. {
  66. private:
  67. IPay * payment_method;
  68.  
  69. public:
  70.  
  71. PaymentPoint() {};
  72.  
  73. void set_payment_method(IPay *payment_method)
  74. {
  75. this->payment_method = payment_method;
  76. }
  77.  
  78. void pay(int a)
  79. {
  80. payment_method->input_money(a);
  81.  
  82. payment_method->print_receipt();
  83. }
  84. };
  85.  
  86.  
  87. int main()
  88. {
  89. PaymentPoint payment_point;
  90. CashPay *cash_pay = new CashPay();
  91. CardPay card_pay();
  92.  
  93. CardPay *card_pay1 = new CardPay();
  94.  
  95. string payment_method;
  96. cout << "Enter payment method: ";
  97. cin >> payment_method;
  98.  
  99. if (payment_method == "cash")
  100. {
  101. payment_point.set_payment_method(cash_pay);
  102. payment_point.pay(150);
  103. }
  104. else if (payment_method == "card")
  105. {
  106. payment_point.set_payment_method(card_pay1);
  107. payment_point.pay(1337);
  108. }
  109.  
  110. system("pause");
  111. }
  112.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:101:44: error: no matching function for call to ‘PaymentPoint::set_payment_method(CashPay*&)’
   payment_point.set_payment_method(cash_pay);
                                            ^
prog.cpp:73:7: note: candidate: ‘void PaymentPoint::set_payment_method(IPay*)’
  void set_payment_method(IPay *payment_method)
       ^~~~~~~~~~~~~~~~~~
prog.cpp:73:7: note:   no known conversion for argument 1 from ‘CashPay*’ to ‘IPay*’
prog.cpp:106:45: error: no matching function for call to ‘PaymentPoint::set_payment_method(CardPay*&)’
   payment_point.set_payment_method(card_pay1);
                                             ^
prog.cpp:73:7: note: candidate: ‘void PaymentPoint::set_payment_method(IPay*)’
  void set_payment_method(IPay *payment_method)
       ^~~~~~~~~~~~~~~~~~
prog.cpp:73:7: note:   no known conversion for argument 1 from ‘CardPay*’ to ‘IPay*’
prog.cpp:110:8: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result [-Wunused-result]
  system("pause");
  ~~~~~~^~~~~~~~~
stdout
Standard output is empty