TÁI L P TRÌNH NG IT NG OPERATOR & OTHERS Tr n Ph c Tu n tuantp@hcmup.edu.vn http://giaotrinh.tranphuoctuan.com NH NGH A Tái nh ngh a hàm Tái nh ngh a toán t Chuy n i ki u Tái nh ngh a toán t xu t () Tái nh ngh a toán t [], toán t () Kh i t o ng m nh - Gán ng m Tái nh ngh a toán t ++ Tái nh ngh a new delete nh ngh a hàm Tái nh ngh a hàm tên i s ph i khác nhau: ng n ng Th Có th dùng 8/30/2009 is a êm c TPT - LTH T nh ngh a toán t nh ngh a phép toán i t ng Các phép toán có th tái nh ngh a: Ki u class Time { // long GetTime (void); // s giây tính t void GetTime (int &hours, int &minutes, int &seconds); }; TPT - LTH T 8/30/2009 Tái nh void main() { int h, m, s; long t = GetTime(); // G i hàm ??? GetTime(h, m, s); // G i hàm ??? } Nh ng + - new * ! ~ & ++ () -> delete + - * / % & | ^ > = += -= /= %= &= |= ^= = == != < > = && || [] () Các phép toán không th tái nh 8/30/2009 * ->* :: ?: TPT - LTH T , nh ngh a: sizeof Tái nh ngh a toán t (tt) Tái ng hàm thành viên: ng hàm class Point { public: Point (int x, int y) { Point::x = x; Point::y = y; } Point operator + (Point &p) { return Point(x + p.x,y + p.y); } Point operator - (Point &p) { return Point(x - p.x, y - p.y); } private: int x, y; }; Có tham s (N u toán t nh ng) void main() { Point p1(10,20), p2(10,20); Point p3 = p1 + p2; Point p4 = p1 - p2; Point p5 = p3.operator + (p4); Point p6 = p3.operator – (p4); }; Tái TPT - LTH T ng) TPT - LTH T 8/30/2009 Chuy n ki u i ti n l p t p h p (Set): 8/30/2009 ng khai báo friend class Point { public: Point (int x, int y) { Point::x = x; Point::y = y; } friend Point operator + (Point &p, Point &q) {return Point(p.x + q.x,p.y + q.y); } friend Point operator - (Point &p, Point &q) {return Point(p.x - q.x,p.y - q.y); } private: Có tham s int x, y; (N u toán t nh }; nh ngh a toán t (tt) #include const maxCard = 100; enum Bool {false, true}; class Set { public: Set(void) { card = 0; } friend Bool operator & (const int, Set&);// vien ? friend Bool operator == (Set&, Set&); // bang ? friend Bool operator != (Set&, Set&); // khong bang ? friend Set operator * (Set&, Set&); // giao friend Set operator + (Set&, Set&); // hop // void AddElem(const int elem); void Copy (Set &set); void Print (void); private: int elems[maxCard]; int card; }; c l p: th void main() { Point p1(10,20), p2(10,20); Point p3 = p1 + p2; Point p4 = p1 - p2; Point p5 =operator + (p3, p4); Point p6 = operator – (p3, p4); }; TPT - LTH T 8/30/2009 nh ngh a toán t (tt) Mu n th c hi n phép c ng: // nh ngh a toán t ……………… ……………… int main (void) { Set s1, s2, s3; s1.AddElem(10); s1.AddElem(20); s1.AddElem(30); s1.AddElem(40); s2.AddElem(30); s2.AddElem(50); s2.AddElem(10); s2.AddElem(60); cout ... y) { Point::x = x; Point::y = y; } Point operator + (Point &p) { return Point(x + p.x,y + p.y); } Point operator - (Point &p) { return Point(x - p.x, y - p.y); } private: int x, y; }; Có tham... p1(10,20), p2(10,20); Point p3 = p1 + p2; Point p4 = p1 - p2; Point p5 = p3 .operator + (p4); Point p6 = p3 .operator – (p4); }; Tái TPT - LTH T ng) TPT - LTH T 8/30/2009 Chuy n ki u i ti n l p t p h p... Point::y = y; } friend Point operator + (Point &p, Point &q) {return Point(p.x + q.x,p.y + q.y); } friend Point operator - (Point &p, Point &q) {return Point(p.x - q.x,p.y - q.y); } private: Có tham