SlideShare a Scribd company logo
1 of 18
TÁI ĐỊNH NGHĨA Bộ môn Hệ Thống Máy Tính và Truyền Thông Khoa Công Nghệ Thông Tin và Truyền Thông Đại học Cần Thơ  CHƯƠNG 8: (OVERLOADING)
Nội dung ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Chương  8
Tái định nghĩa hàm ,[object Object],[object Object],class  Time  { //... long  GetTime  (void); // số giây tính từ nửa đêm void  GetTime  (int &hours,  int &minutes,  int &seconds); }; ,[object Object],[object Object],[object Object],[object Object],void main() { int h, m, s; long t = GetTime();  //  Gọi hàm ???   GetTime(h, m, s);  //  Gọi hàm ??? } Chương  8
Tái định nghĩa toán tử ,[object Object],[object Object],[object Object],[object Object],Chương  8 , () [] || && >= <= > < != == >>= <<= ^= |= &= %= /= -= += = >> << ^ | & % / * - + Nhị  hạng delete new ->* -> () -- ++ & ~ ! * - + Đơn  hạng
Tái định nghĩa toán tử (tt) ,[object Object],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; }; 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); }; Có 1 tham số (Nếu là toán tử nhị hạng) Chương  8
Tái định nghĩa toán tử (tt) ,[object Object],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: int x, y; }; 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); }; Có 2 tham số (Nếu là toán tử nhị hạng) Chương  8
Tái định nghĩa toán tử (tt) ,[object Object],#include <iostream.h> const maxCard = 100; enum Bool {false, true}; class  Set   { public: Set(void) { card = 0; } friend Bool  operator  &   (const int, Set&);// thanh 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; };  // Định nghĩa các 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 << &quot;s1 = &quot;; s1.Print(); cout << &quot;s2 = &quot;; s2.Print(); if (20  &  s1) cout << &quot;20  thuoc  s1&quot;; cout << &quot;s1  giao  s2 = &quot;; (s1  *  s2).Print(); cout << &quot;s1  hop  s2 = &quot;;  (s1  +  s2).Print(); if (s1  !=  s2) cout << &quot;s1 /= s2&quot;; return 0; } Chương  8
Chuyển kiểu ,[object Object],void  main()  { Point  p1(10,20),  p2(30,40), p3, p4, p5; p3 = p1  +  p2; p4 = p1  +  5;  p5 = 5  +  p1; }; Có thể định nghĩa thêm 2 toán tử: class  Point  { //... friend Point operator + (Point, Point); friend Point operator + (int,  Point); friend Point operator + (Point, int); }; Chương  8
Chuyển kiểu (tt) void  main()  { Point  p1(10,20),  p2(30,40), p3, p4, p5; p3 = p1  +  p2; p4 = p1  +  5;  // tương đương  p1 + Point(5) p5 = 5  +  p1;  // tương đương  Point(5) + p1 } class  Point  { //... Point (int x)  { Point::x = Point::y = x; } friend Point  operator + (Point, Point); }; Chuyển kiểu 5    Point(5) ,[object Object],Định nghĩa phép chuyển đổi kiểu Chương  8
Tái định nghĩa toán tử xuất << ,[object Object],[object Object],class  Point  { public: Point (int x=0, int y=0)   { Point::x = x; Point::y = y; } friend  ostream&  operator <<   (ostream&  os,  Point&  p) {  os<< “(“ << p.x << “,” << p.y << “)”;  } // ….. private: int x, y; }; void  main () { Point  p1(10,20), p2; cout<<“Diem P1: “<< p1 << endl; cout<<“Diem P2: “<< p2 << endl; } Kết quả trên  màn hình   ? Chương  8
Tái định nghĩa toán tử nhập >> ,[object Object],[object Object],class  Point  { public: Point (int x=0, int y=0)   { Point::x = x; Point::y = y; } friend  istream&  operator >>   (istream&  is,  Point&  p) {  cout<<“Nhap x: “; is>>p.x;   cout<<“Nhap y: “; is>>p.y; } // ….. private: int x, y; }; void  main () { Point  p1, p2; cout<<“Nhap thong tin cho P1: “; cin>>p1; cout<<“Nhap thong tin cho P2: “; cin>>p2; } Chương  8
Tái định nghĩa toán tử [ ] ,[object Object],[object Object],class  StringVec  { public: StringVec (const int dim); ~StringVec (); char*  operator []  (int); int  add(char* ); // ……….. private: char  **elems;  // cac phan tu  int  dim;  // kich thuoc cua vecto int  used;  // vi tri hien tai }; char*  StringVec:: operator []  (int  i) { if ( i>=0 && i<used)  return elems[i]; return “”; } void main() { StringVec   sv1(100); sv1.add(“PTPhi”);sv1.add(“BQThai”); sv1.add(“LVLam”); sv1.add(“NCHuy”); cout<< sv1[2]<<endl; cout<<sv1[0]; } Chương  8
Tái định nghĩa toán tử () ,[object Object],class  Matrix  { public: Matrix (const short rows, const short cols); ~Matrix (void) {delete elems;} double&  operator () (const short row,  const short col); friend  ostream& operator << (ostream&, Matrix&); friend  Matrix  operator  + (Matrix&, Matrix&); friend  Matrix  operator  - (Matrix&, Matrix&); friend  Matrix  operator  * (Matrix&, Matrix&); private: const short rows;   // số hàng const short cols;   // số cột double *elems;   // các phần tử };  double& Matrix:: operator ()  (const short row, const short col) { static  double dummy = 0.0; return (row >= 1 && row <= rows  && col >= 1 && col <= cols) ?  elems[(row - 1)*cols  + (col - 1)] :  dummy; } void  main()  { Matrix  m(3,2); m(1,1)  = 10;  m(1,2)  = 20;  m(2,1)  = 30;  m(2,2)  = 40; m(3,1)  = 50;  m(3,2)  = 60; cout<<m<<endl; } Chương  8
Khởi tạo ngầm định ,[object Object],[object Object],[object Object],[object Object],Lỗi sẽ xảy ra do khởi tạo ngầm bằng cách  gán tương ứng từng thành phần . Chương  8
Khởi tạo ngầm định (tt) ,[object Object],class  Point  { int x, y; public: Point  (int =0; int =0 ); // Khong can thiet DN Point  (const  Point&  p) { x= p.x;  y = p.y; } // ……….. }; // …………… class  Matrix  { //…. Matrix(const Matrix&); }; Matrix:: Matrix  (const Matrix &m)  : rows(m.rows), cols(m.cols) { int n = rows * cols; elems = new double[n];   // cùng kích thước for (register i = 0; i < n; ++i)  // sao chép phần tử elems[i] = m.elems[i]; } Chương  8
Gán ngầm định ,[object Object],[object Object],[object Object],[object Object],[object Object],class  Matrix  { //…. Matrix&  operator =  (const Matrix &m) { if (rows == m.rows && cols == m.cols) {  // phải khớp   int n = rows * cols;   for (register i = 0; i < n; ++i)  // sao chép các phần tử   elems[i] = m.elems[i]; } return *this; }  }; Hàm thành viên Chương  8
Tái định nghĩa toán tử ++ & -- ,[object Object],[object Object],[object Object],class  PhanSo  { int  tuso, mau so; public: // ….. PhanSo(int=0 , int =1); friend  PhanSo operator ++ (PhanSo&); friend  PhanSo operator ++ (PhanSo&, int); }; PhanSo operator ++ (PhanSo& p) { return (p = PhanSo(tuso+mauso, mauso)); } PhanSo operator ++ (PhanSo& p, int  x) { PhanSo  p2 = PhanSo(tuso+mauso, mauso); return  p2; } void  main () { PhanSo  p1(3,4), p2; cout<< p1++; cout<<++p2; cout<<++(p1++) + (++p2)++; } Kết quả trên màn hình ? Chương  8
Tái định nghĩa new & delete ,[object Object],[object Object],[object Object],[object Object],[object Object],class  Point  { public: //... void*  operator new  (size_t bytes); void  operator delete  (void *ptr, size_t bytes); private: int xVal, yVal; }; void  main () { Point  *p = new Point(10,20); Point  *ds = new Point[30]; //……………… delete p; delete []ds; } Chương  8

More Related Content

What's hot (15)

Hướng dẫn làm bt về chuỗi.doc
Hướng dẫn làm bt về chuỗi.docHướng dẫn làm bt về chuỗi.doc
Hướng dẫn làm bt về chuỗi.doc
 
Nmlt C03 Cac Kieu Du Lieu Co So
Nmlt C03 Cac Kieu Du Lieu Co SoNmlt C03 Cac Kieu Du Lieu Co So
Nmlt C03 Cac Kieu Du Lieu Co So
 
giao trinh c++ Chuong1
giao trinh c++ Chuong1giao trinh c++ Chuong1
giao trinh c++ Chuong1
 
Tut4 solution
Tut4 solutionTut4 solution
Tut4 solution
 
Lesson07
Lesson07Lesson07
Lesson07
 
Ktlt lab full
Ktlt lab fullKtlt lab full
Ktlt lab full
 
Ctdl lab01
Ctdl lab01Ctdl lab01
Ctdl lab01
 
Hàm can bản
Hàm can bảnHàm can bản
Hàm can bản
 
Local sakainame 501127 ktl_trình hlmt1 a01 fall 2013 _ modules
Local sakainame   501127 ktl_trình hlmt1 a01 fall 2013 _ modulesLocal sakainame   501127 ktl_trình hlmt1 a01 fall 2013 _ modules
Local sakainame 501127 ktl_trình hlmt1 a01 fall 2013 _ modules
 
Tn ktlt
Tn ktltTn ktlt
Tn ktlt
 
Neural Network from Scratch
Neural Network from ScratchNeural Network from Scratch
Neural Network from Scratch
 
C đến C++ phần 1
C đến C++ phần 1C đến C++ phần 1
C đến C++ phần 1
 
Lab4
Lab4Lab4
Lab4
 
Slide pointer sepro
Slide pointer seproSlide pointer sepro
Slide pointer sepro
 
Tut6
Tut6Tut6
Tut6
 

Viewers also liked

Lap trinh huong_doi_tuong_cpp_dhct_lesson04
Lap trinh huong_doi_tuong_cpp_dhct_lesson04Lap trinh huong_doi_tuong_cpp_dhct_lesson04
Lap trinh huong_doi_tuong_cpp_dhct_lesson04xcode_esvn
 
T d que_lap_trinh_huong_doi_tuong
T d que_lap_trinh_huong_doi_tuongT d que_lap_trinh_huong_doi_tuong
T d que_lap_trinh_huong_doi_tuongtoiseden91
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson06
Lap trinh huong_doi_tuong_cpp_dhct_lesson06Lap trinh huong_doi_tuong_cpp_dhct_lesson06
Lap trinh huong_doi_tuong_cpp_dhct_lesson06xcode_esvn
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson05
Lap trinh huong_doi_tuong_cpp_dhct_lesson05Lap trinh huong_doi_tuong_cpp_dhct_lesson05
Lap trinh huong_doi_tuong_cpp_dhct_lesson05xcode_esvn
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson02
Lap trinh huong_doi_tuong_cpp_dhct_lesson02Lap trinh huong_doi_tuong_cpp_dhct_lesson02
Lap trinh huong_doi_tuong_cpp_dhct_lesson02xcode_esvn
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson09
Lap trinh huong_doi_tuong_cpp_dhct_lesson09Lap trinh huong_doi_tuong_cpp_dhct_lesson09
Lap trinh huong_doi_tuong_cpp_dhct_lesson09xcode_esvn
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson03
Lap trinh huong_doi_tuong_cpp_dhct_lesson03Lap trinh huong_doi_tuong_cpp_dhct_lesson03
Lap trinh huong_doi_tuong_cpp_dhct_lesson03xcode_esvn
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson01
Lap trinh huong_doi_tuong_cpp_dhct_lesson01Lap trinh huong_doi_tuong_cpp_dhct_lesson01
Lap trinh huong_doi_tuong_cpp_dhct_lesson01xcode_esvn
 
Gtrinh oop
Gtrinh oopGtrinh oop
Gtrinh oopKu Anh
 
Lập trình c++ có lời giải 2
Lập trình c++ có lời giải 2Lập trình c++ có lời giải 2
Lập trình c++ có lời giải 2Minh Ngoc Tran
 
Pplthdt c01 mot_sovandetronglaptrinh_v13.09a
Pplthdt c01 mot_sovandetronglaptrinh_v13.09aPplthdt c01 mot_sovandetronglaptrinh_v13.09a
Pplthdt c01 mot_sovandetronglaptrinh_v13.09aPix Nhox
 
Pplthdt c00 gioi_thieumonhoc_v13.09a
Pplthdt c00 gioi_thieumonhoc_v13.09aPplthdt c00 gioi_thieumonhoc_v13.09a
Pplthdt c00 gioi_thieumonhoc_v13.09aPix Nhox
 
Lap trinh c++ có lời giải 1
Lap trinh c++ có lời giải 1Lap trinh c++ có lời giải 1
Lap trinh c++ có lời giải 1Minh Ngoc Tran
 
Lap trinh c++ có lời giải 3
Lap trinh c++ có lời giải 3Lap trinh c++ có lời giải 3
Lap trinh c++ có lời giải 3Minh Ngoc Tran
 
Bài tập mẫu C và C++ có giải
Bài tập mẫu C và C++ có giảiBài tập mẫu C và C++ có giải
Bài tập mẫu C và C++ có giảiTrung Thanh Nguyen
 

Viewers also liked (15)

Lap trinh huong_doi_tuong_cpp_dhct_lesson04
Lap trinh huong_doi_tuong_cpp_dhct_lesson04Lap trinh huong_doi_tuong_cpp_dhct_lesson04
Lap trinh huong_doi_tuong_cpp_dhct_lesson04
 
T d que_lap_trinh_huong_doi_tuong
T d que_lap_trinh_huong_doi_tuongT d que_lap_trinh_huong_doi_tuong
T d que_lap_trinh_huong_doi_tuong
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson06
Lap trinh huong_doi_tuong_cpp_dhct_lesson06Lap trinh huong_doi_tuong_cpp_dhct_lesson06
Lap trinh huong_doi_tuong_cpp_dhct_lesson06
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson05
Lap trinh huong_doi_tuong_cpp_dhct_lesson05Lap trinh huong_doi_tuong_cpp_dhct_lesson05
Lap trinh huong_doi_tuong_cpp_dhct_lesson05
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson02
Lap trinh huong_doi_tuong_cpp_dhct_lesson02Lap trinh huong_doi_tuong_cpp_dhct_lesson02
Lap trinh huong_doi_tuong_cpp_dhct_lesson02
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson09
Lap trinh huong_doi_tuong_cpp_dhct_lesson09Lap trinh huong_doi_tuong_cpp_dhct_lesson09
Lap trinh huong_doi_tuong_cpp_dhct_lesson09
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson03
Lap trinh huong_doi_tuong_cpp_dhct_lesson03Lap trinh huong_doi_tuong_cpp_dhct_lesson03
Lap trinh huong_doi_tuong_cpp_dhct_lesson03
 
Lap trinh huong_doi_tuong_cpp_dhct_lesson01
Lap trinh huong_doi_tuong_cpp_dhct_lesson01Lap trinh huong_doi_tuong_cpp_dhct_lesson01
Lap trinh huong_doi_tuong_cpp_dhct_lesson01
 
Gtrinh oop
Gtrinh oopGtrinh oop
Gtrinh oop
 
Lập trình c++ có lời giải 2
Lập trình c++ có lời giải 2Lập trình c++ có lời giải 2
Lập trình c++ có lời giải 2
 
Pplthdt c01 mot_sovandetronglaptrinh_v13.09a
Pplthdt c01 mot_sovandetronglaptrinh_v13.09aPplthdt c01 mot_sovandetronglaptrinh_v13.09a
Pplthdt c01 mot_sovandetronglaptrinh_v13.09a
 
Pplthdt c00 gioi_thieumonhoc_v13.09a
Pplthdt c00 gioi_thieumonhoc_v13.09aPplthdt c00 gioi_thieumonhoc_v13.09a
Pplthdt c00 gioi_thieumonhoc_v13.09a
 
Lap trinh c++ có lời giải 1
Lap trinh c++ có lời giải 1Lap trinh c++ có lời giải 1
Lap trinh c++ có lời giải 1
 
Lap trinh c++ có lời giải 3
Lap trinh c++ có lời giải 3Lap trinh c++ có lời giải 3
Lap trinh c++ có lời giải 3
 
Bài tập mẫu C và C++ có giải
Bài tập mẫu C và C++ có giảiBài tập mẫu C và C++ có giải
Bài tập mẫu C và C++ có giải
 

Similar to Lap trinh huong_doi_tuong_cpp_dhct_lesson08

C10 generic algorithms
C10 generic algorithmsC10 generic algorithms
C10 generic algorithmsHồ Lợi
 
4 Pointer String Struct
4 Pointer String  Struct4 Pointer String  Struct
4 Pointer String StructCuong
 
3 Function
3 Function3 Function
3 FunctionCuong
 
1 Gioi Thieu Chung
1 Gioi Thieu Chung1 Gioi Thieu Chung
1 Gioi Thieu ChungCuong
 
Nmlt C06 Ham
Nmlt C06 HamNmlt C06 Ham
Nmlt C06 HamCuong
 
Lec3. Ham.pdf
Lec3. Ham.pdfLec3. Ham.pdf
Lec3. Ham.pdfKinHongnh
 
Cpl test1%20key
Cpl test1%20keyCpl test1%20key
Cpl test1%20keyHồ Lợi
 
C3 functions and_library
C3 functions and_libraryC3 functions and_library
C3 functions and_libraryHồ Lợi
 
Session 4
Session 4Session 4
Session 4pnanhvn
 
Ctdl C04
Ctdl C04Ctdl C04
Ctdl C04giang
 

Similar to Lap trinh huong_doi_tuong_cpp_dhct_lesson08 (20)

Lesson08
Lesson08Lesson08
Lesson08
 
Lesson07
Lesson07Lesson07
Lesson07
 
C10 generic algorithms
C10 generic algorithmsC10 generic algorithms
C10 generic algorithms
 
C10 generic algorithms
C10 generic algorithmsC10 generic algorithms
C10 generic algorithms
 
4 Pointer String Struct
4 Pointer String  Struct4 Pointer String  Struct
4 Pointer String Struct
 
Chuong7
Chuong7Chuong7
Chuong7
 
3 Function
3 Function3 Function
3 Function
 
Session 13
Session 13Session 13
Session 13
 
1 Gioi Thieu Chung
1 Gioi Thieu Chung1 Gioi Thieu Chung
1 Gioi Thieu Chung
 
Nmlt C06 Ham
Nmlt C06 HamNmlt C06 Ham
Nmlt C06 Ham
 
Session 11
Session 11Session 11
Session 11
 
Session 11
Session 11Session 11
Session 11
 
Lec3. Ham.pdf
Lec3. Ham.pdfLec3. Ham.pdf
Lec3. Ham.pdf
 
Cpl test1%20key
Cpl test1%20keyCpl test1%20key
Cpl test1%20key
 
Chuong3 c
Chuong3 c Chuong3 c
Chuong3 c
 
C3 functions and_library
C3 functions and_libraryC3 functions and_library
C3 functions and_library
 
C3 functions and_library
C3 functions and_libraryC3 functions and_library
C3 functions and_library
 
Session 4
Session 4Session 4
Session 4
 
344444
344444344444
344444
 
Ctdl C04
Ctdl C04Ctdl C04
Ctdl C04
 

Lap trinh huong_doi_tuong_cpp_dhct_lesson08

  • 1. TÁI ĐỊNH NGHĨA Bộ môn Hệ Thống Máy Tính và Truyền Thông Khoa Công Nghệ Thông Tin và Truyền Thông Đại học Cần Thơ CHƯƠNG 8: (OVERLOADING)
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.