Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Absolute C++ (4th Edition) part 35. KEY BENEFIT: C++ programming concepts and techniques are presented in a straightforward style using understandable language and code. KEY TOPICS: C++ Basics; Flow of Control; Function Basics; Parameters and Overloading; Arrays; Structures and Classes; Constructors; Operator Overloading, Friends, and References; Strings; Pointers and Dynamic Arrays; Separate Compilation and Namespaces; Streams and File I/O; Recursion; Inheritance; Polymorphism and Virtual Functions; Templates; Linked Data Structures; Exception Handling; Standard Template Library; Patterns and UML. MARKET: Useful for both beginning and intermediate C++ programmers. . | 344 Operator Overloading Friends and References 7. Uses cstdlib const Money operator - const Money amountl const Money amount2 int allCentsl amountl.cents amount1.dollars 100 int allCents2 amount2.cents amount2.dollars 100 int diffAllCents allCentsl - allCents2 int absAllCents abs diffAllCents int finalDollars absAllCents 100 int finalCents absAllCents 100 if diffAllCents 0 finalDollars -finalDollars finalCents -finalCents return Money finalDollars finalCents 8. Add the following declaration and function definition friend bool operator const Money amount1 const Money amount2 bool operator const Money amount1 const Money amount2 return amount1.dollars amount2.dollars amount1.dollars amount2.dollars amount1.cents amount2.cents 9. To understand why it is not circular you need to think about the basic message of overloading A single function or operator name can have two or more definitions. That means that two or more different operators or functions can share a single name. In the line outputstream - the operator is the name of an operator defined in the library iostream to be used when the second argument is a quoted string. The operator named that we define in Display 8.5 is a different operator that works when the second argument is of type Money. 10. If and are to work as we want then the first operand first argument must be cout or cin or some file I O stream . But if we want to overload the operators as members of say the class Money then the first operand would have to be the calling object and so would have to be of type Money and that is not what we want. Programming Projects 345 11. Uses iostream istreamS operator istream inputstream Percent aPercent char percentSign inputstream aPercent.value inputstream percentSign Discards the sign. return inputstream Uses iostream ostreamS operator ostream outputstream const Percents aPercent outputstream aPercent.value return outputstream 12. It is legal but the meaning is not what you might want. a increases the value