Week 9 discussion - Virtual Functions and Abstract Base...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Virtual Functions and Abstract Base Classes Week 9
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Administrative Add/Drop with a “W” Deadline This Friday (11/12) Project 4 is available Due November 24
Background image of page 2
A new class: Rectangle class Rectangle { private: int length; int width; string color; public: Rectangle(int l, int w, string c); int getArea(); int getPerimeter(); void setColor(string color); } class Square: public Rectangle { public: Square(int l, string c); int getArea(); int getPerimeter(); void setColor(string color); } //Implementation of getArea() multiplies length * length
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Square s; Rectangle *r = &s; Rectangle is a superclass of Square, so a pointer to a Rectangle can also point to a Square If we call r->getArea(), will it call Rectangle's implementation or Square's?
Background image of page 4
Answer: Since C++ can't tell statically that r is actually a pointer to a Square, it uses the apparent type, so the implementation for Rectangle will be called. Next Question: How can we get C++ to determine
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

Week 9 discussion - Virtual Functions and Abstract Base...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online