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
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;
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
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;
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; }
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); }
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;
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;
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;
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;
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); }
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
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”; }
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”; }
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];
= new HonoluluCC(); colleges[2] = new KapiolaniCC(); colleges[3] = new KauaiCC(); colleges[4] = new LeewardCC(); colleges[5] = new MauiCollege(); colleges[6] = new WindwardCC();
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; }
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”
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 +
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
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; };
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)
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); } ...
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