Polymorphism Fall 2008What is PolymorphismPolymorphismFunction OverridingSlide 5Slide 6Slide 7DLList.h ExamplePolymorphismFall 2008 Dr. David A. [email protected] is Polymorphism Essentially, it is a programming feature that allows us to take data values of different types and use what would appear to be the same interface. Example: Printing out an integer and a floating point number internally requires different functions. But to a C++ programmer they both appear to be the same. int a=1;double b=0.0; cout << a << endl;cout << b << endl;Polymorphism•Note that we can already have multiple member functions with the same name but difference parameters (signature). Take as an example the following constructors: DLLink::DLLink();DLLink::DLLink(int CRN);DLLink::DLLink(int CRN, string t);•The compiler knows how to differentiate between the three because of the different parameter types and number of parameters.Function Overriding // Suppose we have the // following base classclass Student{ public: void GradeReport();… ( Other stuff)};// Take the following derived// classes: class Grad : public Student{ public: void GradeReport(); };class Undergrad: public Student{ public GradeReport(); };// Look at the definitions // and subsequent calls to// GradeReportStudent S; Grad G; Undergrad U; S.GradeReport(); // Calls // Student // Version. G.GradeReport(); // Calls // Grad // VersionU.GradeReport(); // Calls // Undergrad // Version// Here is a way to override // The override. // This has the Undergraduate// member function calling the// base class version. class Grad : public Student{ public: void GradeReport(); };class Undergrad: public Student{ Student::GradeReport(); };DLList.h Exampleifndef DLLIST_H#define DLLIST_H#include <string>using namespace std;class Node {public: int CourseRefNum; string Title; string CourseNumber; int Section; string Instructor; int SeatsTotal; int SeatsAvail; string Building; string Room; Node * Prev; Node * Next; }; class DLList: public Node
View Full Document