CS246 Lecture Notes - Lecture 20: Idiom

75 views3 pages

Document Summary

We want subclasses to be able to place/provide some behaviour while other remain the same. Thus, all turtle classes must have heads and feet drawn similiarly. It"s a good idea to make the methods drawhead() drawfeet() drawshell() private because you can"t draw parts of turtle. You can only call draw() to draw the entire turtle. 9 class turtle public: void draw() // this is not virtual. // template to draw a turtle drawhead(); drawshell(); drawfeet(); 22 virtual void drawshell() = 0; // the only difference between turtles is the color you draw the shell. 7 class redturtle : public turtle voic drawshell() override. This example uses the nvi idom- non-virtual interface. Consider a public virtual methods. public: part of the interface, comes with pre/post conditions. virtual: invitation to subclasses to modify behaviour. Final question what must you do to use nvi idiom? public methods must be non-virtual all virtual methods are private (except for the destructor)

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents