TAILIEUCHUNG - A Complete Guide to Programming in C++ part 46

A Complete Guide to Programming in C++ part 46. 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. | OVERLOADING SHIFT-OPERATORS FOR I O 429 When outputting a Euro class object price on screen the following output statement causes a compiler error Example cout price cout can only send objects to standard output if an output function has been defined for the type in question and this of course is not the case for user-defined classes. However the compiler can process the previous statement if it can locate a suitable operator function operator . To allow for the previous statement you therefore need to define a corresponding function. Overloading the Operator In the previous example the left operand of is the object cout which belongs to the ostream class. Since the standard class ostream should not be modified it is necessary to define a global operator function with two parameters. The right operand is a Euro class object. Thus the following prototype applies for the operator function Prototype ostream operator ostream os const Euro e The return value of the operator function is a reference to ostream. This allows for normal concatenation of operators. Example cout price endl Overloading the Operator The operator is overloaded for input to allow for the following statements. Example cout Enter the price in Euros cin price The second statement causes the following call operator cin price As cin is an object of the standard istream class the first parameter of the operator function is declared as a reference to istream. The second parameter is again a reference to Euro. The header file contains only the declarations of and . To allow these functions to access the private members of the Euro class you can add a friend declaration within the class. However this is not necessary for the current example. 430 CHAPTER 19 OVERLOADING OPERATORS EXERCISES Prefix and postfix increment To distinguish the postfix increment operator from the prefix increment operator the postfix operator function has an additional parameter of type int. u L L Expression Operator Function

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.