visual c++ programming: concepts and projects
DESCRIPTION
Visual C++ Programming: Concepts and Projects. Chapter 13B: Object-Oriented Programming (Tutorial). Tutorial: Maze Program. Problem analysis Create a program in which a mouse navigates a maze looking for the hidden cheese The maze consists of a two-dimensional array of cells - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/1.jpg)
Visual C++ Programming: Concepts and Projects
Chapter 13B: Object-Oriented Programming
(Tutorial)
![Page 2: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/2.jpg)
Tutorial: Maze Program
• Problem analysis– Create a program in which a mouse navigates a
maze looking for the hidden cheese– The maze consists of a two-dimensional array of
cells– The Mouse is an object created from a Mouse
class definition– Each cell in the maze is an object created from a
Cell class definition
2Programming with Visual C++
![Page 3: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/3.jpg)
Problem Analysis
3Programming with Visual C++
![Page 4: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/4.jpg)
The Mouse Class Definition
• Class variables– Various icons of mouse facing different directions
• private data members– Row and column locations of the mouse in the
maze (row and col)– Icon
• private methods– Default constructor – Mouse()
4Programming with Visual C++
![Page 5: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/5.jpg)
The Mouse Class Definition (continued)
• public methods– Initializing constructor – Mouse(int, int)– Accessor methods
• getRow()• getCol()• getIcon()
– Mutator methods• setRow()• setCol()
5Programming with Visual C++
![Page 6: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/6.jpg)
The Mouse Class Definition (continued)
• public methods– Utility methods
• goRight()• goLeft()• goUp()• goDown()
6Programming with Visual C++
![Page 7: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/7.jpg)
The Mouse Class Definition (continued)
7Programming with Visual C++
![Page 8: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/8.jpg)
The Mouse Class Definition (continued)
8Programming with Visual C++
![Page 9: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/9.jpg)
The Mouse Class Definition (continued)
9Programming with Visual C++
![Page 10: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/10.jpg)
10Programming with Visual C++
![Page 11: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/11.jpg)
The Cell Class Definition
• private data members– row: int – row location of Cell in maze– col: int – column location of Cell in maze– access: bool – indicates whether Mouse can
enter this Cell– hasCheese: bool – indicates whether cheese is
in this Cell• private methods
– Default constructor – Cell()
11Programming with Visual C++
![Page 12: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/12.jpg)
The Cell Class Definition (continued)
• public methods– Initializing constructor – Cell(int, int)– Accessor methods
• getRow()• getCol()• getAccess()• getCheese()
– Mutator methods• setAccess()• setCheese()
12Programming with Visual C++
![Page 13: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/13.jpg)
The Cell Class Definition (continued)
13Programming with Visual C++
![Page 14: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/14.jpg)
14Programming with Visual C++
![Page 15: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/15.jpg)
The Cell Class Definition (continued)
• An initializing constructor is used to assign each Cell a row and column location as well as a Boolean access value
15Programming with Visual C++
![Page 16: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/16.jpg)
Design
• A maze consists of rows and columns– Rows run horizontally– Columns run vertically
• Each cell has a row and column location
16Programming with Visual C++
![Page 17: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/17.jpg)
Design (continued)
17Programming with Visual C++
![Page 18: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/18.jpg)
Design (continued)
18Programming with Visual C++
![Page 19: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/19.jpg)
Design (continued)
19Programming with Visual C++
![Page 20: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/20.jpg)
Design (continued)
• The interface contains two buttons (used to start and stop the animation)
• A Timer control is used to perform Mouse movement
• The Mouse moves from one Cell to another• Previous cells are colored brown• When the mouse reaches the cell with the
cheese, a MessageBox pops up
20Programming with Visual C++
![Page 21: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/21.jpg)
Design (continued)
21Programming with Visual C++
![Page 22: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/22.jpg)
Design (continued)
• The size of Cells and the number of rows and columns of Cells in the maze are constants
22Programming with Visual C++
![Page 23: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/23.jpg)
Design (continued)
23Programming with Visual C++
![Page 24: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/24.jpg)
Design (continued)
• The maze is a two-dimensional array of Cells• Use the Array template as a foundation for
this
24Programming with Visual C++
![Page 25: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/25.jpg)
Design (continued)
• Instance variables include the mouse, maze, and a variable to indicate which direction the mouse is moving
25Programming with Visual C++
![Page 26: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/26.jpg)
Design (continued)
• Creating the maze requires the instantiation of 320 Cell objects (20 in each row)
26Programming with Visual C++
![Page 27: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/27.jpg)
Design (continued)
• The Form1_Load() event will construct the interface, complete with maze, mouse, and cheese
27Programming with Visual C++
![Page 28: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/28.jpg)
Design (continued)
• Drawing the maze (nested loops for rows and columns)
28Programming with Visual C++
![Page 29: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/29.jpg)
Design (continued)
• Drawing and positioning the Mouse
29Programming with Visual C++
![Page 30: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/30.jpg)
Design (continued)
• Keep the mouse in the maze by checking for edges
30Programming with Visual C++
![Page 31: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/31.jpg)
Development
• Interface construction (only two buttons)• Timer control used to control movement• Form1_Load() will draw the maze initially• The maze and mouse are redrawn in each Timer_Tick() event
31Programming with Visual C++
![Page 32: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/32.jpg)
Development (continued)
32Programming with Visual C++
![Page 33: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/33.jpg)
Development (continued)
• The Mouse and Cell class definition header files must both be included at the top of the client (Form1.h)
33Programming with Visual C++
![Page 34: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/34.jpg)
Development (continued)
• Instance variables, constants, and object handles
34Programming with Visual C++
![Page 35: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/35.jpg)
Development (continued)
• Form1_Load() constructs the maze
35Programming with Visual C++
![Page 36: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/36.jpg)
Development (continued)
• The start button creates the mouse and cheese
36Programming with Visual C++
![Page 37: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/37.jpg)
Development (continued)
• The mouse and cheese icons are displayed in Rectangles
37Programming with Visual C++
![Page 38: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/38.jpg)
Development (continued)
• The maze cells are drawn in drawMaze()
38Programming with Visual C++
![Page 39: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/39.jpg)
Development (continued)
• At each interval of the Timer, its Tick() event:– Redraws the maze– Draws the mouse at its new location
• The animation can be halted with btnStop_Click()
39Programming with Visual C++
![Page 40: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/40.jpg)
Development (continued)
• The Timer1_Tick() event:– Creates a Rectangle based on the cell in which the
mouse currently resides (oldRect)
40Programming with Visual C++
![Page 41: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/41.jpg)
Development (continued)• If mouse is not at edge, then move in current
direction
41Programming with Visual C++
![Page 42: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/42.jpg)
Development (continued)
• If mouse moves into cell with cheese, congratulations!• If mouse was at an edge, then choose new direction
42Programming with Visual C++
![Page 43: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/43.jpg)
Development (continued)
• Check for edges by checking row and column
43Programming with Visual C++
![Page 44: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/44.jpg)
Testing
• When btnStart is clicked:– Maze appears (16 rows x 20 columns of cells)– Mouse and cheese appear– Mouse moves across the maze from left to right– When mouse reaches the edge, it turns and
follows the edges around
• Reposition the cheese so that the mouse finds it in its path
44Programming with Visual C++
![Page 45: Visual C++ Programming: Concepts and Projects](https://reader036.vdocuments.us/reader036/viewer/2022062721/568136ec550346895d9e81b6/html5/thumbnails/45.jpg)
On Your Own
• Stronger mutators– Do not allow negative values
• Private instance methods– verifyRow() and verifyCol() for Mouse
class
• New UML class diagram– Add verifyRow() and verifyCol() to
Mouse UML diagram
45Programming with Visual C++