1 / 20

MIT-3511 Multimedia & Game Programming Chapter 9: Polymorphism

MIT-3511 Multimedia & Game Programming Chapter 9: Polymorphism. Outline 10.1 Introduction 10.2 Definition of Polymorphism 10.3 Virtual Functions 10.4 switch Statements & Polymorphic Programming 10.5 Abstract Base Classes and Concrete Classes 10.6 Polymorphism Reviews .

ulmer
Download Presentation

MIT-3511 Multimedia & Game Programming Chapter 9: Polymorphism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MIT-3511 Multimedia & Game ProgrammingChapter 9: Polymorphism Outline 10.1 Introduction 10.2 Definition of Polymorphism 10.3 Virtual Functions 10.4 switch Statements & Polymorphic Programming 10.5 Abstract Base Classes and Concrete Classes 10.6 Polymorphism Reviews

  2. 10.1 Introduction • Before getting into this section, it is recommended that you have a proper understanding of pointers and class inheritance. • virtual functions and polymorphism • One of the key features of derived classes is that a pointer to a derived class is type-compatible with a pointer to its base class. • Polymorphism is the art of taking advantage of this simple but powerful feature that brings OO to its full potential.

  3. 10.2 Definition of Polymorphism • The first type of Polymorphism allows us to reuse the same name for functions as long as the parameter list differs. Sometimes this type of Polymorphism is called Overloading. • The second type of Polymorphism allows object to choose correct methods between the Base class and the Derived class.

  4. 10.2.1 Pointer compatibility property class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width = a; height = b; } }; class CRectangle: public CPolygon { public: int area ( ) { return (width * height); } }; class CTriangle: public CPolygon { public: int area ( ) { return (width * height / 2); } }; void main ( ) { CRectangle rect; CTriangle trg1; CPolygon * ppoly1 = &rect; CPolygon * ppoly2 = &trg1; ppoly1->set_values (4, 5); ppoly1->set_values (4, 5); cout << rect.area( ) << endl; cout << trg1.area( ) << endl; } CPolygon CPolygon + width + height + set_value CRectangle CTriangle CTriangle CRectangle + width + width + height + height + set_value + area + set_value + area Derived classes’ own member functions. area( ) rect: CRectangle set_values( ) CPolygon CPolygon area( ) set_values( ) trg1: CTriangle

  5. In function main, the two pointers, ppoly1 and ppoly2, are point to objects of class CPolygon, and assigned with references to rect and trg1, because both are objects of classes derived from CPolygon, both are valid assignations. • *ppoly1 and *ppoly2 are of type CPolygon* and therefore we can only use these pointers to refer to the members that CRectangle and CTriangle inherit from CPolygon. • In order to use area( ) with the pointers to class CPolygon, this member should also have been declared in the class CPolygon, but the problem is that CRectangle and CTriangle implement different versions of area, therefore we cannot implement it in the base class.

  6. SOLUTION Virtual Member

  7. 10.3 virtual Functions • virtual functions • A member of a class that can be redefined in its derived classes is know as a virtual member. • virtual declaration: • Keyword virtual before function prototype in base-class virtual void draw() const;

  8. 1 // Chapter 9 : Virtual Function 24 int area() // own member function to return the area 25 { return ( width * height / 2 ); } 2 // Example 1 : Definition of derived classes 26 }; 3 #include <iostream> 27 4 using namespace std; 28 void main() { 5 29 CRectangle rect; 6 class CPolygon { 30 CTriangle trig; 7 protected:// accessible by derived classes 31 CPolygon poly; 8 int width, height; 32 9 public: 10 void set_values( int a, int b ); 11 { width = a; height = b; } 12 virtual int area( ) 13 { return 0;}; 15 14 }; 16 class CRectangle: public CPolygon { // derived class -- CRectangle 17 public: 18 int area() // own member function to return the area 19 { return ( width * height ); } 20 }; 21 22 class CTriangle: public CPolygon { // derived class -- CTriangle 23 public: 1. CPolygon class definition 1.1 Virtual Function definitions 1.2 CRectangle class definition 1.3 Function definitions 1.4 CTriangle class definition area( ) in the Base class has been declared as Virtual function.

  9. 33 CPolygon * ppoly1 = &rect; 36 39 ppoly3->set_values (4,5); 42 cout << ppoly2->area() << endl; 34 CPolygon * ppoly2 = &trig; 37 ppoly1->set_values (4,5); 40 43 cout << ppoly3->area() << endl; 35 CPolygon * ppoly3 = &poly; 38 ppoly2->set_values (4,5); 41 cout << ppoly1->area() << endl; 44 } Program Output 20 10 0 • A base-class pointer to a derived class object will call the correct area function • ppoly1->area();

  10. Therefore, what the virtualkeyword does is to allow a member of a derived class with the same name as one in the base class to be appropriately called from a pointer, and more precisely when the type of the pointer is a pointer to the base class but is pointing to an object of the derived class. • A class that declares or inherits a virtual function is called a polymorphic class.

  11. 10.4 switch Statements & Polymorphic programming • switch statement • E.g. switch (WhichArea) { case CircleArea : …….; break; case SquareArea: …….; break; case RectangleArea: …….; break; default: AnyArea; } • The concept of virtual functions and polymorphic programming is similar to switch logic. Programs take on a simplified appearance. This facilitates testing, debuging, program maintenance and bug avoidance

  12. 10.5 Using Polymorphism • Classes without Inheritance relationship cannot use Polymorphism. Therefore, we must create Base and Derived classes. CPolygon CTriangle + width + width + height + height + area () + set_value + area Polymorphism is allowed Polymorphism is not allowed CTriangle CRectangle CRectangle + width + width + width + height + height + height + set_value + area() + set_value + area() + set_value + area Derived classes’ own member functions.

  13. 10.5 How ? Create a Base class with PURE VIRTUAL function Which is function without Implementation at all. Abstract class : CPolygon Contains Pure Virtual Function, so that, Cannot instantiate object. e.g. CPolygon poly1; + width + height + virtual int area () = 0 Concrete class : CTriangle CRectangle Then you can create pointer of CPolygon pointing to Derived Objects to use Polymorphism. E.g. CRectangle rect; CPolygon * ppoly1 = &rect; ppoly1->area(); // Derived class // area function // called No Pure Virtual Function, can instantiate object. e.g. CRectangle rect; + set_value + area() + set_value + area() Derived classes’ own member functions.

  14. 10.5 Abstract and Concrete Classes • Abstract classes • Sole purpose is to provide a base class for other classes • No objects of an abstract base class can be instantiated • Too generic to define real objects (i.e., TwoDimensionalShape) • Can have pointers and references • Concrete classes • Classes that can instantiate objects • Provide specifics to make real objects (i.e., Square, Circle) • Making abstract classes • Declare one or more virtual functions as “pure” by initializing the function to zero • Example of a pure virtual function: virtual double earnings() = 0;

  15. 1 // Chapter 9 : Pure Virtual Function & Polymorphism 24 int area() // own member function to return the area 25 { return ( width * height / 2 ); } 2 // Example 2 : Definition of Abstract Base classes 26 }; 3 #include <iostream> 27 4 using namespace std; 28 void main() { 5 29 CRectangle rect; 6 class CPolygon { 30 CTriangle trig; 7 protected:// accessible by derived classes 31 8 int width, height; 32 9 public: 10 void set_values( int a, int b ); 11 { width = a; height = b; } 12 virtual int area( ) = 0; 15 14 }; 16 class CRectangle: public CPolygon { // derived class -- CRectangle 17 public: 18 int area() // own member function to return the area 19 { return ( width * height ); } 20 }; 21 22 class CTriangle: public CPolygon { // derived class -- CTriangle 23 public: 1. CPolygon class definition 1.1 Virtual Function definitions 1.2 CRectangle class definition 1.3 Function definitions 1.4 CTriangle class definition area( ) in the Base class has been declared as Pure Virtual function.

  16. 33 CPolygon * ppoly1 = &rect; 36 ppoly1->set_values (4,5); 34 CPolygon * ppoly2 = &trig; 37 ppoly2->set_values (4,5); 40 cout << ppoly1->area() << endl; 35 38 41 cout << ppoly2->area() << endl; 44 } Program Output 20 10 Virtual members and abstract classes grant C++ the polymorphic characteristics that make object-oriented programming such a useful instrument in big projects.

  17. 10.6 Polymorphism Reviews • Polymorphism • Ability for objects of different classes to respond differently to the same function call • Implemented through virtual functions • Base-class pointer (or reference) calls a virtual function • C++ chooses the correct overridden function in object • If non-virtual member function defined in multiple classes and called from base-class pointer then the base-class version is used • If called from derived-class pointer then derived-class version is used • Suppose print is not a virtual function Employee e, *ePtr = &e;HourlyWorker h, *hPtr = &h;ePtr->print(); // call base-class print functionhPtr->print(); // call derived-class print functionePtr=&h; // allowable implicit conversionePtr->print(); // still calls base-class print

  18. Questions • What are the name of the classes in an Inheritance relationship ?_____________________________________________ • What is the difference between a virtual and a non-virtual function ?_____________________________________________ • How can we make the following function to be a virtual function : int calAnswer( );  ___________________ • How can we make the above function to a Pure virtual function_____________________________________________ • What is a Pure Virtual function ?____________________________________________________________________________________________

  19. 6. What is an Abstract class ?________________________________________ 7. What is a Concrete class ?________________________________________ 8. What is the advantage of Polymorphism ?________________________________________________________________________________ 9. // Sample program for polymorphism (同名異式) • #include <iostream.h> • class Base • { • public: • Base(int i, int j) { x = i; y = j; } • // declare virtual function • virtual void show( ) { cout << "Base" << '\t' << x << '\t' << y << '\n'; } • protected: • int x, y; • };

  20. class Derived: public Base • { • public: • Derived(int a, int b, int c): Base(a, b) { z = c;} • // declare virtual function • virtual void show( ) { cout << "Derived" << '\t' << x << '\t' << y << '\t' << z << '\n'; } • protected: • int z; • }; • void main( ) • { • Base b1(12, 14); • Derived d1(16, 18, 20); • b1.show( ); // call Base::show( ) d1.show( ); // call Derived::show( ) • Base *bPtr; • Derived *dPtr; • bPtr = &b1; • bPtr->show( ); ( Question : Which show( ) is called ? ____________) • bPtr = &d1; • bPtr->show( ); ( Question : Which show( ) is called ? ____________) • } What is the output? : _____________________________________________________________________________________

More Related