Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 12 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
12
Dung lượng
222,49 KB
Nội dung
N i dung L P TRÌNH NG IT NG VIRTUAL METHOD & POLYMORPHISM Tr n Ph c Tu n tuantp@hcmup.edu.vn http://giaotrinh.tranphuoctuan.com i k t t nh (Static binding) i k t ng (Dynamic binding) Ch y ch ng trình Bài toán th c t Ph ng th c o, a hình Ph ng th c h y b o Ph ng th c thu n o, l p tr u t 9/6/2009 Static binding TPTU N - LTH T 2 Dynamic binding class Circle { int x,y,r; public: Circle (int xx, int yy, int rr) { x=xx; y=yy; r=rr; } void print() { cout [...]... Summary Virtual Method is a way to make polymorphism Syntax for virtual method: virtual ReturnType Method (parameters) ReturnType virtual Method (parameters) Compiler will determine the right method will be called using a virtual function table for every class which contains virtual methods Pure virtual method is a virtual method but it has no code Syntax for pure virtual method: virtual ReturnType Method. .. code Syntax for pure virtual method: virtual ReturnType Method (parameters)=0; TPTU N - LTH T 9/6/2009 45 Abstract class is a result of so-high generation Abstract class must have at least one pure virtual method You can not create an object of abstract class but you can declare a pointer to it then, it points to an object of a concrete subclass 9/6/2009 TPTU N - LTH T 46 9/6/2009 TPTU N - LTH T 48 Exercises ... 43 9/6/2009 TPTU N - LTH T 44 Summary Summary Virtual Method is a way to make polymorphism Syntax for virtual method: virtual ReturnType Method (parameters) ReturnType virtual Method (parameters)... right method will be called using a virtual function table for every class which contains virtual methods Pure virtual method is a virtual method but it has no code Syntax for pure virtual method: ... ch Ch y ch ng trình ng trình Polymorphism ability occurs only when you use a pointer to an object and used-methods of classes are virtual methods virtual ReturnType or ReturnType virtual are accepted