

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This document from Stanford University's CS106X course covers inheritance and polymorphism, including basics, templates, run-time polymorphism, and pure virtual functions. It includes examples using the Employee class and its subclasses, Lawyer and Programmer. The document also discusses the Expression class and its derived classes, DoubleExp and IdentifierExp.
Typology: Study notes
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















Inheritance Topics
Polymorphism polymorphism : Ability for the same code to be used with different types of objects and behave differently with each. Templates provide a kind of compile-time polymorphism. › Grid
Polymorphism
5
Polymorphism A pointer of type T can point to any subclass of T. **Employee *** neha = new Programmer ("Neha", 2); **Employee *** diane = new Lawyer ("Diane", "Stanford", 5); **Programmer *** cynthia = new Programmer ("Cynthia", 10); When a member function is called on diane, it behaves as a Lawyer. › diane->salary(); › (This is because all the employee functions are declared virtual.) You can not call any Lawyer-only members on diane (e.g. sue). › diane->sue(); // will NOT compile! You can call any Programmer-only members on cynthia (e.g. code). › cynthia->code("Java"); // ok!
Polymorphism examples
Employee diane = new Lawyer("Diane", "Stanford", 5 ); diane->vacationDays(); // ok diane->sue("Cynthia"); // compiler error ((Lawyer) diane)->sue("Cynthia"); // ok
Employee carlos = new Programmer("Carlos", 3); carlos->code(); // compiler error ( (Programmer) carlos)->code("C++"); // ok ( (Lawyer)* carlos)->sue("Cynthia"); // No!!! Compiles but crash!!
Rules for “virtual”: pure virtual
class Mammal { public: virtual void makeSound() = 0; string toString() { return “Mammal”; } }; class Cat : public Mammal { public: virtual void makeSound() { cout << “rawr” << endl; } string toString() { return “Cat”; } }; class Siamese : public Cat { public: virtual void makeSound() { cout << “meow” << endl; } string toString() { return “Siamese”; } virtual void scratchCouch() { cout << “scraaaatch” << endl; } }; What is printed? Siamese * s = new Mammal; cout << s->toString();
class Mammal { public: virtual void makeSound() = 0; string toString() { return “Mammal”; } }; class Cat : public Mammal { public: virtual void makeSound() { cout << “rawr” << endl; } string toString() { return “Cat”; } }; class Siamese : public Cat { public: virtual void makeSound() { cout << “meow” << endl; } string toString() { return “Siamese”; } virtual void scratchCouch() { cout << “scraaaatch” << endl; } }; What is printed? Mammal * m = new Mammal; cout << m->toString();
class Mammal { public: virtual void makeSound() = 0; string toString() { return “Mammal”; } }; class Cat : public Mammal { public: virtual void makeSound() { cout << “rawr” << endl; } string toString() { return “Cat”; } }; class Siamese : public Cat { public: virtual void makeSound() { cout << “meow” << endl; } string toString() { return “Siamese”; } virtual void scratchCouch() { cout << “scraaaatch” << endl; } }; What is printed? Mammal * m = new Siamese; cout << m->toString();
class Mammal { public: virtual void makeSound() = 0; string toString() { return “Mammal”; } }; class Cat : public Mammal { public: virtual void makeSound() { cout << “rawr” << endl; } string toString() { return “Cat”; } }; class Siamese : public Cat { public: virtual void makeSound() { cout << “meow” << endl; } string toString() { return “Siamese”; } virtual void scratchCouch() { cout << “scraaaatch” << endl; } }; What is printed? Cat * c = new Siamese; c->makeSound();
Stanford 1- 2 - 3 Walkthrough
Evaluation of CompoundExp
C++ and the “virtual” keyword