Presentation is loading. Please wait.

Presentation is loading. Please wait.

6.2 Classes “ A class is basically a structure with member functions as well as member data. Classes are central to the programming methodology known as.

Similar presentations


Presentation on theme: "6.2 Classes “ A class is basically a structure with member functions as well as member data. Classes are central to the programming methodology known as."— Presentation transcript:

1 6.2 Classes “ A class is basically a structure with member functions as well as member data. Classes are central to the programming methodology known as object-oriented programming.”

2 Procedural Programming In procedural programming, we break down a program in terms of functions. Top-down design principles start with the problem at hand and finds a solution by breaking down the problem into steps that can be used to solve it. We begin with the major steps, and then break those down into smaller steps. This process is called stepwise-refinement.

3 Result The result is a program in which functions play the major role. Data is merely passed to and from functions. Data may be structured (arrays, structs), but is secondary to the flow of control of the program. This is the procedural programming paradigm.

4 Object-Oriented Design In object-oriented design, instead of thinking in terms of functions, we think in terms of objects. Objects are collections of related data along with the functions that apply to that data. Objects occur naturally in many problems, and so by thinking about what objects are needed to solve a problem, we often can create better solutions.

5 OOD - Example Let's take a simple example: A clock. What is the data associated with a clock? What functions do we normally perform on that data?

6 OOD – Another Example A second example is a bank account. Let's take a simple form of a bank account – a checking account with interest. What is the data? balance, APR What are the functions? deposit, withdrawal, check balance, modify APR, check APR

7 Implementation In object-oriented programming, we call the functions that are part of an object its methods. The data is stored in fields or components.

8 Object-Oriented Design In object-oriented design the problem is decomposed into different objects that are inherent in the problem. The program relies on the interaction between the objects. The objects can call each others' methods. This is called message passing. A message is the name (and parameters) of the method to be called. Different types of objects may have methods with the same name, so which method is actually run depends on the object and the message.

9 OOD Example – Battleship Game Consider the game Battleship. An example can be found at http://www.superkids.com/aweb/tools/logic/bship This is a two player game, where each player has a board full of ships, unseen by the other player. The players take turns shooting at squares on their opponents board. The opponent indicates when a ship is hit and when a ship is sunk (all spots are hit). The game is over when one player sinks all of the opponent's ships.

10 Battleship Implementation What objects are necessary? Ship Water Hit Miss Board Player?

11 Ships and Other Objects Ship objects would have the following fields: name, position?, size, number of hits, symbol (for non-GUI version)‏ Ship objects would have the following methods: shootAt, place?, anything else? Shooting a ship would increment the number of hits, and if the size is reached, sink the ship. The other classes – Water, Hit, and Miss, would also have a shootAt method.

12 Board Object The board would have a two dimensional array of Things (where a Thing is a Ship, Water, Hit, or Miss). The board would also have to remember the number of ships sunk. The board would have the following methods: shootAt(x,y), display, placeShips.

13 Object Interaction The main program would prompt the user for a shot, say (x,y), then call the board's shootAt(x,y) method (send the shootAt(x,y) message to the board). The board object would, in turn, look up what object is at (x,y) (a Ship, Water, Hit, or Miss) and send a shootAt message to that object. The board does not care what object is there. It sends the same shootAt message to the object, and depending on the object, the appropriate method is called.

14 Ship ShootAt A ship object would react to the shootAt method by incrementing its number of hits (numHits) field, and if numHits == size, send a message to the board to increment its number of ships sunk (incrShipsSunk). The object would also return an object that is to replace it on the board, in this case, a Hit object, which looks different, and avoids the problem of counting a second hit if the player shoots at the same spot more than once.

15 Pseudo-code The Ship's shootAt method would look like: if(++numHits == size)‏ send the incrShipsSunk message to the board, return Hit; The Board's shootAt(x,y) method would look like: send the shootAt message to what's at (x,y), and set that position to be whatever is returned. Water's shootAt method would just return a Miss. Hit and Miss would just return a Hit and Miss object, respectively.

16 Main Program The main program would first create the board and place the ships, and then have a loop which: Reads in the player's move – (x,y)‏ Sends the shoot(x,y) message to the board Displays the result Continues until the board says that all the ships are sunk (this would mean asking the board if all the ships are sunk)‏


Download ppt "6.2 Classes “ A class is basically a structure with member functions as well as member data. Classes are central to the programming methodology known as."

Similar presentations


Ads by Google