Chapter 15 Polymorphism and Virtual Functions

DiscountSale’s member function bill() implemented ,ifferently than Sale’s Particular to "discounts". Member functions savings and " | Chapter 15 Polymorphism and Virtual Functions Learning Objectives Virtual Function Basics Late binding Implementing virtual functions When to use a virtual function Abstract classes and pure virtual functions Pointers and Virtual Functions Extended type compatibility Downcasting and upcasting C++ "under the hood" with virtual functions Virtual Function Basics Polymorphism Associating many meanings to one function Virtual functions provide this capability Fundamental principle of object-oriented programming! Virtual Existing in "essence" though not in fact Virtual Function Can be "used" before it’s "defined" Figures Example Best explained by example: Classes for several kinds of figures Rectangles, circles, ovals, etc. Each figure an object of different class Rectangle data: height, width, center point Circle data: center point, radius All derive from one parent-class: Figure Require function: draw() Different instructions for each figure Figures Example 2 Each class needs different . | Chapter 15 Polymorphism and Virtual Functions Learning Objectives Virtual Function Basics Late binding Implementing virtual functions When to use a virtual function Abstract classes and pure virtual functions Pointers and Virtual Functions Extended type compatibility Downcasting and upcasting C++ "under the hood" with virtual functions Virtual Function Basics Polymorphism Associating many meanings to one function Virtual functions provide this capability Fundamental principle of object-oriented programming! Virtual Existing in "essence" though not in fact Virtual Function Can be "used" before it’s "defined" Figures Example Best explained by example: Classes for several kinds of figures Rectangles, circles, ovals, etc. Each figure an object of different class Rectangle data: height, width, center point Circle data: center point, radius All derive from one parent-class: Figure Require function: draw() Different instructions for each figure Figures Example 2 Each class needs different draw function Can be called "draw" in each class, so: Rectangle r; Circle c; (); //Calls Rectangle class’s draw (); //Calls Circle class’s draw Nothing new here yet Figures Example: center() Parent class Figure contains functions that apply to "all" figures; consider: center(): moves a figure to center of screen Erases 1st, then re-draws So Figure::center() would use function draw() to re-draw Complications! Which draw() function? From which class? Figures Example: New Figure Consider new kind of figure comes along: Triangle class derived from Figure class Function center() inherited from Figure Will it work for triangles? It uses draw(), which is different for each figure! It will use Figure::draw() won’t work for triangles Want inherited function center() to use function Triangle::draw() NOT function Figure::draw() But class Triangle wasn’t even WRITTEN when Figure::center() was! Doesn’t know "triangles"! Figures Example: Virtual! Virtual functions are the answer Tells

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
2    211    1    29-04-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.