Bài giảng Lập trình C++ - Chương 7: Các lớp (tiếp)

7.1 Giới thiệu
7.2 Các đối tượng hằng và các phương thức hằng
7.3 Sự cấu thành: các đối tượng là các lớp thành viên
7.4 Các hàm friend và các lớp friend
7.5 Sử dụng con trỏ this
7.6 Quản lý bộ nhớ động với toán tử new và toán tử delete
7.7 Các thành viên là lớp static
7.8 Dữ liệu trừu tượng và che dấu thông tin

ppt56 trang | Chuyên mục: C/C++ | Chia sẻ: dkS00TYs | Lượt xem: 1602 | Lượt tải: 1download
Tóm tắt nội dung Bài giảng Lập trình C++ - Chương 7: Các lớp (tiếp), để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
r value 34 void Time::setHour( int h ) 35 { 36 hour = ( h >= 0 && h = 0 && m = 0 && s 5 6 using std::cout; 7 using std::endl; 8 9 class Increment { 10 11 public: 12 Increment( int c = 0, int i = 1 ); // default constructor 13 14 void addIncrement() 15 { 16 count += increment; 17 18 } // end function addIncrement 19 20 void print() const; // prints count and increment 21 * fig07_04.cpp(2 of 3) 22 private: 23 int count; 24 const int increment; // const data member 25 26 }; // end class Increment 27 28 // constructor 29 Increment::Increment( int c, int i ) 30 : count( c ), // initializer for non-const member 31 increment( i ) // required initializer for const member 32 { 33 // empty body 34 35 } // end Increment constructor 36 37 // print count and increment values 38 void Increment::print() const 39 { 40 cout 5 6 using std::cout; 7 using std::endl; 8 9 class Increment { 10 11 public: 12 Increment( int c = 0, int i = 1 ); // default constructor 13 14 void addIncrement() 15 { 16 count += increment; 17 18 } // end function addIncrement 19 20 void print() const; // prints count and increment 21 * fig07_05.cpp(2 of 3) 22 private: 23 int count; 24 const int increment; // const data member 25 26 }; // end class Increment 27 28 // constructor 29 Increment::Increment( int c, int i ) 30 { // Constant member 'increment' is not initialized 31 count = c; // allowed because count is not constant 32 increment = i; // ERROR: Cannot modify a const object 33 34 } // end Increment constructor 35 36 // print count and increment values 37 void Increment::print() const 38 { 39 cout 4 5 using std::cout; 6 using std::endl; 7 8 // include Date class definition from date1.h 9 #include "date1.h" 10 11 // constructor confirms proper value for month; calls 12 // utility function checkDay to confirm proper value for day 13 Date::Date( int mn, int dy, int yr ) 14 { 15 if ( mn > 0 && mn 0 && testDay 4 5 using std::cout; 6 using std::endl; 7 8 #include // strcpy and strlen prototypes 9 10 #include "employee1.h" // Employee class definition 11 #include "date1.h" // Date class definition 12 * employee1.cpp(2 of 3) 13 // constructor uses member initializer list to pass initializer 14 // values to constructors of member objects birthDate and 15 // hireDate [Note: This invokes the so-called "default copy 16 // constructor" which the C++ compiler provides implicitly.] 17 Employee::Employee( const char *first, const char *last, 18 const Date &dateOfBirth, const Date &dateOfHire ) 19 : birthDate( dateOfBirth ), // initialize birthDate 20 hireDate( dateOfHire ) // initialize hireDate 21 { 22 // copy first into firstName and be sure that it fits 23 int length = strlen( first ); 24 length = ( length 4 5 using std::cout; 6 using std::endl; 7 8 #include "employee1.h" // Employee class definition 9 10 int main() 11 { 12 Date birth( 7, 24, 1949 ); 13 Date hire( 3, 12, 1988 ); 14 Employee manager( "Bob", "Jones", birth, hire ); 15 16 cout 4 5 using std::cout; 6 using std::endl; 7 8 // Count class definition 9 class Count { 10 friend void setX( Count &, int ); // friend declaration 11 12 public: 13 14 // constructor 15 Count() 16 : x( 0 ) // initialize x to 0 17 { 18 // empty body 19 20 } // end Count constructor 21 * fig07_11.cpp(2 of 3) 22 // output x 23 void print() const 24 { 25 cout 5 6 using std::cout; 7 using std::endl; 8 9 // Count class definition 10 // (note that there is no friendship declaration) 11 class Count { 12 13 public: 14 15 // constructor 16 Count() 17 : x( 0 ) // initialize x to 0 18 { 19 // empty body 20 21 } // end Count constructor 22 * fig07_12.cpp (2 of 3) 23 // output x 24 void print() const 25 { 26 cout 4 5 using std::cout; 6 using std::endl; 7 8 class Test { 9 10 public: 11 Test( int = 0 ); // default constructor 12 void print() const; 13 14 private: 15 int x; 16 17 }; // end class Test 18 19 // constructor 20 Test::Test( int value ) 21 : x( value ) // initialize x to value 22 { 23 // empty body 24 25 } // end Test constructor * fig07_13.cpp (2 of 3) 26 27 // print x using implicit and explicit this pointers; 28 // parentheses around *this required 29 void Test::print() const 30 { 31 // implicitly use this pointer to access member x 32 cout x = " x; 36 37 // explicitly use dereferenced this pointer and 38 // the dot operator to access member x 39 cout x = 12 (*this).x = 12 * 7.6 	Quản lý bộ nhớ động với toán tử new và delete Quản lý bộ nhớ động Điều khiển quá trình cấp phát và thu hồi bộ nhớ Toán tử new và delete * 7.6 	Quản lý bộ nhớ động với toán tử new và delete New Time *timePtr; timePtr = new Time; Toán tử new Tạo ra đối tượng với cỡ bằng cỡ của Time Lỗi nếu không đủ bộ nhớ cấp phát cho đối tượng Gọi hàm khởi tạo mặc định cho đối tượng Trả về con trỏ của kiểu xác định Hỗ trợ khởi tạo double *ptr = new double( 3.14159 ); Time *timePtr = new Time( 12, 0, 0 ); Cấp phát mảng: int *gradesArray = new int[ 10 ]; * 7.6 	Quản lý bộ nhớ động với toán tử new và delete delete Huỷ đối tượng được cấp phát và giải phóng bộ nhớ delete timePtr; Toán tử delete Gọi hàm huỷ cho đối tượng Dọn dẹp bộ nhớ của đối tượng Bộ nhớ có thể được sử dụng lại để cấp phát cho đối tượng khác Huỷ mảng các phần tửDeallocating arrays delete [] gradesArray; Dọn dẹp mảng được quản lý với con trỏ gradesArray Nếu con trỏ trỏ tới một dãy các đối tượng Đầu tiên gọi hàm huỷ cho mỗi đối tượng trong mảng Sau đó dọn dẹp con trỏ * 7.7	thành viên static các biến static Là dữ liệu của lớp lớn Là tính chất của lớp, không phải của một đối tượng cụ thể trong lớp Hiệu quả khi sao chép dữ liệu Chỉ có biến static được cập nhật Có thể xem như là biến toàn cục, nhưng chỉ trong phạm vi của lớp Chỉ có thể được truy cập từ các đối tượng trong cùng một lớp Khởi tạo đúng một lần trong phạm vi của file Tồn tại ngay cả khi không có đối tượng nào tồn tại Có thể dữ liệu ở quyền public, private hoặc protected * 7.7	các thành viên static Truy cập tới các biến static Truy cập thông qua một đối tượng bất kỳ của lớp Các biến public static Có thể truy cập thông qua toán tử phạm vi (::) Employee::count Các biến private static Khi không có đối tượng thuộc lớp Chỉ có thể truy cập thông qua các phương thức public Gọi phương thức public static kết hợp với tên lớp toán tử phạm vi và tên hàm 	Employee::getCount() * 7.7	các thành viên static Các phương thức static Không thể truy cập tới dữ liệu hoặc hàm không phải static Không có con trỏ this cho các phương thức static Các dữ liệu static và các hàm static tồn tại độc lập với các đối tượng * employee2.h (1 of 2) 1 // Fig. 7.17: employee2.h 2 // Employee class definition. 3 #ifndef EMPLOYEE2_H 4 #define EMPLOYEE2_H 5 6 class Employee { 7 8 public: 9 Employee( const char *, const char * ); // constructor 10 ~Employee(); // destructor 11 const char *getFirstName() const; // return first name 12 const char *getLastName() const; // return last name 13 14 // static member function 15 static int getCount(); // return # objects instantiated 16 17 private: 18 char *firstName; 19 char *lastName; 20 21 // static data member 22 static int count; // number of objects instantiated 23 24 }; // end class Employee 25 * employee2.h (2 of 2)employee2.cpp(1 of 3) 26 #endif 1 // Fig. 7.18: employee2.cpp 2 // Member-function definitions for class Employee. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include // C++ standard new operator 9 #include // strcpy and strlen prototypes 10 11 #include "employee2.h" // Employee class definition 12 13 // define and initialize static data member 14 int Employee::count = 0; 15 16 // define static member function that returns number of 17 // Employee objects instantiated 18 int Employee::getCount() 19 { 20 return count; 21 22 } // end static function getCount * employee2.cpp(2 of 3) 23 24 // constructor dynamically allocates space for 25 // first and last name and uses strcpy to copy 26 // first and last names into the object 27 Employee::Employee( const char *first, const char *last ) 28 { 29 firstName = new char[ strlen( first ) + 1 ]; 30 strcpy( firstName, first ); 31 32 lastName = new char[ strlen( last ) + 1 ]; 33 strcpy( lastName, last ); 34 35 ++count; // increment static count of employees 36 37 cout 4 5 using std::cout; 6 using std::endl; 7 8 #include // C++ standard new operator 9 10 #include "employee2.h" // Employee class definition 11 12 int main() 13 { 14 cout getCount(); 22 * fig07_19.cpp(2 of 2) 23 cout getFirstName() 25 getLastName() 26 getFirstName() 28 getLastName() << "\n\n"; 29 30 delete e1Ptr; // recapture memory 31 e1Ptr = 0; // disconnect pointer from free-store space 32 delete e2Ptr; // recapture memory 33 e2Ptr = 0; // disconnect pointer from free-store space 34 35 cout << "Number of employees after deletion is " 36 << Employee::getCount() << endl; 37 38 return 0; 39 40 } // end main * fig07_19.cppoutput (1 of 1) Number of employees before instantiation is 0 Employee constructor for Susan Baker called. Employee constructor for Robert Jones called. Number of employees after instantiation is 2   Employee 1: Susan Baker Employee 2: Robert Jones   ~Employee() called for Susan Baker ~Employee() called for Robert Jones Number of employees after deletion is 0 * 7.8	Dữ liệu trừu tượng và che dấu thông tin Che dấu thông tin Các lớp che dấu thông tin chi tiết với người sử dụng Example: cấu trúc dữ liệu stack Các phần tử dữ liệu được chèn vào đỉnh stack Các phần tử dữ liệu được loại tử đỉnh stack Cấu trúc dữ liệu Last-in, first-out (LIFO) Người sử dụng chỉ muốn cấu trúc dữ liệu LIFO Không quan tâm tới việc thực hiện của stack Dữ liệu trừu tượng Mô tả chức năng của lớp độc lập với việc thực hiện các chức năng * 7.8	Dữ liệu trừu tượng và che dấu thông tin Các kiểu dữ liệu trừu tượng Các mô hình (xấp xỉ) với các khái niệm và các hành vi của thế giới thực int, float là các mô hình cho các số Biểu diễn dữ liệu Các phép toán cho phép trên các kiểu dữ liệu này C++ mở rộng Các kiểu dữ liệu chuẩn không thể thay đổi, nhưng các kiểu dữ liệu mới có thể được tạo ra 

File đính kèm:

  • pptBài giảng Lập trình C++ - Chương 7 Các lớp (tiếp).ppt
Tài liệu liên quan