Bài giảng Ngôn ngữ lập trình C++ - Chương 7: Kỹ thuật kế thừa

Đề mục
7.1 Giới thiệu chung
7.2 Lớp cơ sở và lớp dẫn xuất
7.3 Đơn kế thừa
7.4 Hàm ảo và tính đa hình
7.5 Đa kế thừa
7.6 Kế thừa private
7.7 Kế thừa public
7.8 Kế thừa protected
7.9 Thừa kế dữ liệu và phương thức
7.10 Tạo và hủy hàm

 

ppt82 trang | Chuyên mục: C/C++ | Chia sẻ: dkS00TYs | Lượt xem: 1587 | Lượt tải: 1download
Tóm tắt nội dung Bài giảng Ngôn ngữ lập trình C++ - Chương 7: Kỹ thuật kế thừa, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
ến các thành viên của cấu trúc/lớp Toán tử mũi tên (->) truy nhập các thành viên qua con trỏ đến đối tượng Ví dụ: in thành viên hour của đối tượng timeObject: 	cout hour; timePtr->hour tương đương ( *timePtr ).hour Cần có cặp ngoặc do * không được ưu tiên bằng . fig06_01.cpp(1 of 3) 1 // Fig. 6.1: fig06_01.cpp 2 // Create a structure, set its members, and print it. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setfill; 11 using std::setw; 12 13 // structure definition 14 struct Time { 15 int hour; // 0-23 (24-hour clock format) 16 int minute; // 0-59 17 int second; // 0-59 18 19 }; // end struct Time 20 21 void printUniversal( const Time & ); // prototype 22 void printStandard( const Time & ); // prototype 23 fig06_01.cpp(2 of 3) 24 int main() 25 { 26 Time dinnerTime; // variable of new type Time 27 28 dinnerTime.hour = 18; // set hour member of dinnerTime 29 dinnerTime.minute = 30; // set minute member of dinnerTime 30 dinnerTime.second = 0; // set second member of dinnerTime 31 32 cout 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setfill; 11 using std::setw; 12 13 // Time abstract data type (ADT) definition 14 class Time { 15 16 public: 17 Time(); // constructor 18 void setTime( int, int, int ); // set hour, minute, second 19 void printUniversal(); // print universal-time format 20 void printStandard(); // print standard-time format 21 fig06_03.cpp(2 of 5) 22 private: 23 int hour; // 0 - 23 (24-hour clock format) 24 int minute; // 0 - 59 25 int second; // 0 - 59 26 27 }; // end class Time 28 29 // Time constructor initializes each data member to zero and 30 // ensures all Time objects start in a consistent state 31 Time::Time() 32 { 33 hour = minute = second = 0; 34 35 } // end Time constructor 36 37 // set new Time value using universal time, perform validity 38 // checks on the data values and set invalid values to zero 39 void Time::setTime( int h, int m, int s ) 40 { 41 hour = ( h >= 0 && h = 0 && m = 0 && s ) dùng cho các con trỏ tới đối tượng fig06_04.cpp(1 of 2) 1 // Fig. 6.4: fig06_04.cpp 2 // Demonstrating the class member access operators . and -> 3 // 4 // CAUTION: IN FUTURE EXAMPLES WE AVOID PUBLIC DATA! 5 #include 6 7 using std::cout; 8 using std::endl; 9 10 // class Count definition 11 class Count { 12 13 public: 14 int x; 15 16 void print() 17 { 18 cout x = 3; // assign 3 to data member x 39 counterPtr->print(); // call member function print 40 41 return 0; 42 43 } // end main Assign 1 to x and print using the object's name: 1 Assign 2 to x and print using a reference: 2 Assign 3 to x and print using a pointer: 3 6.7 Tách giao diện ra khỏi cài đặt Tách giao diện khỏi cài đặt ích lợi dễ sửa đổi chương trình bất lợi phải tạo các file header gồm một phần của cài đặt Inline member functions – các hàm inline gợi ý về phần khác của cài đặt private members 6.7 Tách giao diện ra khỏi cài đặt Các file header chứa các định nghĩa lớp và các nguyên mẫu hàm được include trong mỗi file sử dụng lớp đó #include mở rộng của file .h Các file mã nguồn – Source-code files chứa định nghĩa của các hàm thành viên trùng tên file với file header tương ứng (không kể phần mở rộng) đây chỉ là thông lệ, không bắt buộc được biên dịch và liên kết với file chương trình chính time1.h (1 of 1) 1 // Fig. 6.5: time1.h 2 // Declaration of class Time. 3 // Member functions are defined in time1.cpp 4 5 // prevent multiple inclusions of header file 6 #ifndef TIME1_H 7 #define TIME1_H 8 9 // Time abstract data type definition 10 class Time { 11 12 public: 13 Time(); // constructor 14 void setTime( int, int, int ); // set hour, minute, second 15 void printUniversal(); // print universal-time format 16 void printStandard(); // print standard-time format 17 18 private: 19 int hour; // 0 - 23 (24-hour clock format) 20 int minute; // 0 - 59 21 int second; // 0 - 59 22 23 }; // end class Time 24 25 #endif time1.cpp (1 of 3) 1 // Fig. 6.6: time1.cpp 2 // Member-function definitions for class Time. 3 #include 4 5 using std::cout; 6 7 #include 8 9 using std::setfill; 10 using std::setw; 11 12 // include definition of class Time from time1.h 13 #include "time1.h" 14 15 // Time constructor initializes each data member to zero. 16 // Ensures all Time objects start in a consistent state. 17 Time::Time() 18 { 19 hour = minute = second = 0; 20 21 } // end Time constructor 22 time1.cpp (2 of 3) 23 // Set new Time value using universal time. Perform validity 24 // checks on the data values. Set invalid values to zero. 25 void Time::setTime( int h, int m, int s ) 26 { 27 hour = ( h >= 0 && h = 0 && m = 0 && s 5 6 using std::cout; 7 using std::endl; 8 9 // include definition of class Time from time1.h 10 #include "time1.h" 11 12 int main() 13 { 14 Time t; // instantiate object t of class Time 15 16 // output Time object t's initial values 17 cout 5 6 using std::cout; 7 8 // include definition of class Time from time1.h 9 #include "time1.h" 10 11 int main() 12 { 13 Time t; // create Time object 14 15 16 t.hour = 7; // error: 'Time::hour' is not accessible 17 18 // error: 'Time::minute' is not accessible 19 cout 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 using std::fixed; 9 10 #include 11 12 using std::setprecision; 13 14 // include SalesPerson class definition from salesp.h 15 #include "salesp.h" 16 17 // initialize elements of array sales to 0.0 18 SalesPerson::SalesPerson() 19 { 20 for ( int i = 0; i > salesFigure; 33 setSales( i, salesFigure ); 34 35 } // end for 36 37 } // end function getSalesFromUser 38 39 // set one of the 12 monthly sales figures; function subtracts 40 // one from month value for proper subscript in sales array 41 void SalesPerson::setSales( int month, double amount ) 42 { 43 // test for valid month and amount values 44 if ( month >= 1 && month 0 ) 45 sales[ month - 1 ] = amount; // adjust for subscripts 0-11 46 47 else // invalid month or amount value 48 cout 4 5 using std::cout; 6 7 #include 8 9 using std::setfill; 10 using std::setw; 11 12 // include definition of class Time from time2.h 13 #include "time2.h" 14 15 // Time constructor initializes each data member to zero; 16 // ensures all Time objects start in a consistent state 17 Time::Time( int hr, int min, int sec ) 18 { 19 setTime( hr, min, sec ); // validate and set time 20 21 } // end Time constructor 22 time2.cpp (2 of 2) 23 // set new Time value using universal time, perform validity 24 // checks on the data values and set invalid values to zero 25 void Time::setTime( int h, int m, int s ) 26 { 27 hour = ( h >= 0 && h = 0 && m = 0 && s 4 5 using std::cout; 6 using std::endl; 7 8 // include definition of class Time from time2.h 9 #include "time2.h" 10 11 int main() 12 { 13 Time t1; // all arguments defaulted 14 Time t2( 2 ); // minute and second defaulted 15 Time t3( 21, 34 ); // second defaulted 16 Time t4( 12, 25, 42 ); // all values specified 17 Time t5( 27, 74, 99 ); // all bad values specified 18 19 cout 4 5 using std::cout; 6 using std::endl; 7 8 // include CreateAndDestroy class definition from create.h 9 #include "create.h" 10 11 // constructor 12 CreateAndDestroy::CreateAndDestroy( 13 int objectNumber, char *messagePtr ) 14 { 15 objectID = objectNumber; 16 message = messagePtr; 17 18 cout 5 6 using std::cout; 7 using std::endl; 8 9 // include CreateAndDestroy class definition from create.h 10 #include "create.h" 11 12 void create( void ); // prototype 13 14 // global object 15 CreateAndDestroy first( 1, "(global before main)" ); 16 17 int main() 18 { 19 cout 4 5 using std::cout; 6 7 #include 8 9 using std::setfill; 10 using std::setw; 11 12 // include definition of class Time from time3.h 13 #include "time3.h" 14 15 // constructor function to initialize private data; 16 // calls member function setTime to set variables; 17 // default values are 0 (see class definition) 18 Time::Time( int hr, int min, int sec ) 19 { 20 setTime( hr, min, sec ); 21 22 } // end Time constructor 23 time3.cpp (2 of 4) 24 // set hour, minute and second values 25 void Time::setTime( int h, int m, int s ) 26 { 27 setHour( h ); 28 setMinute( m ); 29 setSecond( s ); 30 31 } // end function setTime 32 33 // set hour value 34 void Time::setHour( int h ) 35 { 36 hour = ( h >= 0 && h = 0 && m = 0 && s 4 5 using std::cout; 6 using std::endl; 7 8 // include definition of class Time from time3.h 9 #include "time3.h" 10 11 void incrementMinutes( Time &, const int ); // prototype 12 13 int main() 14 { 15 Time t; // create Time object 16 17 // set time using individual set functions 18 t.setHour( 17 ); // set hour to valid value 19 t.setMinute( 34 ); // set minute to valid value 20 t.setSecond( 25 ); // set second to valid value 21 fig06_20.cpp(2 of 3) 22 // use get functions to obtain hour, minute and second 23 cout 5 6 using std::cout; 7 using std::endl; 8 9 // class Date definition 10 class Date { 11 12 public: 13 Date( int = 1, int = 1, int = 1990 ); // default constructor 14 void print(); 15 16 private: 17 int month; 18 int day; 19 int year; 20 21 }; // end class Date 22 fig06_24.cpp (2 of 3) 23 // Date constructor with no range checking 24 Date::Date( int m, int d, int y ) 25 { 26 month = m; 27 day = d; 28 year = y; 29 30 } // end Date constructor 31 32 // print Date in the format mm-dd-yyyy 33 void Date::print() 34 { 35 cout << month << '-' << day << '-' << year; 36 37 } // end function print 38 39 int main() 40 { 41 Date date1( 7, 4, 2002 ); 42 Date date2; // date2 defaults to 1/1/1990 43 fig06_24.cpp (3 of 3)fig06_24.cpp output (1 of 1) 44 cout << "date1 = "; 45 date1.print(); 46 cout << "\ndate2 = "; 47 date2.print(); 48 49 date2 = date1; // default memberwise assignment 50 51 cout << "\n\nAfter default memberwise assignment, date2 = "; 52 date2.print(); 53 cout << endl; 54 55 return 0; 56 57 } // end main date1 = 7-4-2002 date2 = 1-1-1990   After default memberwise assignment, date2 = 7-4-2002 

File đính kèm:

  • pptBài giảng Ngôn ngữ lập trình C++ - Chương 7 Kỹ thuật kế thừa.ppt
Tài liệu liên quan