Inheritance and Polymorphism in Programming: Stanford CS106X Course, Study notes of Programming Abstractions

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

2021/2022

Uploaded on 09/27/2022

alenapool
alenapool 🇬🇧

4.6

(13)

223 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming Abstractions
Cynthia Lee
CS106X
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Inheritance and Polymorphism in Programming: Stanford CS106X Course and more Study notes Programming Abstractions in PDF only on Docsity!

Programming Abstractions

Cynthia Lee

C S 1 0 6 X

Inheritance Topics

Inheritance

 The basics

› Example: Stanford GObject class

 Polymorphism

› Example: Expression trees (final project)

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 or Grid will output different things for myGrid[0][0], but we can predict at compile time which it will do  Inheritance provides run-time polymorphism. › someEmployee.salary() will behave differently at runtime depending on what type of employee—may not be able to predict at compile time which it is

Polymorphism

We will keep working with the Employee class:

5

 Employees have a name, years worked, salary, vacation, …

 Lawyers know how to sue and get paid 2x as much

 Programmers know how to write code and get bigger raises each year

 (Code is now on lectures page of website.)

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

You can use the object's extra functionality by casting.

Employee diane = new Lawyer("Diane", "Stanford", 5 ); diane->vacationDays(); // ok diane->sue("Cynthia"); // compiler error ((Lawyer) diane)->sue("Cynthia"); // ok

Pro Tip: you should not cast a pointer into something that it is not!

  • It will compile, but the code will crash (or behave unpredictably)

when you try to run it.

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

If a method of a class looks like this:

 virtual returntype method() = 0 ;

 then this method is a called “ pure virtual ” function

 and the class is called an “ abstract class ”

 Abstract classes are like Java interfaces

 You cannot do “= new Foo();” if Foo is abstract (just like Java

interfaces)

 ALSO, you cannot do “= new DerivedFoo();” if DerivedFoo extends

Foo and DerivedFoo does not implement all the pure virtual

methods of Foo

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();

(A)“Mammal”

(B)“Cat”

(C)“Siamese”

(D) Gives an error (identify compiler or crash)

(E) Other/none/more

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();

(A)“Mammal”

(B)“Cat”

(C)“Siamese”

(D) Gives an error (identify compiler or crash)

(E) Other/none/more

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();

(A)“Mammal”

(B)“Cat”

(C)“Siamese”

(D) Gives an error (identify compiler or crash)

(E) Other/none/more

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();

(A)“rawr”

(B)“meow”

(C)“Siamese”

(D) Gives an error (identify compiler or crash)

(E) Other/none/more

Stanford 1- 2 - 3 Walkthrough

The Expression class

Evaluation of CompoundExp

This is in the implementation of CompoundExp —let’s take a look at the .h file

to see what op , lhs , and rhs are

C++ and the “virtual” keyword