tailieunhanh - Lecture Object oriented programming - Lecture no 10

This section repeats the above two sections using a fmmal mathematical style. Its purpose is to illustrate the power of denotational semantics to enable the full specification of a programming languages type system and semantics. Since it is machine independent, this notation can be used by language and compiler designers as they define and implement new programming languages. | CSC241: Object Oriented Programming Lecture No 10 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 Distance operator + (Distance d2) { float i = inches + ; int f = feet + ; return Distance(f, i); } d3= d1+d2; 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 Overloading -, + and = binary Operators class ThreeD { int x, y, z; public: ThreeD() { x = y = z = 0; } ThreeD(int i, int j, int k) { x = i; y = j; z = k; } ThreeD operator+(ThreeD op2); ThreeD operator=(ThreeD op2); ThreeD operator-(ThreeD op2); void show() ; }; // Overload subtraction. ThreeD ThreeD::operator-(ThreeD op2) { ThreeD temp; = x - ; = y - ; . | CSC241: Object Oriented Programming Lecture No 10 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 Distance operator + (Distance d2) { float i = inches + ; int f = feet + ; return Distance(f, i); } d3= d1+d2; 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 Overloading -, + and = binary Operators class ThreeD { int x, y, z; public: ThreeD() { x = y = z = 0; } ThreeD(int i, int j, int k) { x = i; y = j; z = k; } ThreeD operator+(ThreeD op2); ThreeD operator=(ThreeD op2); ThreeD operator-(ThreeD op2); void show() ; }; // Overload subtraction. ThreeD ThreeD::operator-(ThreeD op2) { ThreeD temp; = x - ; = y - ; = z - ; return temp; } // Overload +. ThreeD ThreeD::operator+(ThreeD op2) { ThreeD temp; = x + ; = y + ; = z + ; return temp; } Cont. // Overload assignment. ThreeD ThreeD::operator=(ThreeD op2){ x = ; y = ; z = ; return *this; } void ThreeD::show() { cout << x << ", ”<< y << ", ”<< z << "\n"; } main() { ThreeD a(1, 2, 3), b(10, 10, 10), c; cout << "Original value of a: "; (); cout << "Original value of b: "; (); c = b - a; cout << “b - a: "; (); } PROGRAM OUTPUT: Original value of a: 1, 2, 3 Original value of b: 10, 10, 10 b - a: 9, 8, 7 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

TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
8    170    0    19-04-2024
14    170    0    19-04-2024
173    101    0    19-04-2024
2    96    0    19-04-2024
8    105    0    19-04-2024
28    109    0    19-04-2024
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.