Absolute C++ (4th Edition) part 32

Absolute C++ (4th Edition) part 32. 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. . | 314 Operator Overloading Friends and References and You can overload the and operators in ways similar to how we overloaded the negation operator in Display . If you overload the and operators following the example of the minus sign - in Display then the overloading definition will apply to the operator when it is used in prefix position as in x and x. Later in this chapter we will discuss overloading and more fully and will then explain how to overload these operators for use in the postfix position. OVERLOADING AS MEMBER FUNCTIONS In Display we overloaded operators as standalone functions defined outside the class. It is also possible to overload an operator as a member operator member function . This is illustrated in Display . Note that when a binary operator is overloaded as a member operator there is only one parameter not two. The calling object serves as the first parameter. For example consider the following code Money cost 1 50 tax 0 15 total total cost tax When is overloaded as a member operator then in the expression cost tax the variable cost is the calling object and tax is the single argument to . The definition of the member operator is given in Display . Notice the following line from that definition int allCentsl cents dollars 100 The expressions cents and dollars are member variables of the calling object which in this case is the first operand. If this definition is applied to cost tax then cents means and dollars means . Note that since the first operand is the calling object you should in most cases add the const modifier to the end of the operator declaration and to the end of the operator definition. Whenever the operator invocation does not change the calling object which is the first operand good style dictates that you add the const to the end of the operator declaration and to the end of the operator definition as illustrated in Display . Overloading an operator as a member variable can seem .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã 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.