1 / 16

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 .”. 6.2 Classes.

tuan
Download Presentation

6.2 Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. “ 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.” 6.2 Classes

  2. 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. Procedural Programming

  3. 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. Result

  4. 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. Object-Oriented Design

  5. 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? OOD - Example

  6. 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 OOD – Another Example

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

  8. 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. Object-Oriented Design

  9. 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. OOD Example – Battleship Game

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

  11. 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. Ships and Other Objects

  12. 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. Board Object

  13. 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. Object Interaction

  14. 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. Ship ShootAt

  15. 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. Pseudo-code

  16. 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)‏ Main Program

More Related