1 / 79

Inheritance, Polymorphism, and Virtual Functions

Inheritance

lupita
Télécharger la présentation

Inheritance, Polymorphism, and Virtual Functions

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. Inheritance, Polymorphism, and Virtual Functions

    2. Inheritance & Polymorphism 2 What Is Inheritance? Provides a way to create a new class from an existing class The new class is a specialized version of the existing class It inherits all the member variables and functions (except the constructors and destructor) of the class it is based on

    3. Inheritance & Polymorphism 3 Example: Insects

    4. Inheritance & Polymorphism 4 The "is a" Relationship Inheritance establishes an "is a" relationship between classes. A poodle is a dog A car is a vehicle A flower is a plant A football player is an athlete A savings account is a type of bank account

    5. Inheritance & Polymorphism 5 Inheritance Terminology and Notation Base class (or parent) inherited from Derived class (or child) inherits from the base class Notation: class Student // base class { . . . }; class UnderGrad : public student { // derived class . . . };

    6. Inheritance & Polymorphism 6 Inheritance Terminology and Notation The derived class inherits the member variables and member functions of the base class without any of them being rewritten New member variables and functions may be added to the derived class to make it more specialized than the base class

    7. Inheritance & Polymorphism 7 Back to the is a Relationship An object of a derived class 'is a(n)' object of the base class Example: a Secretary is an Employee a Stock is an Investment A derived object has all of the characteristics of the base class

    8. Inheritance & Polymorphism 8 Base class GradedActivity Create a base class GradedActivity that holds the numeric score and provides a method to calculate the equivalent letter grade This base class provides a general way of storing and calculating a letter grade for any type of scoring For example, quizzes, midterm exams, final exams, essays, lab reports

    9. Inheritance & Polymorphism 9 #ifndef GRADEDACTIVITY_H #define GRADEDACTIVITY_H // GradedActivity class declaration class GradedActivity { private: double score; // To hold the numeric score public: // Default constructor GradedActivity() { score = 0.0; } // Constructor GradedActivity(double s) { score = s; } // Mutator function void setScore(double s) { score = s; } // Accessor functions double getScore() const { return score; } char getLetterGrade() const; }; #endif

    10. Inheritance & Polymorphism 10 Inheritance GradedActivity.cpp

    11. Inheritance & Polymorphism 11 Base class GradedActivity The numeric score for each type of exam can be calculated differently Equal point value for all questions Different point values for each questions Extra credit questions The class, FinalExam, is a derived class of GradedActivity This is indicated in FinalExam.h by the line of code class FinalExam : public GradedActivity FinalExam inherits the instance variable score and all the methods of the class GradedActivity Adds on its own methods specifically needed for grading a final exam Final Exam is a Graded Activity

    12. Inheritance & Polymorphism 12 Inheritance FinalExam.h

    13. Inheritance & Polymorphism 13 Inheritance FinalExam.cpp

    14. Inheritance & Polymorphism 14 Inheritance FinalExam.cpp

    15. Inheritance & Polymorphism 15 Inheritance example FinalExam.cpp runs Instantiates object test of class FinalExam Invokes constructor of two parameters Invokes method set(questions,missed) Calculates numeric score Invokes setScore method of GradedActivity to set the instance variable score

    16. Inheritance & Polymorphism 16 Inheritance example Think of the base class as being contained within the derived class so that FinalExam has access to all of the variables and methods of GradedActivity

    17. Inheritance & Polymorphism 17 Inheritance example

    18. Inheritance & Polymorphism 18 Getting in Shape Base class Shape has an instance variable area and functions getArea and setArea Derived class Circle has its own instance variable radius and functions getRadius and setRadius Note that setRadius calls function setArea and passes the area of the circle using the formula p*r2 This sets the value of area in the base class. Since it is private , Circle can access it only via the public functions

    19. Inheritance & Polymorphism 19 Getting in Shape class Shape{ private: double area; public: void setArea(double a) {area = a;} double getArea() {return area;} }; class Circle: public Shape { private: double radius; public: void setRadius(double r){ radius = r; setArea(3.14*r*r); } double getRadius() {return radius;} };

    20. Inheritance & Polymorphism 20 Getting in Shape #include <iostream> #include "Circle.h" using namespace std; int main() { Circle c; c.setRadius(10.0); cout<<"Area of the circle is "<<c.getArea()<<endl; return 0; } Area of the circle is 314 Instantiate an object of the Circle class; this automatically instantiates an object of the Shape class Call the setRadius function this calls the setArea function of the Shape class which sets the private instance variable area of the Shape class

    21. Inheritance & Polymorphism 21 Getting in Shape Create an new class named Rectangle. It has private instance variables length and width of type double public functions getLength, getWidth and setLengthAndWidth setLengthAndWidth calls setArea with the value length*width Add code to the main program to instantiate a Rectangle object r, set the length to 6 and width to 9 and then call the getArea function on r

    22. Inheritance & Polymorphism 22 Getting in Shape class Rectangle: public Shape { private: double length; double width; public: void setLengthAndWidth(double l, double w){ length = l; width = w; setArea(length*width); } double getLength() {return length;} double getWidth() {return width;} };

    23. Inheritance & Polymorphism 23 Getting in Shape #include <iostream> #include "Circle.h" #include "Rectangle.h" using namespace std; int main() { Circle c; c.setRadius(10.0); cout<<"Area of the circle is "<<c.getArea()<<endl; Rectangle r; r.setLengthAndWidth(6,9); cout<<"Area of the rectangle is "<<r.getArea()<<endl; return 0; } Area of the circle is 314 Area of the rectangle is 54

    24. Inheritance & Polymorphism 24 What Does a Child Have? An object of the derived class has: all members defined in child class all members declared in parent class An object of the derived class can use: all public members defined in child class all public members defined in parent class

    25. Inheritance & Polymorphism 25 Protected Members and Class Access protected member access specification: like private, but accessible by objects of derived class Class access specification: determines how private, protected, and public members of base class are inherited by the derived class

    26. Inheritance & Polymorphism 26 Class Access Specifiers public object of derived class can be treated as object of base class (not vice-versa) protected more restrictive than public, but allows derived classes to know details of parents private prevents objects of derived class from being treated as objects of base class.

    27. Inheritance & Polymorphism 27 Class Access Specifiers

    28. Inheritance & Polymorphism 28 Inheritance vs. Access

    29. Inheritance & Polymorphism 29 More Inheritance vs. Access

    30. Inheritance & Polymorphism 30 More Inheritance vs. Access

    31. Inheritance & Polymorphism 31 More Inheritance vs. Access

    32. Inheritance & Polymorphism 32 Constructors and Destructors in Base and Derived Classes Derived classes can have their own constructors and destructors When an object of a derived class is created, the base classs constructor is executed first, followed by the derived classs constructor When an object of a derived class is destroyed, its destructor is called first, then that of the base class

    33. Inheritance & Polymorphism 33 Constructors & Destructors in Base & Derived Classes

    34. Inheritance & Polymorphism 34

    35. Inheritance & Polymorphism 35 Passing Arguments to Base Class Constructor Suppose both the base and derived class have default constructors which are called automatically What if the base classs constructor takes arguments? What if there is more than one constructor in the base class? Let the derived class constructor pass arguments to the base class constructor

    36. Inheritance & Polymorphism 36 Passing Arguments to Base Class Constructor Allows selection between multiple base class constructors Specify arguments to base constructor on derived constructor heading: Square::Square(int side) : Rectangle(side, side) Can also be done with inline constructors Must be done if base class has no default constructor

    37. Inheritance & Polymorphism 37 Passing Arguments to Base Class Constructor Square::Square(int side):Rectangle(side,side)

    38. Inheritance & Polymorphism 38 Passing Arguments to Base Class Constructor

    39. Inheritance & Polymorphism 39 Passing Arguments to Base Class Constructor

    40. Inheritance & Polymorphism 40 Passing Arguments to Base Class Constructor The derived class : base class constructor notation is used only in the definition of a constructor, not in a prototype If the constructor is defined outside the class, the notation would appear in the function header Class Cube:public Rectangle { }; Cube::Cube(double w,double len,double h): Rectangle(w,len) { }

    41. Inheritance & Polymorphism 41 Redefining Base Class Functions Redefining function: function in a derived class that has the same name and parameter list as a function in the base class Typically used to replace a function in base class with different actions in derived class

    42. Inheritance & Polymorphism 42 Redefining Base Class Functions Not the same as overloading with overloading, parameter lists must be different Objects of base class use base class version of function; objects of derived class use derived class version of function

    43. Inheritance & Polymorphism 43 Base Class

    44. Inheritance & Polymorphism 44

    45. Inheritance & Polymorphism 45

    46. Inheritance & Polymorphism 46 Problem with Redefining Consider this situation: Class BaseClass defines functions x() and y(). x() calls y(). Class DerivedClass inherits from BaseClass and redefines function y(). An object D of class DerivedClass is created and function x() is called. When x() is called, which y() is used, the one defined in BaseClass or the the redefined one in DerivedClass?

    47. Inheritance & Polymorphism 47 Problem with Redefining

    48. Inheritance & Polymorphism 48 Class Hierarchies A base class can be derived from another base class.

    49. Inheritance & Polymorphism 49 Class Hierarchies PassFailActivity inherits score from GradedActivity and has its own member variable minPassingScore PassFailExam inherits score and minPassingScore from PassFailActivity and has its own member variables numQuestions, pointsEach,numMissed PassFailActivity redefines function getLetterGrade so that the the letter grade is either P or F and not A,B,C or D as computed by getLetterGrade in GradedActivity

    50. Inheritance & Polymorphism 50 GradedActivity .h and .cpp #ifndef GRADEDACTIVITY_H #define GRADEDACTIVITY_H // GradedActivity class declaration class GradedActivity { protected: double score; // To hold the numeric score public: // Default constructor GradedActivity() { score = 0.0; } // Constructor GradedActivity(double s) { score = s; } // Mutator function void setScore(double s) { score = s; } // Accessor functions double getScore() const { return score; } char getLetterGrade() const; }; #endif

    51. Inheritance & Polymorphism 51 FinalExam .h and .cpp #ifndef FINALEXAM_H #define FINALEXAM_H #include "GradedActivity.h" class FinalExam : public GradedActivity { private: int numQuestions; // Number of questions double pointsEach; // Points for each question int numMissed; // Number of questions missed public: FinalExam() { numQuestions = 0; pointsEach = 0.0; numMissed = 0; } FinalExam(int questions, int missed) { set(questions, missed); } void set(int, int); // Defined in FinalExam.cpp double getNumQuestions() const { return numQuestions; } double getPointsEach() const { return pointsEach; } int getNumMissed() const { return numMissed; } }; #endif

    52. Inheritance & Polymorphism 52 PassFailActivity .h and .cpp #ifndef PASSFAILACTIVITY_H #define PASSFAILACTIVITY_H #include "GradedActivity.h" class PassFailActivity : public GradedActivity { protected: double minPassingScore; // Minimum passing score. public: // Default constructor PassFailActivity() : GradedActivity() { minPassingScore = 0.0; } // Constructor PassFailActivity(double mps) : GradedActivity() { minPassingScore = mps; } // Mutator void setMinPassingScore(double mps) { minPassingScore = mps; } // Accessors double getMinPassingScore() const { return minPassingScore; } char getLetterGrade() const; }; #endif

    53. Inheritance & Polymorphism 53 PassFailExam .h and .cpp #ifndef PASSFAILEXAM_H #define PASSFAILEXAM_H #include "PassFailActivity.h" class PassFailExam : public PassFailActivity { private: int numQuestions; // Number of questions double pointsEach; // Points for each question int numMissed; // Number of questions missed public: // Default constructor PassFailExam() : PassFailActivity() { numQuestions = 0; pointsEach = 0.0; numMissed = 0; } // Constructor PassFailExam(int questions, int missed, double mps) : PassFailActivity(mps) { set(questions, missed); } // Mutator function void set(int, int); // Defined in PassFailExam.cpp // Accessor functions double getNumQuestions() const { return numQuestions; } double getPointsEach() const { return pointsEach; } int getNumMissed() const { return numMissed; } }; #endif

    54. Inheritance & Polymorphism 54 Polymorphism and Virtual Member Functions Polymorphism allows an object reference variable or an object pointer to reference objects of different types, and to call the correct member functions, depending upon the type of object being referenced

    55. Inheritance & Polymorphism 55 Polymorphism and Virtual Member Functions Virtual member function: function in base class that expects to be redefined in derived class Function defined with key word virtual: virtual void Y() {...} Supports dynamic binding: functions bound at run time to function that they call Without virtual member functions, C++ uses static (compile time) binding

    56. Inheritance & Polymorphism 56

    57. Inheritance & Polymorphism 57

    58. Inheritance & Polymorphism 58

    59. Inheritance & Polymorphism 59 Static Binding Program 15-9 displays 'C' instead of 'P' because the call to the getLetterGrade function is statically bound (at compile time) with the GradedActivity class's version of the function. Thus, the actual letter grade is computed instead of the P/F grade We can remedy this by making the getLetterGrade function virtual.

    60. Inheritance & Polymorphism 60 Virtual Functions A virtual function is dynamically bound to calls at runtime. At runtime, C++ determines the type of object making the call, and binds the function to the appropriate version of the function.

    61. Inheritance & Polymorphism 61 Virtual Functions To make a function virtual, place the virtual key word before the return type in the base class's declaration (or prototype) virtual char getLetterGrade() const; If the function is defined outside the class, you do not place the virtual keyword in the function header The compiler will not bind the function to calls. Instead, the program will bind them at runtime.

    62. Inheritance & Polymorphism 62

    63. Inheritance & Polymorphism 63

    64. Inheritance & Polymorphism 64

    65. Inheritance & Polymorphism 65 Polymorphism Requires References or Pointers Polymorphic behavior is only possible when an object is referenced by a reference variable or a pointer, as demonstrated in the displayGrade function. Polymorphic behavior is not possible when an object is passed by value Static binding will take place when the object is not a reference variable or pointer, for example: void displayGrade (const GradedActivity activity)

    66. Inheritance & Polymorphism 66 Polymorphism Using Pointers #include <iostream> #include <iomanip> #include "PassFailExam.h" using namespace std; void displayGrade(const GradedActivity *); int main() { // Create a GradedActivity object-the score is 88. GradedActivity test1(88.0); // Create a PassFailExam object. There are 100 questions, the student missed 25 of // them and the minimum passing score is 70. PassFailExam test2(100, 25, 70.0); // Display the grade data for both objects. cout << "Test 1:\n"; displayGrade(&test1); // Address of the GradedActivity object cout << "\nTest 2:\n"; displayGrade(&test2); // Address of the PassFailExam object return 0; } void displayGrade(const GradedActivity *activity) { cout << setprecision(1) << fixed; cout << "The activity's numeric score is "<< activity->getScore() << endl; cout << "The activity's letter grade is " << activity->getLetterGrade() << endl; }

    67. Inheritance & Polymorphism 67 Base Class Pointers Can define a pointer to a base class object Can assign it the address of a derived class object This statement dynamically allocates a PassFailExam object and assigns is address to exam, which is a GradedActivity pointer

    68. Inheritance & Polymorphism 68 Base Class Pointers #include <iostream> #include <iomanip> #include "PassFailExam.h" using namespace std; // Function prototype void displayGrade(const GradedActivity *); int main() { // Constant for the size of an array. const int NUM_TESTS = 4; // tests is an array of GradedActivity pointers. // Each element of tests is initialized with the // address of a dynamically allocated object. GradedActivity *tests[NUM_TESTS] = { new GradedActivity(88.0), new PassFailExam(100, 25, 70.0), new GradedActivity(67.0), new PassFailExam(50, 12, 60.0) }; // Display the grade data for each element in the array. for (int count = 0; count < NUM_TESTS; count++) { cout << "Test #" << (count + 1) << ":\n"; displayGrade(tests[count]); cout << endl; } return 0; }

    69. Inheritance & Polymorphism 69 Base Class Pointers Base class pointers and references only know about members of the base class So, you cant use a base class pointer to call a derived class function For example, GradedActivity has 3 member functions besides its constrcutors setScore, getScore and getLetterGrade A GradedActivity pointer can be used to call only those functions, regardless of the type of object it points to GradedActivity *exam = new PassFailExam(100,25,70.0); cout<<exam->getScore()<<endl; // WORKS cout<<exam->getLetterGrade()<<endl; //WORKS cout<<exam->getPointsEach()<<endl; //ERROR

    70. Inheritance & Polymorphism 70 Base Class Pointers The is-a relationship does not work in reverse A final exam is a graded activity is true but a graded activity is a final exam is not true. Not all graded activities are final exams GradedActivity *gaPointer = new GradedActivity(88.0); FinalExam *fePointer = gaPointer; //WILL NOT WORK A final exam has more functionality than a graded activity so you can not assign gaPointer to fePointer You can make the assignment with casting; this means fePointer points to a base class object. FinalExam *fePointer = static_cast<FinalExam *>(gaPointer); fePointer->getPointsEach() will compile but a runtime error will occur. fePointer is a FinalExam pointer that points to a GradedActivity object

    71. Inheritance & Polymorphism 71 Redefining vs. Overriding Redefined functions in derived class will be ignored unless base class declares the function virtual In C++, redefined functions are statically bound and overridden functions are dynamically bound. So, a virtual function is overridden, and a non-virtual function is redefined.

    72. Inheritance & Polymorphism 72 Virtual Destructors It's a good idea to make destructors virtual if the class could ever become a base class. Otherwise, the compiler will perform static binding on the destructor if the class ever is derived from. If the derived class is pointed to by an object of the base class, only the base class destructor will be called Making the base class destructor virtual will enable both destructors to execute When a base class function is declared virtual, all overridden versions of the function in derived classes automatically become virtual Including a virtual destructor in a base class, even one that does nothing, will ensure that any derived class destructors will also be virtual

    73. Inheritance & Polymorphism 73 Abstract Base Classes and Pure Virtual Functions Pure virtual function: a virtual member function that must be overridden in a derived class that has objects The abstract class represents the represents the generic or abstract form of all classes that are derived from it Abstract base class contains at least one pure virtual function: virtual void Y() = 0; The = 0 indicates a pure virtual function Must have no function definition in the base class

    74. Inheritance & Polymorphism 74 Abstract Base Classes and Pure Virtual Functions Abstract base class: class that can have no objects. Serves as a basis for derived classes that may/will have objects A class becomes an abstract base class when one or more of its member functions is a pure virtual function

    75. Inheritance & Polymorphism 75 Abstract Base Classes and Pure Virtual Functions In Student.h we have a pure virtual function virtual int getRemainingHours() const = 0; This is defined in CsStudent.cpp int CsStudent::getRemainingHours() const { int reqHours, // Total required hours remainingHours; // Remaining hours // Calculate the required hours. reqHours = MATH_HOURS + CS_HOURS + GEN_ED_HOURS; // Calculate the remaining hours. remainingHours = reqHours - (mathHours + csHours + genEdHours); // Return the remaining hours. return remainingHours; }

    76. Inheritance & Polymorphism 76 Abstract Base Classes and Pure Virtual Functions There is no meaning to calculating the remaining hours for a general student each major has its own requirements Thus, the pure virtual function in class Student which is given specific definition each derived class

    77. Inheritance & Polymorphism 77 Multiple Inheritance A derived class can have more than one base class Each base class can have its own access specification in derived class's definition: class cube : public square, public rectSolid;

    78. Inheritance & Polymorphism 78 Multiple Inheritance Arguments can be passed to both base classes' constructors: cube::cube(int side) : square(side), rectSolid(side, side, side); DateTime::DateTime(int dy, int mon, int yr, int hr, int mt, int sc) : Date(dy, mon, yr), Time(hr, mt, sc) The order the base class constructor calls appear in the list does not matter. They are called in order of inheritance. Base class constructors are called in order they are listed in the first line of the class declaration

    79. Inheritance & Polymorphism 79 Multiple Inheritance Problem: what if base classes have member variables/functions with the same name? Solutions: Derived class redefines the multiply-defined function Derived class invokes member function in a particular base class using scope resolution operator :: Compiler errors occur if derived class uses base class function without one of these solutions

More Related