csci 104
play

CSCI 104 Polymorphism Mark Redekopp David Kempe 2 Virtual - PowerPoint PPT Presentation

1 CSCI 104 Polymorphism Mark Redekopp David Kempe 2 Virtual functions, Abstract classes, and Interfaces POLYMORPHISM 3 Assignment of Base/Declared class Person { Can we assign a derived object into a base public: object? void


  1. 1 CSCI 104 Polymorphism Mark Redekopp David Kempe

  2. 2 Virtual functions, Abstract classes, and Interfaces POLYMORPHISM

  3. 3 Assignment of Base/Declared • class Person { Can we assign a derived object into a base public: object? void print_info(); // print name, ID string name; int id; • Can we assign a base object into a derived? }; • Think hierarchy & animal classification? class Student : public Person { public: – Can any dog be assigned as a mammal void print_info(); // print major too – int major; double gpa; Can any mammal be assigned as a dog }; • We can only assign a derived into a base int main(){ (since the derived has EVERYTHING the Person p("Bill",1); Student s("Joe",2,5); base does) // Which assignment is plausible – p = s; // Base = Derived…GOOD p = s; // or s = p; – s = p; // Derived = Base…BAD } Class Person Class Student string name_ string name_ int id_ int id_ int major_ double gpa_

  4. 4 Inheritance • A pointer or reference to a derived class class Person { public: object is type-compatible with (can be void print_info(); // print name, ID string name; int id; assigned to) a base-class type }; pointer/reference class Student : public Person { – public: Person pointer or reference can also point to Student void print_info(); // print major too or Faculty object (i.e. a Student is a person) int major; double gpa; – All methods known to Person are supported by a }; Student object because it was derived from Person class Faculty : public Person { – Will apply the function corresponding to the type of public: the pointer void print_info(); // print tenured bool tenure; • For second and third call to print_info() we }; would like to have Student::print_info() and int main(){ Faculty::print_info() executed since the actual Person *p = new Person("Bill",1); Student *s = new Student("Joe",2,5); object pointed to is a Student/Faculty Faculty *f = new Faculty("Ken",3,0); Person *q; • BUT…it will call Person:: print_info() q = p; q->print_info(); • This is called 'static binding' q = s; q->print_info(); q = f; q->print_info(); – Which version is called is based on the static type of } the pointer being used Name=Bill, ID=1 Name=Joe, ID=2 Name=Ken, ID=3

  5. 5 Virtual Functions & Dynamic Binding class Person { public: • Member functions can be virtual void print_info(); string name; int id; declared 'virtual' }; class Student : public Person { • 'Virtual' declaration allows public: void print_info(); // print major too derived classes to redefine int major; double gpa; }; the function and which class Faculty : public Person { public: version is called is determined void print_info(); // print tenured bool tenure; by the type of object pointed }; to/referenced rather than the int main(){ Person *p = new Person("Bill",1); type of pointer/reference Student *s = new Student("Joe",2,5); Faculty *f = new Faculty("Ken",3,0); – This is known as dynamic Person *q; q = p; q->print_info(); binding q = s; q->print_info(); q = f; q->print_info(); // calls print_info // for objected pointed to, not type of q } Name=Bill, ID=1 Name=Joe, ID=2, Major = 5

  6. 6 Polymorphism • Idea of polymorphism says int main() that one set of code should { Person* p[5]; operate appropriately (call p[0] = new Person("Bill",1); p[1] = new Student("Joe",2,5); appropriate functions of p[2] = new Faculty("Ken",3,0); p[3] = new Student("Mary",4,2); p[4] = new Faculty("Jen",5,1); derived classes) on all derived for(int i=0; i < 5; i++){ p[i]->print_info(); types of objects // should print most specific info // based on type of object } } Name=Bill, ID=1 Name=Joe, ID=2, Major = 5 Name = Ken, ID=3, Tenured=0 Name = Mary, ID=4, Major=2 Name = Jen, ID=5, Tenured=1

  7. 7 Summary • No virtual declaration: – Member function that is called is based on the _______________ – Static binding • With virtual declaration: – Member function that is called is based on the _______________ – Dynamic Binding

  8. 8 Summary • No virtual declaration: – Member function that is called is based on the type of the pointer/reference – Static binding • With virtual declaration: – Member function that is called is based on the type of the object pointed at (referenced) – Dynamic Binding

  9. 9 Virtual Destructors class Student{ class Student{ ~Student() { } virtual ~Student() { } string major(); string major(); ... ... } } class StudentWithGrades : public Student class StudentWithGrades : public Student { { public: public: StudentWithGrades(...) StudentWithGrades(...) { grades = new int[10]; } { grades = new int[10]; } ~StudentWithGrades { delete [] grades; } ~StudentWithGrades { delete [] grades; } int *grades; int *grades; } } int main() int main() { { Student *s = new StudentWithGrades(...); Student *s = new StudentWithGrades(...); cout << s->major(); cout << s->major(); delete s; // What destructor gets called? delete s; // What destructor gets called? return 0; return 0; } } ~Student() gets called and doesn’t delete ~StudentWithGrades() gets called and does grades array delete grades array • Classes that will be used as a base class should have a virtual destructor ( http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 )

  10. 10 Abstract Classes • In software development we may want class CollegeStudent { to create a base class that serves only public: as a requirement/interface that string get_name(); virtual void take_test(); derived classes must virtual string play_sports(); protected: implement/adhere to string name; }; • College students take tests and play sports so it makes sense to ensure that Valid class. Objects of type is defined for any type of CollegeStudent can be declared. CollegeStudent class CollegeStudent { – public: But depending on which college you go to string get_name(); you may do these activities differently virtual void take_test() = 0; – virtual string play_sports() = 0; But…until we know the university we don’t protected: know how to write take_test() and string name; play_sports ()…these are abstract }; • Make this an abstract base class (i.e. Abstract Base Class…No object of interface for future derived classes) type CollegeStudent will be allowed. It only serves as an interface that derived classes will have to implement.

  11. 11 Abstract Classes • An abstract class is one that class CollegeStudent { public: defined pure virtual functions string get_name() { return name; } virtual void take_test() = 0; – Prototype only virtual string play_sports() = 0; – Make function body protected: string name; " = 0; " }; – Functions that are not class TrojanStudent : public CollegeStudent { public: implemented by the base class void take_test() { cout << "Got an A."; } but must be implemented by the string play_sports(){return string("WIN!");} }; derived class class BruinStudent : public CollegeStudent { • No objects of the abstract public: void take_test() { cout << "Uh..uh..C-."; } type are allowed to be string play_sports(){return string("LOSE");} }; instantiated int main() { vector<CollegeStudent *> mylist; mylist.push_back(new TrojanStudent()); mylist.push_back(new BruinStudent()); for(int i=0; i < 2; i++){ mylist[i]->take_test(); cout << mylist[i]->play_sports() << endl; } Output: return 0; Got an A. WIN! } Uh..uh..C-. LOSE

  12. 12 When to Use Inheritance • Main use of inheritance is to #include "student.h" void sports_simulator(CollegeStudent *stu){ setup interfaces (abstract ... stu->play_sports(); classes) that allow for new, }; derived classes to be written in g++ -c sportsim.cpp the future that provide outputs sportsim.o (10 years ago) additional functionality but still #include "student.h" works seamlessly with original class MITStudent : public CollegeStudent { public: code void take_test() { cout << "Got an A+."; } string play_sports() { return string("What are sports?!?"); } }; int main() { vector<CollegeStudent *> mylist; mylist.push_back(new TrojanStudent()); mylist.push_back(new MITStudent()); for(int i=0; i < 2; i++){ sports_simulator(mylist[i]); } return 0; } g++ main.cpp sportsim.o program will run fine today with new MITStudent

  13. 13 Abstract Classes • No objects of the abstract class Animal { public: type are allowed to be Animal(string c) : color(c) { } virtual ~Animal() instantiated string get_color() { return c; } virtual void make_sound() = 0; • But the abstract base class protected: string color; }; can define common class Dog : public Animal { public: functions, have data void make_sound() { cout << "Bark"; } }; members, etc. that all class Cat : public Animal { public: derived classes can use via void make_sound() { cout << "Meow"; } }; inheritance class Fox : public Animal { public: – Ex. 'color' of the Animal void make_sound() { cout << "???"; } }; int main(){ Animal* a[3]; a[0] = new Animal; // WON'T COMPILE...abstract class a[1] = new Dog("brown"); a[2] = new Cat("calico"); cout << a[1]->get_color() << endl; Output: cout << a[2]->make_sound() << endl; brown } meow

Recommend


More recommend