Operator Overloading
lượt xem 1
download
Operator Overloading's Objectives is discuss operator overloading (definition, use, advantages, limitations); present type conversion operators. It presents about arithmetic operations; operator overloading; using overloaded operator; advantages of operator overloading.
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Operator Overloading
- Operator Overloading
- Objectives • Discuss operator overloading – definition – use – advantages – limitations • Present type conversion operators 2
- Arithmetic operations • Could define a method to perform arithmetic operation – supply as part of class or struct struct Point { int x; int y; add points public static Point Add(Point p, Point q) { return new Point(p.x + q.x, p.y + q.y); } ... } Point a = new Point(1, 2); Point b = new Point(3, 4); invoke Add Point c = Point.Add(a, b); 3
- Operator overloading • Can overload operators to work with class and struct types – use keyword operator – follow with symbol struct Point { int x; int y; overload + public static Point operator+(Point p, Point q) { return new Point(p.x + q.x, p.y + q.y); } ... } 4
- Using overloaded operator • Overloaded operator used like operators for other types – compiler translates into method call Point a = new Point(1, 2); Point b = new Point(3, 4); use operator+ Point c = a + b; 5
- Advantages of operator overloading • Operator overloading yields advantages for user code – concise – readable – takes advantage of user's existing knowledge of symbol Point a = new Point(1, 2); Point b = new Point(3, 4); operator Point c = a + b; method Point d = Point.Add(a, b); 6
- Binary operators • Binary operators take two parameters struct Point { int x; int y; binary + public static Point operator+(Point p, Point q) { return new Point(p.x + q.x, p.y + q.y); } binary - public static Point operator-(Point p, Point q) { return new Point(p.x - q.x, p.y - q.y); } ... } 7
- Unary operators • Unary operators take single parameter struct Point { int x; int y; unary + public static Point operator+(Point p) { return new Point(p.x, p.y); } unary - public static Point operator-(Point p) { return new Point(-p.x, -p.y); } ... } 8
- Mixed types • Can mix parameter types – separate method for each combination of parameter type/order struct Point { Point*int public static Point operator*(Point p, int a) { return new Point(p.x * a, p.y * a); } int*Point public static Point operator*(int a , Point p) { return new Point(a * p.x, a * p.y); } ... } 9
- Equality • Can overload equality and inequality – should ensure Equals method has same semantics struct Point { equality public static bool operator==(Point p, Point q) { return p.x == q.x && p.y == q.y; } inequality public static bool operator!=(Point p, Point q) { return !(p == q); } ... } Point a = new Point(1, 2); Point b = new Point(3, 4); compare points if (a == b) ... 10
- Operator pairs • Some operators are required to be present in pairs – == and != – > and < – >= and
- Compound assignment • Compound assignment operator provided automatically – when corresponding binary operator overloaded struct Point { define binary+ public static Point operator+(Point p, Point q) { return new Point(p.x + q.x, p.y + q.y); } ... } Point a = new Point(1, 2); Point b = new Point(3, 4); Point c; get operator+ c = a + b; get operator+= c += b; 12
- Method format • Overloaded operator must be member of class or struct • Must have specific modifiers – public – static struct Point { int x; int y; required public static Point operator+(Point p, Point q) modifiers { return new Point(p.x + q.x, p.y + q.y); } ... } 13
- Parameter types • At least one parameter must be of enclosing type – prevents redefinition of operators on existing type struct Point { int x; int y; error public static Point operator+(int x, int y) { return new Point(x, y); } ... } 14
- Limitations • Only some operators can be overloaded – unary: + - ! ~ ++ -- true false – binary: + - * / % & | ^ > == != > < >=
- Cross language • Not all .NET languages support operator overloading – operators therefore not available to clients in all languages – should provide regular method in addition to operator struct Point { provide operator public static Point operator+(Point p, Point q) { return Add(p, q); } provide method public static Point Add(Point p, Point q) { return new Point(p.x + q.x, p.y + q.y); } ... } 16
- Type conversion operators • Can overload the type conversion operators – implement user defined type converters – invoke automatically or using cast syntax Rational: 1/2 double: 0.5 int: 3 Rational: 3/1 Polar: (1, 3.14) Cartesian: (-1, 0) 17
- Converter syntax • Define converter using keyword operator – operator name is destination type of conversion – parameter is source of conversion struct Cartesian { ... explicit public static operator Cartesian(Polar p) { ... } } implicit required required choose one required destination source 18
- Implementing converter • Converter should create and return object of destination type – using data in source struct Cartesian { int x; int y; public static explicit operator Cartesian(Polar p) { create object of Cartesian c = new Cartesian(); destination type c.x = p.r * Math.Cos(p.theta); convert data c.y = p.r * Math.Sin(p.theta); return new object return c; } ... } 19
- Converter uses • Converter can be used whenever conversion required – assignment – parameter passing – method return 20
CÓ THỂ BẠN MUỐN DOWNLOAD
-
Lập trình Java cơ bản : OOP trong Java part 8
5 p | 293 | 61
-
Chapter 8 - Operator Overloading
80 p | 194 | 47
-
Phần 3: Operator Overloading
98 p | 158 | 18
-
Chapter 8 Operator Overloading, Friends, and References
46 p | 50 | 7
-
Object oriented programming with C++ - Session 2 More on Classes
37 p | 62 | 5
-
Bài giảng Ngôn ngữ lập trình: Bài 6 - Lê Nguyễn Tuấn Thành
50 p | 65 | 5
-
Bài giảng Lập trình hướng đối tượng C: Chương 5 - ThS. Trần Anh Dũng
84 p | 52 | 5
-
Bài giảng Lập trình hướng đối tượng (Object-Oriented Programming): Phần 2 - GV. Ngô Công Thắng
54 p | 12 | 4
-
Object oriented programming with C++ - Session 4 Operator Overloading
49 p | 70 | 4
-
C# language refference_6
26 p | 43 | 3
-
Bài giảng Lập trình trên Windows với Microsoft.NET: Bài 3 - ThS. Trần Bá Nhiệm
16 p | 70 | 3
-
Object oriented programming with C++ - Session 3 Function Overloading and References
35 p | 49 | 3
-
Bài giảng Lập trình hướng đối tượng: Định nghĩa phép toán Operator Overloading
96 p | 63 | 3
-
Bài giảng Lập trình hướng đối tượng (Object-Oriented Programming) - Chương 5: Chồng hàm và chồng toán tử (function overloading and operator overloading)
56 p | 9 | 2
-
Chương 8 - Operator Overloading
87 p | 47 | 2
-
Bài giảng Lập trình hướng đối tượng: Chương 10 - Trường Đại học Ngoại ngữ - Tin học, TP.HCM
31 p | 7 | 2
-
Bài giảng Lập trình hướng đối tượng - Chương 5: Overload toán tử và hàm
84 p | 42 | 1
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn