fork download
  1. //DISPLAY 11.8 Overloading << and >>
  2. //Program to demonstrate the class Money.
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cstdlib>
  6. #include <cctype>
  7. using namespace std;
  8.  
  9. //Class for amounts of money in U.S. currency.
  10. class Money
  11. {
  12. public:
  13. friend Money operator +(const Money& amount1, const Money& amount2);
  14.  
  15. friend Money operator -(const Money& amount1, const Money& amount2);
  16.  
  17. friend Money operator -(const Money& amount);
  18.  
  19. friend bool operator ==(const Money& amount1, const Money& amount2);
  20.  
  21. friend bool operator < (const Money& amount1, const Money& amount2);
  22. //Returns true if amount1 is less than amount2; false otherwise.
  23.  
  24.  
  25. Money(long dollars, int cents);
  26.  
  27. Money(long dollars);
  28.  
  29. Money( );
  30.  
  31. double get_value( ) const;
  32.  
  33. friend istream& operator >>(istream& ins, Money& amount);
  34. //Overloads the >> operator so it can be used to input values of type Money.
  35. //Notation for inputting negative amounts is as in -$100.00.
  36. //Precondition: If ins is a file input stream, then ins has already been
  37. //connected to a file.
  38.  
  39. friend ostream& operator <<(ostream& outs, const Money& amount);
  40. //Overloads the << operator so it can be used to output values of type Money.
  41. //Precedes each output value of type Money with a dollar sign.
  42. //Precondition: If outs is a file output stream,
  43. //then outs has already been connected to a file.
  44. private:
  45. long all_cents;
  46. };
  47. int digit_to_int(char c);
  48. //Used in the definition of the overloaded input operator >>.
  49. //Precondition: c is one of the digits '0' through '9'.
  50. //Returns the integer for the digit; for example, digit_to_int('3') returns 3.
  51.  
  52. int main( )
  53. {
  54. Money amount1, amount2;
  55.  
  56. cout << "input first amount:";
  57. cin >> amount1;
  58. cout << "input second amount:";
  59. cin >> amount2;
  60.  
  61. cout << "You entered " << amount1 << " and " << amount2 << endl;
  62. cout << "Negated " << -amount1 << " and " << -amount2 << endl;
  63. cout << "Sum " << amount1 + amount2 << endl;
  64. cout << "Difference " << amount1 - amount2 << endl;
  65.  
  66. if (amount1 < amount2) {
  67. cout << amount1 << " < " << amount2;
  68. } else if (amount2 < amount1) {
  69. cout << amount1 << " > " << amount2;
  70. } else {
  71. cout << amount1 << " = " << amount2;
  72. }
  73. cout << endl;
  74.  
  75. return 0;
  76. }
  77.  
  78. Money operator +(const Money& amount1, const Money& amount2)
  79. {
  80. Money temp;
  81. temp.all_cents = amount1.all_cents + amount2.all_cents;
  82. return temp;
  83. }
  84.  
  85. bool operator ==(const Money& amount1, const Money& amount2)
  86. {
  87. return (amount1.all_cents == amount2.all_cents);
  88. }
  89.  
  90. Money operator -(const Money& amount1, const Money& amount2)
  91. {
  92. Money temp;
  93. temp.all_cents = amount1.all_cents - amount2.all_cents;
  94. return temp;
  95. }
  96.  
  97. Money operator -(const Money& amount)
  98. {
  99. Money temp;
  100. temp.all_cents = -amount.all_cents;
  101. return temp;
  102. }
  103.  
  104. bool operator < (const Money& amount1, const Money& amount2){
  105. return amount1.all_cents < amount2.all_cents;
  106. }
  107.  
  108. //Uses iostream, cctype, cstdlib:
  109. istream& operator >>(istream& ins, Money& amount)
  110. {
  111. char one_char, decimal_point,
  112. digit1, digit2; //digits for the amount of cents
  113. long dollars;
  114. int cents;
  115. bool negative;//set to true if input is negative.
  116.  
  117. ins >> one_char;
  118. if (one_char == '-')
  119. {
  120. negative = true;
  121. ins >> one_char; //read '$'
  122. }
  123. else
  124. negative = false;
  125. //if input is legal, then one_char == '$'
  126.  
  127. ins >> dollars >> decimal_point >> digit1 >> digit2;
  128.  
  129. if ( one_char != '$' || decimal_point != '.'
  130. || !isdigit(digit1) || !isdigit(digit2) )
  131. {
  132. cout << "Error illegal form for money input\n";
  133. exit(1);
  134. }
  135.  
  136. cents = digit_to_int(digit1)*10 + digit_to_int(digit2);
  137.  
  138. amount.all_cents = dollars*100 + cents;
  139. if (negative)
  140. amount.all_cents = -amount.all_cents;
  141.  
  142.  
  143. return ins;
  144. }
  145.  
  146. int digit_to_int(char c)
  147. {
  148. return ( static_cast<int>(c) - static_cast<int>('0') );
  149. }
  150.  
  151. //Uses cstdlib and iostream:
  152. ostream& operator <<(ostream& outs, const Money& amount)
  153. {
  154. long positive_cents, dollars, cents;
  155. positive_cents = labs(amount.all_cents);
  156. dollars = positive_cents/100;
  157. cents = positive_cents%100;
  158.  
  159. if (amount.all_cents < 0)
  160. outs << "-$" << dollars << '.';
  161. else
  162. outs << "$" << dollars << '.';
  163.  
  164. if (cents < 10)
  165. outs << '0';
  166. outs << cents;
  167.  
  168. return outs;
  169. }
  170.  
  171. Money::Money(long dollars, int cents)
  172. {
  173. if(dollars*cents < 0) //If one is negative and one is positive
  174.  
  175.  
  176. {
  177. cout << "Illegal values for dollars and cents.\n";
  178. exit(1);
  179. }
  180. all_cents = dollars*100 + cents;
  181. }
  182.  
  183. Money::Money(long dollars) : all_cents(dollars*100)
  184. {
  185. //Body intentionally blank.
  186. }
  187.  
  188. Money::Money( ) : all_cents(0)
  189. {
  190. //Body intentionally blank.
  191. }
  192.  
Success #stdin #stdout 0s 3420KB
stdin
$12.54
$10.44
stdout
input first amount:input second amount:You entered $12.54 and $10.44
Negated -$12.54 and -$10.44
Sum $22.98
Difference $2.10
$12.54 > $10.44