pctechguide.com

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

Object Oriented Programming – Examples

Earlier we developed a class inheritance tree for some game characters in a maze game, but we need to design another class, or class hierarchy, for the maze. Since we want the mazes to have different levels of difficulty, each level of maze will require slightly different functions.

We can say that each maze, of any level, will be a grid, with each square of the grid being defined as the size of a single game character move. The grid will be stored in a two dimensional array, in which each grid square is represented with a 0 for no wall, or a 1 for wall. There will be one exit defined in the grid with a -1. The maze itself will be randomly created with a function, as will the start location for both the monster and the player character.

The

At first glance it appears that our derived complexMaze class has only one function, but it has actually inherited all the member functions and data of the base class maze. In that light, you can see that in fact it’s single member function is actually a repeat of the base class’s function, createMaze(). The new createMaze() function will use a completely different algorithm in order to create a more difficult maze. When a derived class redefines a base class’s function in this way it is called overriding, and it is the basic essence of polymorphism.

Polymorphism can cause headaches if programmers are not aware of the version of the function they are using. In this example, if an object is created of type maze and the function createMaze() is called, it will use the version of createMaze defined in the base maze class. If however the function createMaze() is called by an object of type complexMaze, then the version of the function called is the one defined in the complexMaze class. In a very deep inheritance tree it is at least possible that a function may be overridden many times, and a programmer has to be very careful to know exactly which function is going to be called.

It’s also important to note that this is by no means the only solution to the problem. For instance, parameters passed to the maze function createMaze() could just as easily govern the complexity of the maze created. However, there may be other compelling reasons for developing a derived complex maze class, reasons that the project designer must weigh up when creating the class hierarchy. There will often be several different solutions available, and sometimes it may be worth using inheritance and polymorphism, whilst at other times using conditional programming branches might be better. The ability to make these often crucial decisions well and with confidence, like most things, comes with practice and experience.

  • 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

How To Remove Remove Internet Antivirus 2017

Internet Antivirus 2017 is a false security client and a clone of several other rogues. Like all fake security clients you are going to want to remove Internet Antivirus 2017 as soon as you realize you are infected. This program is known to highjack a users browser and direct them to a … [Read More...]

Browser Security Removal

A program called Browser Security has to be a good idea, right? Its advertised to protect you online afterall. Well, the truth is that this is one program that you want to stay away from and remove as soon as possible. This program is distributed through some rather dubious methods, and it is … [Read More...]

Best Partition Data Recovery Approaches for Windows 10 Systems

There are a number of benefits of partitioning hard drives. Partitioning is an effective way to organize your files and folders into different segments of your hard drive, which means they will be much easier to find. This is something that you can easily do with the Windows 10 operating system. You … [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

Use WinDirStat to Clean Up Hard Drive

Today's hard drives are large and can store tons of files, pictures, videos, etc. But, even with all this space there still may be a time when you … [Read More...]

Digital Video for Windows

AVI (Audio Video Interleaved) is Microsoft's generic format for digital video in Windows, provided via its MCI (Media … [Read More...]

UI Regression Testing as a Tool to Build a User-Friendly Product

Developing a new product, whether it is an application or an online website, is a quite challenging task, which requires a well-planned and structured … [Read More...]

[footer_backtotop]

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