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 106X Inheritance Topics Inheritance The basics › Example: Stanford GObject class Polymorphism › Example: Expression trees (final project) 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); Why would you do this? › Handy if you want to have a function that works on any Employee, but takes advantage of custom behavior by specific employee type: void doMonthlyPaycheck(Employee *employee) { cout << "You are now $" << employee->salary()/12 << " wealthier!" << endl; } 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! 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? Siamese * s = new Siamese; 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 Siamese; m->scratchCouch(); (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 Expres
Stanford University
4 class CompoundExp : public Expression {
public:
b [Re we ¥f
CompoundExp(const std::string& op, const Expression *lhs, const Expression *rhs);
/* Prototypes for the virtual methods overridden by this class */
virtual ~CompoundExp();
irtual d eval (EvaluationContext& cortext) const;
virtual std::string toStrimgt) const;
virtual ExpressionType getType() const;
/* Prototypes of methods specific to this class */
std::string getOperator() const;
const Expression *getLHS() const;
const Expression *getRHS() const;
private:
std::string op; /* The operator string (+, -, *, /) */
const/fExpression *lhs Jrhs;: /* The left and right subexpression */
Stanford University
Expression (base class) Note: you cannot actually create an Expression object These methods are never implemented (note the “= 0”) “pure virtual” Expression exists solely to provide a base class to others “abstract” Another Derived cla eExp
class DoubleExp : public Expression
public:
DoubleExp(double value);
/* Prototypes for the virtual methods overridden by this class */
double eval(EvaluationContext& context) const;
std::string toString() const;
ExpressionType getfype() const;
/* Prototypes of methods specific to this class */
double getDoubleValue() const;
private:
double value; /* The value of the constant */
Be
Stanford University