TAILIEUCHUNG - Object Oriented Programming (10)

CSC241: Object Oriented Programming. Lecture No 10. 1 Previous Lecture.•. Example : Distance class. – overloading ++ and –. •. ++ add 1 in feet and 1 in inches,. •. -- subtract 1 from feet and 1 from inches. – Prefix and postfix.•. Overloading binary operator. d3= d1+d2;. Distance operator + (Distance d2) {. float i = inches + ;. int f = feet + ;. return Distance(f, i);. } 2 Today’s Lecture.•. Overloading binary operator. –. +, – and = operator for ThreeD class. –. < operator for distance class. –. += operator for distance class. –. Subscript operator [] for Safe array class.•. Data conversion. 3 Overloading -, + and = ThreeD { Operators. // Overload subtraction int x, y, z; ThreeD ThreeD::operator-. (ThreeD op2) {.public: ThreeD temp;. ThreeD() { x = y = z = 0; } = x - ;. ThreeD(int i, int j, int k) = y - ;. { x = i; y = j; z = k; } = z - ;. ThreeD operator+ return temp;. }.(ThreeD op2); // Overload + ThreeD operator=(ThreeD op ThreeD ThreeD::operator+.2); (ThreeD op2) {. ThreeD operator- ThreeD temp;.(ThreeD op2); = x + ;. = y + ;. void show() ; = z + ;.}; return temp;. }. 4 Cont// Overload assignment. main() {.ThreeD ThreeD::operator=(ThreeD ThreeD a(1, 2, 3), b(10, 10, ){. x = ;. ), c;. y = ; cout << "Original value of a:. z = ; ";. return *this; ();.}.void ThreeD::show() {. cout << x << ", ”<< y << ", ”<< z <<. cout << "Original value of b: ". "\n"; ;.} ();. c = b - a;.PROGRAM OUTPUT: cout << “b - a: ";.Original value of a: 1, 2, 3 ();.Original value of b: 10, 10, 10 }.b - a: 9, 8, 7 5 Multiple Overloading.•. We’ve seen different uses of the + operator: to. add distances and to concatenate strings•. You could put both these classes together in the. same program, and C++ would still know how to. interpret the + operator:. – d1 = d2 + d3;. – s1 = s2 + s3;.•. It selects the correct function to carry out the. “addition” based on the type of operand 6 Comparison Operator <. Main function. Distance dist1(3, );. Distance dist2(6, );. if( dist1 < dist2 ). cout << “\ndist1 is less than dist2”;. else. cout << “\ndist1 is greater than (or equal to). dist2”;.bool Distance::operator < (Distance d2).const {. float bf1 = feet + inches/12;. float bf2 = + ;. return (bf1 < bf2) ? true : false; Go to program.}. 7 Arithmetic assignment: Operator +=. Main() function. Distance dist1(3, );. Distance dist2(5 )

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.