pctechguide.com

  • Home
  • Guides
  • Tutorials
  • Articles
  • Reviews
  • Glossary
  • Contact

Object Oriented Programming 5

To illustrate putting together an object oriented (OO) class hierarchy design, let’s take a look at a simple maze game. We’ll be creating an OO design, but we’re not going to select a programming language at this stage. We’ll see how we can analyse the problem from an OO perspective, and produce a workable OO design solution that can be translated into implementation code.

Our simple game involves a two dimensional maze and two moving objects. The moving objects are the player’s character and a monster that chases the player around while they try to solve the maze. For our OO design, the key here is that, although these two objects have clear differences, initially we’re only interested in what they have in common.

During the game the player’s character and the monster both have to have their screen position stored, and this is best held as a two dimensional coordinate x and y. The point class is a very common class for storing coordinates, and one we’ll use in our design. We also want to know whether or not the characters are alive in the game. To cover these similarities, we can create a class that covers both the game characters, as follows.

We’re using Unified Modelling Language (UML) notation in our class modelling. UML is commonly used in object oriented design, and ArgoUML is a free Java tool you can use to produce your own UML diagrams.

The

Notice that gameCharacter has not been derived from the class point, but has included a data member currentLocation which is of type point. Now we’ll get on to inheritance as we start to think about the differences between the game characters.

Remember that a class doesn’t just have data members, but also methods, and the methods for the game objects concerning movement are very different. The player character is going to be moved in response to player commands, while the monster will need an Artificial Intelligence (AI) game function to make it move in pursuit of the player.

There are also different data requirements. For the player, we want to keep a score, and what level of maze they’ve reached. For the monster, we at least want to have a speed variable, so that we can have it move faster or slower.

A

Note that there are a number of functions that have been ommitted from the class designs, particularly how to get and set the values of the various class data members, assuming they are private class members, but the basics are there. With thought, this class tree can be extended in all sorts of ways. Various game characters might be derived from the gameCharacter class, or from the monster class, whichever makes most sense. You can see that using inheritance each derived class provides greater and greater specialisation, but that the base class provides a foundation of reusable code to build upon.

Next we’ll take a look at times when a class may be designed that has more than one bass class, known as multiple inheritance.

  • Object Oriented Programming 2
  • Object Oriented Programming 3
  • Object Oriented Programming 4
  • Object Oriented Programming 5
  • Object Oriented Programming 6
  • Object Oriented Programming 7
  • Object Oriented Programming 8

Filed Under: Object Oriented Programming

Latest Articles

Fatal Exception OE Errors

Anybody who owns a PC has probably seen enough error screens to last a lifetime. Out of all of these possible error messages, one of the most annoying continues to be the fatal exception OE error screen. The reason that fatal exception errors are so annoying is because there are so many … [Read More...]

Win98 Installation

After you've partitioned and formatted your hard disk, you're ready to reinstall Windows 98: Insert the Windows 98 Startup disk in the floppy disk drive, and then restart your computer. When the Windows 98 Startup menu is displayed, choose the Start computer with CD-ROM support option, and … [Read More...]

5 More Awesome Google Assistant Features

We previously wrote a post on some of the best features of Google Assistant. We wanted to cover some of the more advanced features today. 1.. One of the strong points of intelligent loudspeakers such as Google Home is at the neuralgic center of home automation. Although Google claims that it is … [Read More...]

20 Cool Creative Commons Photographs About the Future of AI

AI technology is starting to have a huge impact on our lives. The market value for AI is estimated to have been worth $279.22 billion in 2024 and it … [Read More...]

13 Impressive Stats on the Future of AI

AI technology is starting to become much more important in our everyday lives. Many businesses are using it as well. While he has created a lot of … [Read More...]

Graphic Designers on Reddit Share their Views of AI

There are clearly a lot of positive things about AI. However, it is not a good thing for everyone. One of the things that many people are worried … [Read More...]

Redditors Talk About the Impact of AI on Freelance Writers

AI technology has had a huge impact on our lives. A 2023 survey by Pew Research found that 56% of people use AI at least once a day or once a week. … [Read More...]

11 Most Popular Books on Perl Programming

Perl is not the most popular programming language. It has only one million users, compared to 12 million that use Python. However, it has a lot of … [Read More...]

10 Exceptional Books on ChatGPT that Will Blow Your Mind

ChatGPT is a powerful new AI tool that is taking the world by storm. You are going to find a lot of amazing books that will teach you how to make the … [Read More...]

Guides

  • Computer Communications
  • Mobile Computing
  • PC Components
  • PC Data Storage
  • PC Input-Output
  • PC Multimedia
  • Processors (CPUs)

Recent Posts

Migrating from Windows Server 2003 – What to Do First

Support for Microsoft Windows Server for 2003 is ending in the next few months. It is time for organizations to start considering the migration to … [Read More...]

LPX – Low Profile eXtension motherboard form factor

The LPX format is a specialised variant of the Baby-AT used in low profile desktop systems and is a loose specification with a variety of … [Read More...]

Office 2016 Is All About Teamwork

Microsoft Office 2016 is now available and it offers a range of new features you will find useful. It has tweaked some of the features to make them … [Read More...]

[footer_backtotop]

Copyright © 2025 About | Privacy | Contact Information | Wrtie For Us | Disclaimer | Copyright License | Authors