Page 1
goupee.vn goupee.com Lập trình Chương5: Dẫn xuất/thừa kế và đa hình/đa xạ cuu duong than cong . com fb.com/goupee.vn 2/10/2017 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vn goupee.com Lập trình Chương5: Dẫn xuất/thừa kế và đa hình/đa xạ cuu duong than cong . com fb.com/goupee.vn 2/10/2017 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vnNội dung goupee.com 5.1 Dẫn xuất/thừa kế 5.2 Hàm ảo và cơ chế đa hình/đa xạ 5.3 Lớp thuần ảo 5.4 Kiểm soát truy nhập 5.5 Tương thích kiểu cuu duong than cong . com fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 2 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vn5.1Dẫn xuất và kế thừa goupee.com Nghiên cứu các thuộc tính và phương thức của các loại xe đạp sau Xe đạp (Bicycle) Tandem bicycle cuu duong than cong . com fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 3 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vn goupee.com Racing Bike Mountain Bike cuu duong than cong . com fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 4 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vn5.1Dẫn xuất và kế thừa (…) goupee.com Tandem bicycle là một loại xe đạp – Xe đạp có hai yên Mountain bicycle là một loại xe đạp – Xe đạp có khả năng chống sốc (lốp dầy và nhiều bắnh răng) Racing bicyle là một loại xe đạp – Xe đạp có cấu tạo khí động lực học nhẹ Tandem, mountain, racing bicycle là những loại xe đạp chuyên dụng – Có các thành phần cơ bản của một chiếc xe đạp – Cùng nguyên lý hoạt độngcuu duong than cong . com – Bổ sung thêm các thông tin khác fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 5 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vn5.1Dẫn xuất và kế thừa (…) goupee.com Cơ chế dẫn xuất/thừa kế là một kỹ thuật lập trình hướng đối tượng cho phép chuyên biệt hóa Dẫn xuất cho phép tạo ra một lớp mới (lớp dẫn xuất) của các đối tượng bằng cách sử dụng các lớp cũ như là các lớp cơ sở – Lớp dẫn xuất thừa hưởng các thuộc tính và hành vi của lớp “cha- mẹ” (lớp cơ sở) – Lớp dẫn xuất là một phiên bản chuyên biệt hóa của lớp “cha-mẹ” Lớp cơ sở - ” lớp cha mẹ” Bicycle cuu duong than cong . comMountainBikes RacingBikes TandemBikes Lớp dẫn xuấtfb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 6 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vnVí dụ minh họa sử dụng cơ chế dẫn xuấtgoupee.com Xây dựng các lớp biểu diễn về con người, sinh viên, giảng viên Các thuộc tính và phép toán cơ bản của lớp Person Tên lớp Person name: string age: int Thuộc tính gender: string set_name get_name set_age Phép toán/ get_agecuu duong than cong . comphương thức set_gender get_gender display fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 7 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vn goupee.com Các thuộc tính và phép toán cơ bản của lớp Student và Lecture Student Lecture name: string name: string age: int age: int gender: string gender: string class: string faculty: string id: int telnumber: int set_name set_name get_name get_name set_age set_age get_age get_age set_gender set_gender get_gender get_gender set_classget_class cuu duong than cong . comset_facultyget_faculty set_id set_telnumber get_id get_telnumber display display fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 8 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vn5.1Dẫn xuất và kế thừa (…) goupee.com Ba lớp trên giống nhau về: – Thuộc tính: Name Age gender – Phương thức set_name, get_name set_age, get_age set_gender, get_gender Khác nhau: lớp Student, Lecture có bổ sung thêm các thuộc tính và phương thức cuu duong than cong . com fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 9 CuuDuongThanCong.com https://fb.com/tailieudientucntt
Quan hệ lớp Person goupee.vn goupee.com name: string Lớp cơ sở age: int gender: string set_name get_name set_age get_age set_gender get_gender display Quan hệ dẫn xuất Lớp dẫn xuất Student Lecture class: string Faculty: string ID: int Telnumber: int set_class cuu duong than cong . comset_faculty get_class get_faculty set_ID set_telnumber get_ID get_telnumber display display fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 10 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vnThực hiện (Lớp Person) goupee.com //Khai báo lớp Person trong file person.h #include <iostream> #include <string> using namespace std; class Person{ string name; int age; string gender; public: Person(string,int,string); string get_name(); void set_name(string); int get_age(); void cuu duong than cong . comset_age(int); string get_gender(); void set_gender(string); void display() }; fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 11 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vnThực hiện (Lớp Person) goupee.com //Định nghĩa lớp Person trong file person.cpp #include “person.h” Person::Person(string _name,int _age, string _gender){ name = _name; age = _age; sex = _gender; } string Person::get_name(){ return name;} void Person::set_name(string _name){ name = _name;} int Person::get_age(){ return age;} void Person::set_age(int _age){ age = _age;} string Person::get_gender(){ return gender;} void Person::set_gender(string _gender){gender = _gender;} void Person::display(){ cout<<“Person:\n”; cout<<“Name:\t”<<name<< endl; cout<<“Age:cuu duong than cong . com\t”<<age<< endl; cout<<“Gender:\t”<<gender<< endl; } fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 12 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vnThực hiện (Lớp Student) goupee.com //Khai báo lớp Student trong file student.h class Student: public Person { string lop; int id; public: Student(string, int, string, string,int); void set_class(string c); string get_class(); void set_id(int i); int get_id(); void display(); }; cuu duong than cong . com fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 13 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vnThực hiện (Lớp Student) goupee.com //Định nghĩa lớp Student trong file student.cpp #include “student.h” Student::Student(string _n,int _a, string _g, string _l,int _id) :Person(_n,_a,_g){ lop = _l; id = _id; } void Student:: set_class(string c){lop = c;} string Student:: get_class(){ return lop;} void Student:: set_id(int i){id = i;} int Student:: get_id(){return id;} void Student:: display(){ Person::display();cuu duong than cong . com cout<<"class:\t"<<lop<<endl; cout<<"ID:\t"<<id<<endl; } fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 14 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vnThực hiện (Lớp Lecture) goupee.com // Khai báo lớp Lecture trong file lecture.h class Lecture:public Person{ string faculty; int telnumber; public: Lecture(string,int, string, string,int); void set_faculty(string f); string get_faculty(); void set_telnumber(int tel); int get_telnumber(); void display(); }; cuu duong than cong . com fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 15 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vnThực hiện (Lớp Lecture) goupee.com //Định nghĩa lớp Lecture trong file lecture.cpp #include “lecture.h” Lecture::Lecture(string _n,int _a, string _g, string _f,int _t) :Person(_n,_a,_g){ faculty = _f; telnumber = _t; } void Lecture:: set_faculty(string f){faculty = f;} string Lecture:: get_faculty(){ return faculty;} void Lecture:: set_telnumber(int tel){telnumber = tel;} int Lecture:: get_telnumber(){return telnumber;} void Lecture:: display(){ Person::display(); cout<<"Faculty:\t"<<faculty<<endl;cout<<"Telephonecuu duong than cong . comNumber:\t"<<telnumber<<endl; } fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 16 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vn Chương trình minh họa sử dụng 1 goupee.com //Thực hiện trong file main.cpp #include <iostream> #include “person.h” #include “student.h” #include “lecture.h” void main(){ Person per("John",21,"man"); Student stu("Marry",22,"woman","Electronics1-K53",20080001); Lecture lec("Michel",22,"man","Electronics Engineering",123456789); cout<<“Person:\t”<<per.get_name()<<“\t”<<per.get_age() <<“\t”<<per.get_gender()<<“\n\n”; cout<<“Student:\t”<<stu.get_name()<<“\t”<<stu.get_age()<<“\t” <<stu.get_gender()<<“\t”<<stu.get_class()<<“\t”<<stu.get_id()<<“\n\n”; cout<<“Lecture:\t”<<lec.get_name()<<“\t”<<lec.get_age()<<“\t” <<lec.get_gender() <<“\t”<<lec.get_faculty()<<“\t” <<lec.get_telnumber()<<“\n\n”; } Kết quả chạy chương trình cuu duong than cong . com fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 17 CuuDuongThanCong.com https://fb.com/tailieudientucntt
Chương trình minh họa sử dụng 2 goupee.vn goupee.com //Thực hiện trong file main.cpp void main(){ Person per("John",21,"man"); Student stu("Marry",22,"woman","Electronics1-K53",20080001); Lecture lec("Michel",22,"man","Electronics Engineering",123456789); per.display(); stu.display(); lec.display(); } Kết quả chạy chương trình cuu duong than cong . com fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 18 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vnCác dạng dẫn xuất/thừa kế goupee.com cuu duong than cong . com fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 19 CuuDuongThanCong.com https://fb.com/tailieudientucntt
goupee.vnTóm lược goupee.com Lớp dẫn xuất có hai mục đích cơ bản – Mở rộng các tính năng của lớp cơ sở – Thừa hưởng các thuộc tính và phép toán của lớp cơ sở – Cụ thể hóa các phép toán qua những phương thức khác nhau Ưu điểm của cơ chế thừa hưởng – Xây dựng một mô hình phần mềm hướng đối tượng dễ hiểu – Tiết kiệm được công việc thực hiện qua sử dụng lại các lớp cơ sở – Hạn chế lỗi qua cơ chế thừa hưởng cuu duong than cong . com fb.com/goupee.vn Chương 5: Dẫn xuất/thừa kế và đa hình/đa xạ 20 CuuDuongThanCong.com https://fb.com/tailieudientucntt
Affichage des 20 premières pages. Téléchargez le document pour le voir en entier.
Aucun commentaire pour le moment. Soyez le premier !
Commentaires (0)
Aucun commentaire pour le moment. Soyez le premier !