Bài giảng Lập trình C++ - Chương 9: Lập trình hướng đối tượng - Kế thừa

Outline
9.1 Giới thiệu
9.2 Các lớp cơ sở và lớp dẫn xuất
9.3 dữ liệu thành viên quyền protected
9.4 Mối quan hệ giữa các lớp cơ sở và lớp dẫn xuất
9.5 Case Study: cây kế thừa 3 mức
9.6 Hàm Constructors and Destructors trong lớp dẫn xuất
9.7 Kế thừa public, protected và private

 

ppt80 trang | Chuyên mục: C/C++ | Chia sẻ: dkS00TYs | Lượt xem: 1719 | Lượt tải: 1download
Tóm tắt nội dung Bài giảng Lập trình C++ - Chương 9: Lập trình hướng đối tượng - 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 xuất: Car Nhỏ hơn, tập con xác định của lớp vihicle * 9.2 Các lớp cơ sở và lớp dẫn xuất Các ví dụ về kế thừa * 9.2 Các lớp cơ sở và lớp dẫn xuất Phân cấp kế thừa Mối quan hệ kế thừa: Cấu trúc cây kế thừa Mỗi lớp trở thành Lớp cơ sở Cung cấp dữ liệu và phương thức cho các lớp khác Hoặc là Lớp dẫn xuất Kế thừa dữ liệu và phương thức từ các lớp khác * Fig. 9.2	Inheritance hierarchy for university CommunityMembers. * Fig. 9.3	Inheritance hierarchy for Shapes. * 9.2 Các lớp cơ sở và lớp dẫn xuất Kế thừa public Được xác định với: 	Class TwoDimensionalShape : public Shape Lớp TwoDimensionalShape kế thừa từ Shape Thành viên private của lớp cơ sở Không truy cập trực tiếp được Vẫn có thể thể được kế thừa Xứ lý thông quả các phương thức được kế thừa Lớp cơ sở public and và thành viên có quyền truy cập protected Được kế thừa (truy cập trực tiếp) Hàm friend Không được kế thừa * 9.3 Các thành viên có quyền truy cập protected Quyền truy cập protected Là quyền trung gian giữa public và private Các thành viên protected được truy cập từ Các phương thức của lớp cơ sở Các hàm bạn của lớp cơ sở Các phương thức của lớp dẫn xuất Các hàm bạn của lớp dẫn xuất Các thành viên của lớp dẫn xuất Có các thành viên public và protected của lớp cơ sở * 9.4 	Mối quan hệ giữa các lớp cơ sở và các lớp dẫn xuất Mối quan hệ giữa các lớp cơ sở và các lớp dẫn xuất Ví dụ: cây kết thừa Point/circle Point x-y coordinate pair Circle x-y coordinate pair Radius * point.h (1 of 1) 1 // Fig. 9.4: point.h 2 // Point class definition represents an x-y coordinate pair. 3 #ifndef POINT_H 4 #define POINT_H 5 6 class Point { 7 8 public: 9 Point( int = 0, int = 0 ); // default constructor 10 11 void setX( int ); // set x in coordinate pair 12 int getX() const; // return x from coordinate pair 13 14 void setY( int ); // set y in coordinate pair 15 int getY() const; // return y from coordinate pair 16 17 void print() const; // output Point object 18 19 private: 20 int x; // x part of coordinate pair 21 int y; // y part of coordinate pair 22 23 }; // end class Point 24 25 #endif * point.cpp (1 of 3) 1 // Fig. 9.5: point.cpp 2 // Point class member-function definitions. 3 #include 4 5 using std::cout; 6 7 #include "point.h" // Point class definition 8 9 // default constructor 10 Point::Point( int xValue, int yValue ) 11 { 12 x = xValue; 13 y = yValue; 14 15 } // end Point constructor 16 17 // set x in coordinate pair 18 void Point::setX( int xValue ) 19 { 20 x = xValue; // no need for validation 21 22 } // end function setX 23 * point.cpp (2 of 3) 24 // return x from coordinate pair 25 int Point::getX() const 26 { 27 return x; 28 29 } // end function getX 30 31 // set y in coordinate pair 32 void Point::setY( int yValue ) 33 { 34 y = yValue; // no need for validation 35 36 } // end function setY 37 38 // return y from coordinate pair 39 int Point::getY() const 40 { 41 return y; 42 43 } // end function getY 44 * point.cpp (3 of 3) 45 // output Point object 46 void Point::print() const 47 { 48 cout 4 5 using std::cout; 6 using std::endl; 7 8 #include "point.h" // Point class definition 9 10 int main() 11 { 12 Point point( 72, 115 ); // instantiate Point object 13 14 // display point coordinates 15 cout 4 5 using std::cout; 6 7 #include "circle.h" // Circle class definition 8 9 // default constructor 10 Circle::Circle( int xValue, int yValue, double radiusValue ) 11 { 12 x = xValue; 13 y = yValue; 14 setRadius( radiusValue ); 15 16 } // end Circle constructor 17 18 // set x in coordinate pair 19 void Circle::setX( int xValue ) 20 { 21 x = xValue; // no need for validation 22 23 } // end function setX 24 * circle.cpp (2 of 4) 25 // return x from coordinate pair 26 int Circle::getX() const 27 { 28 return x; 29 30 } // end function getX 31 32 // set y in coordinate pair 33 void Circle::setY( int yValue ) 34 { 35 y = yValue; // no need for validation 36 37 } // end function setY 38 39 // return y from coordinate pair 40 int Circle::getY() const 41 { 42 return y; 43 44 } // end function getY 45 * circle.cpp (3 of 4) 46 // set radius 47 void Circle::setRadius( double radiusValue ) 48 { 49 radius = ( radiusValue 4 5 using std::cout; 6 using std::endl; 7 using std::fixed; 8 9 #include 10 11 using std::setprecision; 12 13 #include "circle.h" // Circle class definition 14 15 int main() 16 { 17 Circle circle( 37, 43, 2.5 ); // instantiate Circle object 18 19 // display point coordinates 20 cout 4 5 using std::cout; 6 7 #include "circle2.h" // Circle2 class definition 8 9 // default constructor 10 Circle2::Circle2( int xValue, int yValue, double radiusValue ) 11 { 12 x = xValue; 13 y = yValue; 14 setRadius( radiusValue ); 15 16 } // end Circle2 constructor 17 * circle2.cpp (2 of 3) 18 // set radius 19 void Circle2::setRadius( double radiusValue ) 20 { 21 radius = ( radiusValue 4 5 using std::cout; 6 7 #include "point2.h" // Point2 class definition 8 9 // default constructor 10 Point2::Point2( int xValue, int yValue ) 11 { 12 x = xValue; 13 y = yValue; 14 15 } // end Point2 constructor 16 17 // set x in coordinate pair 18 void Point2::setX( int xValue ) 19 { 20 x = xValue; // no need for validation 21 22 } // end function setX 23 * point2.cpp (2 of 3) 24 // return x from coordinate pair 25 int Point2::getX() const 26 { 27 return x; 28 29 } // end function getX 30 31 // set y in coordinate pair 32 void Point2::setY( int yValue ) 33 { 34 y = yValue; // no need for validation 35 36 } // end function setY 37 38 // return y from coordinate pair 39 int Point2::getY() const 40 { 41 return y; 42 43 } // end function getY 44 * point2.cpp (3 of 3) 45 // output Point2 object 46 void Point2::print() const 47 { 48 cout 4 5 using std::cout; 6 7 #include "circle3.h" // Circle3 class definition 8 9 // default constructor 10 Circle3::Circle3( int xValue, int yValue, double radiusValue ) 11 { 12 x = xValue; 13 y = yValue; 14 setRadius( radiusValue ); 15 16 } // end Circle3 constructor 17 18 // set radius 19 void Circle3::setRadius( double radiusValue ) 20 { 21 radius = ( radiusValue 4 5 using std::cout; 6 using std::endl; 7 using std::fixed; 8 9 #include 10 11 using std::setprecision; 12 13 #include "circle3.h" // Circle3 class definition 14 15 int main() 16 { 17 Circle3 circle( 37, 43, 2.5 ); // instantiate Circle3 object 18 19 // display point coordinates 20 cout 4 5 using std::cout; 6 7 #include "point3.h" // Point3 class definition 8 9 // default constructor 10 Point3::Point3( int xValue, int yValue ) 11 : x( xValue ), y( yValue ) 12 { 13 // empty body 14 15 } // end Point3 constructor 16 17 // set x in coordinate pair 18 void Point3::setX( int xValue ) 19 { 20 x = xValue; // no need for validation 21 22 } // end function setX 23 * point3.cpp (2 of 3) 24 // return x from coordinate pair 25 int Point3::getX() const 26 { 27 return x; 28 29 } // end function getX 30 31 // set y in coordinate pair 32 void Point3::setY( int yValue ) 33 { 34 y = yValue; // no need for validation 35 36 } // end function setY 37 38 // return y from coordinate pair 39 int Point3::getY() const 40 { 41 return y; 42 43 } // end function getY 44 * point3.cpp (3 of 3) 45 // output Point3 object 46 void Point3::print() const 47 { 48 cout 4 5 using std::cout; 6 7 #include "circle4.h" // Circle4 class definition 8 9 // default constructor 10 Circle4::Circle4( int xValue, int yValue, double radiusValue ) 11 : Point3( xValue, yValue ) // call base-class constructor 12 { 13 setRadius( radiusValue ); 14 15 } // end Circle4 constructor 16 17 // set radius 18 void Circle4::setRadius( double radiusValue ) 19 { 20 radius = ( radiusValue 4 5 using std::cout; 6 using std::endl; 7 using std::fixed; 8 9 #include 10 11 using std::setprecision; 12 13 #include "circle4.h" // Circle4 class definition 14 15 int main() 16 { 17 Circle4 circle( 37, 43, 2.5 ); // instantiate Circle4 object 18 19 // display point coordinates 20 cout 4 5 using std::cout; 6 7 #include "cylinder.h" // Cylinder class definition 8 9 // default constructor 10 Cylinder::Cylinder( int xValue, int yValue, double radiusValue, 11 double heightValue ) 12 : Circle4( xValue, yValue, radiusValue ) 13 { 14 setHeight( heightValue ); 15 16 } // end Cylinder constructor 17 * cylinder.cpp(2 of 3) 18 // set Cylinder's height 19 void Cylinder::setHeight( double heightValue ) 20 { 21 height = ( heightValue 4 5 using std::cout; 6 using std::endl; 7 using std::fixed; 8 9 #include 10 11 using std::setprecision; 12 13 #include "cylinder.h" // Cylinder class definition 14 15 int main() 16 { 17 // instantiate Cylinder object 18 Cylinder cylinder( 12, 23, 2.5, 5.7 ); 19 20 // display point coordinates 21 cout 4 5 using std::cout; 6 using std::endl; 7 8 #include "point4.h" // Point4 class definition 9 10 // default constructor 11 Point4::Point4( int xValue, int yValue ) 12 : x( xValue ), y( yValue ) 13 { 14 cout 4 5 using std::cout; 6 using std::endl; 7 8 #include "circle5.h" // Circle5 class definition 9 10 // default constructor 11 Circle5::Circle5( int xValue, int yValue, double radiusValue ) 12 : Point4( xValue, yValue ) // call base-class constructor 13 { 14 setRadius( radiusValue ); 15 16 cout 5 6 using std::cout; 7 using std::endl; 8 9 #include "circle5.h" // Circle5 class definition 10 11 int main() 12 { 13 { // begin new scope 14 15 Point4 point( 11, 22 ); 16 17 } // end scope 18 19 cout << endl; 20 Circle5 circle1( 72, 29, 4.5 ); 21 22 cout << endl; 23 Circle5 circle2( 5, 5, 10 ); 24 25 cout << endl; * fig09_29.cpp(2 of 2)fig09_29.cppoutput (1 of 1) 26 27 return 0; // indicates successful termination 28 29 } // end main Point4 constructor: [11, 22] Point4 destructor: [11, 22]   Point4 constructor: [72, 29] Circle5 constructor: Center = [72, 29]; Radius = 4.5   Point4 constructor: [5, 5] Circle5 constructor: Center = [5, 5]; Radius = 10   Circle5 destructor: Center = [5, 5]; Radius = 10 Point4 destructor: [5, 5] Circle5 destructor: Center = [72, 29]; Radius = 4.5 Point4 destructor: [72, 29] * 9.8 	public, protected and private Inheritance 

File đính kèm:

  • pptBài giảng Lập trình C++ - Chương 9 Lập trình hướng đối tượng - Kế thừa.ppt