tailieunhanh - A Complete Guide to Programming in C++ part 45

A Complete Guide to Programming in C++ part 45. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | USING OVERLOADED OPERATORS 419 Calling Operator Functions The following expressions are valid for the operators in the Euro class. Example Euro wholesale 15 30 retail profit 7 50 discount 1 75 retail wholesale profit Call profit retail - discount Call discount retail Euro Call Euro These expressions contain only Euro type objects for which operator functions have been defined. However you can also add or subtract int or double types. This is made possible by the Euro constructors which create Euro objects from int or double types. This allows a function that expects a Euro value as argument to process int or double values. As the program opposite shows the statement Example retail is valid. The compiler attempts to locate an operator function that is defined for both the Euro object and the double type for . Since there is no operator function with these characteristics the compiler converts the double value to Euro and calls the existing operator function for euros. Symmetry of Operands The available constructors also allow you to call the operator functions of and - with int or double type arguments. Example retail wholesale 10 ok wholesale retail - ok The first statement is equivalent to retail Euro 10 But the following statement is invalid Example retail 10 wholesale wrong Since the operator function was defined as a method the left operand must be a class object. Thus you cannot simply exchange the operands of the operator . However if you want to convert both operands you will need global definitions for the operator functions. 420 CHAPTER 19 OVERLOADING OPERATORS GLOBAL OPERATOR FUNCTIONS Operators overloadable by methods only The operator functions of the following operators have to be methods Operators Meaning Assignment operator Function call Subscript operator - Class member access NOTE The function call operator is used to represent operations for objects like .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.