TAILIEUCHUNG - Object Oriented Programming (8)

CSC241: Object Oriented Programming. Lecture No 08. 1 Previous Lecture.•. Abstract data type (ADT).•. Container classes.•. Proxy classes.•. Operator overloading. 2 Today’s Lecture.•. Operator overloading.•. Overloading unary operator. o ++. o --.•. Argument of overloaded function. – Post increment. 3 Operator overloading.•. For example, statements like. – (d1, d2);. or the similar but equally obscure. – d3 = (d2);.•. can be changed to the much more readable. – d3 = d1 + d2;. 4 Examples of c++ overloaded operator.•. Addition + and subtraction – operator. § +, – operators perform differently, depending on their. context in integer arithmetic, floating-point arithmetic. and pointer arithmetic•. Stream insertion (>) operator. (cout>). § > is used both as the stream extraction operator and. as the bitwise right-shift operator. § > are overloaded in the C++ Standard Library. std 5Syntax of overloaded operator function.•. An operator is overloaded by writing a non-static. member function definition.•. Function name becomes the keyword operator. followed by the symbol for the operator being. overloaded. – . operator + () { . }.•. Function name operator+ would be used to. overload the addition operator (+).•. Assignment operator (=) may be used with every. class to perform member wise assignment of the. data members 6 Precedence, Associativity and No. of. Operands.•. Precedence means which operator to solve first. (+, -, *, /, = ).•. The precedence of an operator cannot be. changed by overloading.•. The associativity of an operator cannot be. changed by overloading.•. Overloaded unary operators (++, --) remain. unary operators.•. overloaded binary operators remain binary. operators. 7 Creating New Operators.•. It is not possible to create new operators.•. only existing operators can be overloaded. – . ** can be used for exponential in some. programming languages. – ** is not in the list od existing operator so it cannot be. overloaded.•. Attempting to create new operators via operator. overloading is a syntax error 8 Operators for Fundamental Types.•. The meaning of how an operator works on. fundamental types cannot be changed by. operator overloading.•. For example, programmer cannot change the. meaning of how + adds two integers.•. Operator overloading works only with. – objects of user-defined types or. – a mixture of an object of a user-defined type and an. object of a fundamental type. 9 Examples.•. c1++; or c1--;. – Overloading ++ and -- unary operator.•. dist3 = dist1 + dist2;. – Overloading assignment and addition binary operator. – It does not mean that += is also overloaded for. Distance objects.•. dist1+= dist2; or dist1-=dist2. 10 Overloading Unary Operators•. Examples of unary operators are the increment. and decrement operators ++ and --, and the. unary minus, as in -33.•. Counter class example. –

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.