Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ics212-26-polymorphism

 ics212-26-polymorphism

Avatar for William Albritton

William Albritton

November 12, 2015
Tweet

More Decks by William Albritton

Other Decks in Programming

Transcript

  1. Memory Allocation  Static and Dynamic Binding  Virtual Functions

     Polymorphism  Abstract and Concrete Classes  Overloading and Overriding
  2. Inheritance Example  Base class Point2D  Represents a point

    in two dimensional space: (x, y)  Derived class Point3D  Represents a point in three dimensional space: (x, y, z)  See example program at: points.cpp
  3. Class Point2D  Overloaded constructors: Point2D(), Point2D(double, double)  Member

    functions: setX(), setY(), getX(), getY(), distance()  Overloaded global friend function: operator<<()  Data members: x, y, ZERO
  4. Class Point2D Data Members  Data members represent a point

    in two-dimensional space, somewhere on the x and y-axis  Using “protected” member access, so the derived classes can access the Point2D’s data members protected: double x; double y;
  5. Class Point2D Constructor  Constructor initializing the data members Point2D(double

    x2, double y2){ //initialize the data members x = x2; y = y2; }
  6. Point2D’s distance() Function  Calculates the distance from point (0,

    0)  Can be the magnitude of a vector or length of a line double distance(){ return sqrt(x*x + y*y); }
  7. Class Point3D  Overloaded constructors: Point3D(), Point3D(double, double, double) 

    Inherited member functions: setX(), setY(), getX(), getY()  Overridden member function: distance()  New member function: setZ(), getZ()  Overloaded global friend function: operator<<()  Inherited data members: x, y, ZERO  New data members: z
  8. Class Point3D Data Members  Data members represent a point

    in three-dimensional space, somewhere on the x, y, and z-axis  Using “protected” member access, so the derived classes can access the Point3D’s data members //inherits x and y from Point2D protected: double z;
  9. Class Point3D Constructor  Initialize the data members x and

    y in base class Point2D and data member z in derived class Point3D Point3D(double x2, double y2, double z2):Point2D(x2,y2){ //initialize data member z = z2; }
  10. Point3D’s distance() Function  Calculates the distance from point (0,

    0, 0)  Can be the magnitude of a vector or length of a line  Overrides the Point2D’s distance() function double distance(){ return sqrt(x*x + y*y + z*z); }
  11. Issue with distance() Function  The distance() function for Point2D

    will be called instead of the distance() function for Point3D Point3D point(3, 4, 5); Point2D *pointer2D = &point; //returns 5.0, instead of 7.07106 cout<<pointer2D->distance()<<endl;
  12. Static Binding  Normally, which type of function gets called

    depends on the type of pointer  This is due to static binding, which means the system identifies which function to call at compile time Point3D point(3, 4, 5); Point2D *pointer2D = &point; //returns 5.0, instead of 7.07106 cout<<pointer2D->distance()<<endl;
  13. Dynamic Binding  We can use dynamic binding or late

    binding to determine which version of function to call at runtime  Look at what type of object the pointer points to  Call the appropriate function based on this type Point3D point(3, 4, 5); Point2D *pointer2D = &point; //correctly returns 7.07106 cout<<pointer2D->distance()<<endl;
  14. Virtual Function Calls  Virtual functions allow derived classes to

    have their own version of the base class functions, which can be bound at runtime (dynamic binding) Point3D point(3, 4, 5); Point2D *pointer2D = &point; //returns 7.07106 cout<<pointer2D->distance()<<endl;
  15. Virtual Function Definition  To enable dynamic binding, add the

    keyword “virtual” at the beginning of the function prototype (1st line of function) in the base class (see virtual.cpp) virtual double distance(){ return sqrt(x*x + y*y); }
  16. Virtual Function  A function is virtual if:  The

    function itself is declared virtual  Or, there is a base class function with the same signature that is declared virtual  A signature consists of function’s name plus the types of all the parameters, in order, of the function
  17. UH Community Colleges  Base class: CommunityCollege  Derived classes:

    HawaiiCC, HonoluluCC, KapiolaniCC, KauaiCC, LeewardCC, MauiCollege and WindwardCC  CommunityCollege has virtual function name(), which returns the name of the Community College virtual char * name(){ return “Community College”; }
  18. Leeward Community College  All derived classes have overridden function

    name(), which returns the name of the Community College  For example, here is the name() function definition for class LeewardCC char * name(){ return “Leeward Community College”; }
  19. Declare an Array of Pointers  In the main() function,

    we declare an array of pointers to the seven UH Community College objects  A base class pointer (CommunityCollege *) can contain a pointer to any object of its derived classes (the seven UH Community College objects) CommunityCollege *colleges[SEVEN];
  20. Initialize an Array of Pointers  Initialize each array element

    to the address of each object on the heap (new makes an object on the heap)
  21. Initialize an Array of Pointers colleges[0] = new HawaiiCC(); colleges[1]

    = new HonoluluCC(); colleges[2] = new KapiolaniCC(); colleges[3] = new KauaiCC(); colleges[4] = new LeewardCC(); colleges[5] = new MauiCollege(); colleges[6] = new WindwardCC();
  22. Loop through Array  Next, we loop through the array

    of pointers  See what happens with and without the keyword “virtual” in the overridden function name()  See example program: colleges.cpp for(int i=0;i<SEVEN;i++){ cout<<colleges[i]->name()<<endl; }
  23. Polymorphism Definition  Polymorphism is the ability of objects of

    different classes related by inheritance to respond differently to the same member function call  In other words, polymorphism calls the correct function, according to the object’s type  In Greek, polymorphism means “many shapes”
  24. Polymorphism Implementation  To get polymorphism to work in C++,

    we need: 1. A base class 2. One or more derived classes 3. Overridden functions 4. Virtual functions 5. Pointers 6. Dynamic binding
  25. Polymorphism Examples  We implemented polymorphism in these examples: 

    With the distance() function in virtual.cpp  With the name() function in colleges.cpp  With the evaluate() function in binary.cpp
  26. Class Hierarchy  Base class Node  Has derived classes

    BinaryOperator and Data  Base class BinaryOperator  Has derived classes Plus and Times  All classes have virtual function evaluate()
  27.  In the main() function, we create and evaluate a

    binary expression tree, which is a binary tree where the nodes are either operators or numbers  Evaluates expression: 5 * 4 + 3  See example program: binary.cpp Binary Expression Tree 5 4 * 3 +
  28. Abstract Classes  An abstract class is a base class

    from which we cannot instantiate objects  We can declare a pointer from an abstract class  A class is made abstract by declaring one or more of its virtual functions “pure” by setting the function equal to zero
  29. Pure Virtual Function  Virtual function name() is made “pure”

    by deleting the body of the function and setting the prototype to zero  See example program: abstract.cpp class CommunityCollege { public: virtual char * name() = 0; };
  30. Concrete Classes  Contrary to an abstract class, we can

    instantiate objects from a concrete class
  31. Overloaded Function Definition  Two or more functions with same

    name, but different signatures Point2D(){ x = ZERO; y = ZERO; } Point2D(double x2, double y2){ x = x2; y = y2; }
  32. Overloaded Function Call  Uses static binding, so overloading identifies

    which function to call at compile time  Overloaded function operator<<() only looks at pointer type (Point2D), not what it points to (Point3D object) Point3D point(3, 4, 5); cout<<point; //(3, 4, 5) Point2D *pointer2D = &point; cout<<*pointer2D; //(3, 4)
  33. Overridden Function Definition  Same name and same signature, but

    in different classes related by inheritance class Point2D{ virtual double distance(){ return sqrt(x*x + y*y); } ... }; class Point3D: public Point2D{ double distance(){ return sqrt(x*x + y*y + z*z); } ...
  34. Overridden Function Call  With pointers and virtual functions, overriding

    uses dynamic binding, so overriding identifies which function to call at runtime  This is polymorphism, which calls the correct function, according to the object’s type Point3D point(3, 4, 5); Point2D *pointer2D = &point; cout<<pointer2D->distance(); //7.07107
  35. Memory Management  Static and Dynamic Binding  Virtual Functions

     Polymorphism  Abstract and Concrete Classes  Overloading and Overriding