virtfuncnotes - EECS 280 Week 10 Discussion Inheritance...

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

View Full Document Right Arrow Icon
EECS 280 Week 10 Discussion Inheritance Applied: Design a Drawing Program 1 Goals After this discussion, you will: Be able to identify which functions are called during virtual and non-virtual function dispatch. Have seen a real example of interface-implementation inheritance. Be able to implement an interface given the interface and an example implementation. 2 Introduction Inheritance is a tricky business in C++: it can express several different but related relationships. Last week’s tax return example used inheritance in a way substantially different from the way we ask you to use it in project 4. In fact, the project 4 model is simpler than the one we illustrated last week! With that in mind, this time we’ll be looking at the simplest case of that model (sometimes known as “interface/implementation” inheritance) along with a couple twists. 3 Drawing Let’s consider a simple drawing program. It will let us create several kinds of shapes and move them around on the screen, like the drawing tools in popular office software. How would this sort of program be designed? Typically, a library (i.e., someone else’s code) handles all the messy details of the graphics and dealing with the mouse, and you’re left filling in something like this: void onMouseClick ( int x , int y) { //EFFECTS: Do whatever your program does when the mouse is clicked // at position (x , y ). } (The library will take a function pointer to onMouseClick and will call it whenever the mouse is clicked.) You can imagine similar functions for dragging and dropping the mouse, right-clicking, and so forth. The graphical code will be the “driver” of the program, and you just have to write code that handles “events” like mouse clicks and movements. We’ll even ignore the issue of how to actually draw these things on the screen because it would be tedious and outside the scope of this class. All that’s left for us is to handle the actual operations on shapes – creating, moving, resizing, and so forth. We’ve just expressed a particular kind of thing our program has to deal with – a shape! The fundamental principle in object-oriented programming is that we can create classes to model the kinds of things that our program deals with. Here’s a first pass at a Shape class: 1
Background image of page 1

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

View Full Document Right Arrow Icon
class Shape { int xPos ; int yPos ; int length ; int width ; public : Shape ( int xx , int yy , int len , int wid ) : xPos(xx ) , yPos(yy ) , length ( len ) , width ( wid ) {} void setPosition ( int xx , int yy) { xPos = xx ; yPos = yy ; draw ( ) ; } void setLength ( int len ) { length = len ; draw ( ) ; } void setWidth ( int wid ) { width = wid ; draw ( ) ; } virtual void draw () = 0; //EFFECTS: display t h i s shape on the screen . protected
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

virtfuncnotes - EECS 280 Week 10 Discussion Inheritance...

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

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