Download presentation
Presentation is loading. Please wait.
Published byΦοίβη Κωνσταντόπουλος Modified over 5 years ago
1
COP 3330 Object-oriented Programming in C++
Polymorphism Spring 2019
2
A Motivational Example
At FSU, we may have many sub-categories of Students Undergrads, graduates, online, part-time, …… Thus, with many different types of grade reports for different type of students It would be great to store one aray of students, and print out ALL grade reports with a loop Student list[42000]; for (int j = 0; j < size; j++) list[j].gradeReport();
3
Problem The items in the array are base-class Student objects
Base class objects do not know about subtypes (Grads, Undergrads, or others) Everything in an array needs to be the same type and size The gradeReport() function being called is the Student class version However, we want to call the Grad version for graduate students and Undergrad version for undergraduate students What about creating separate arrays for different student types? Not realistic if there are many subtypes
4
A Base Class Pointer Property
We can get around the first problem through a special property of inheritance Normally, a pointer can only point to one type However, there is a special rule for inheritance A pointer to a base class type can be pointed at an object derived from the base class Similarly, a base class reference variable can refer to an object derived from that base class Examples Student s; Grad g; Undergrad u; Student *sp1, *sp2, *sp3; sp1 = &s; // pointing at Student object sp2 = &g; // pointing at Grad object sp3 = &u; // pointing at Undergrad object
5
Heterogeneous List A single array of pointers to a base class Example
These pointers can point to ANY objects derived from that base Thus, we have created a list of various types of objects Without breaking the array rules! All pointers have the same type—a pointer to the base class All pointers have the same size Example Student *list[42000]; Student s; Grad g; Undergrad u; list[0] = &g; list[1] = &u; list[2] = new Grad; list[3] = &s; …
6
Virtual Functions The heterogenous list solves the first problem. How about the second problem? Which version of gradeReport() will be run? for (j = 0; j < size; j++) list[j]->gradeReport(); This still calls the Student version of gradeReport due to a concept known as binding list[j].gradeReport is bound to the Student version at compile time Compiler cannot guess the version of objects at runtime If we want to run the correct version at runtime, we need to achieve late (dynamic) binding
7
Virtual Functions The keyword virtual will do the trick
To override a base class function when it is called through a pointer, declare the function to be virtual class Student { public: virtual void gradeReport(); }; Now when gradeReport()is called through a base-class pointer, the program will run the appropriate version (late binding)
8
Example Student s; Grad g; Undergrad u; Student *sp1, *sp2, *sp3; sp1 = &s; // pointing at Student object sp2 = &g; // pointing at Grad object sp3 = &u; // pointing at Undergrad object sp1->gradeReport(); // runs Student’s version sp2->gradeReport(); // runs Grad’s version sp3->gradeReport(); // runs Undergrad’s version
9
Example With the heterogeneous list, and the use of virtual functions, we can put all Students in one list, and print all grade reports: for (int i = 0; i < size; i++) list[i]->GradeReport();
10
Pure Virtual Function Suppose you do not want to do anything for the Student’s version of gradeReport() You can omit the definition virtual void gradeReport()=0; A virtual function without a definition is a pure virtual function has no function definition in .cpp file, only declaration in .h file For instance, class Student might not have enough information for the full grade reports -- we need to know what kind of student (i.e. which subtype)
11
Abstract Class Any class that has at least one pure virtual function is an abstract class An abstract class cannot be instantiated Abstract classes are generally used as base classes They are intended to be a place to declare data and functions common to classes derived from them Abstract class can still be used to build pointers, to take advantage of virtual functions Example abstract class Shape Shape s; // illegal Shape *sptr; // legal
12
virtual void printCheck()=0
Code Review: Employee Employee virtual void printCheck()=0 Temporary void printCheck() Permanent void printCheck() = 0 Hourly Salaried
13
Questions
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.