fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. class Sparschwein{
  6.  
  7. int inhalt;
  8. int muenzen[3] {0, 0, 0};
  9.  
  10. public:
  11. Sparschwein();
  12. Sparschwein(int coin);
  13. Sparschwein(int firstCoin, int secondCoin, int thirdCoin);
  14. void print();
  15. void einwerfen(int firstCoin, int secondCoind, int thirdCoin);
  16. void leeren();
  17.  
  18. private:
  19. void einwerfen(int coin);
  20.  
  21. };
  22.  
  23. Sparschwein::Sparschwein()
  24. {
  25. this->inhalt = 0;
  26. }
  27.  
  28. Sparschwein::Sparschwein(int coin)
  29. {
  30. this->einwerfen(coin);
  31. }
  32.  
  33. Sparschwein::Sparschwein(int firstCoin, int secondCoin, int thirdCoin)
  34. {
  35. this->einwerfen(firstCoin, secondCoin, thirdCoin);
  36. }
  37.  
  38. void Sparschwein::print()
  39. {
  40. cout << this->muenzen[0] << "x 1 cent +" << this->muenzen[1] << "x 5 cent + " << this->muenzen[2] << "x 10 cent = " << this->inhalt << " cent" << endl;
  41. }
  42.  
  43. void Sparschwein::einwerfen(int firstCoin, int secondCoin, int thirdCoin) {
  44. this->einwerfen(firstCoin);
  45. this->einwerfen(secondCoin);
  46. this->einwerfen(thirdCoin);
  47. }
  48.  
  49. void Sparschwein::einwerfen(int coin)
  50. {
  51. if(coin != 1 && coin != 5 && coin != 10)
  52. throw std::range_error("Münzwert ist ungültig.");
  53.  
  54. this->inhalt += coin;
  55. if(coin == 1)
  56. this->muenzen[0] += 1;
  57. else if(coin == 5)
  58. this->muenzen[1] += 1;
  59. else if(coin == 10)
  60. this->muenzen[2] += 1;
  61. }
  62.  
  63. void Sparschwein::leeren() {
  64. this->inhalt = 0;
  65. this->muenzen[0] = 0;
  66. this->muenzen[1] = 0;
  67. this->muenzen[2] = 0;
  68. }
  69.  
  70.  
  71. int main () {
  72.  
  73. Sparschwein a;
  74. a.print();
  75.  
  76. Sparschwein b = 10;
  77. b.print();
  78.  
  79. Sparschwein c(47, 11, 42);
  80. c.print();
  81.  
  82. Sparschwein d;
  83. d.einwerfen(0, 8, 15);
  84. d.print();
  85.  
  86. d.leeren();
  87. d.print();
  88.  
  89.  
  90. return 0;
  91. }
  92.  
  93.  
Runtime error #stdin #stdout #stderr 0s 3460KB
stdin
Standard input is empty
stdout
0x 1 cent +0x 5 cent + 0x 10 cent = 0 cent
0x 1 cent +0x 5 cent + 1x 10 cent = 134514842 cent
stderr
terminate called after throwing an instance of 'std::range_error'
  what():  Münzwert ist ungültig.