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

OpenGL technology

Initiated in 1992 by Silicon Graphics as a general CAD and 3D API for Unix-based X-terminals, OpenGL evolved out of SGI's proprietary graphics library, IrisGL. Originally, use of OpenGL was restricted to business applications, such as … [Read More...]

Belarc Advisor – Help Keep Your System Secure and Up To Date

The Belarc Advisor tool is a useful tool for keeping your system secure and up to date. It also can tell you a good deal of information about system in a pinch. This tool creates a quick and handy HTML page that gives you a ton of information including system keys you may need to reinstall software … [Read More...]

Creative Commons Images for Your Web Projects

Do you know which images from the Internet you can reuse and which you can't? Do you know Creative Commons? There is a misconception that all the images you find on the net, just because they are on the Internet, are free and can be used as you want and when you want. But it is not so because not … [Read More...]

Revolutionize Your Internet Experience with Orbi 960 – The Ultimate WiFi System

In a world where seamless connectivity is essential, slow and unreliable internet connections are a major problem. Whether you are running a business, … [Read More...]

Do You Need a VPN When Trading Cryptocurrency?

There’s no doubt that the biggest global industries in 2023 are tech-driven, while there remains a significant crossover between many of these … [Read More...]

Goodbye Bitcoin: the 3 alternative cryptocurrencies that have great upside potential, according to experts

Bitcoin has been a very lucrative investment for people that got into it early. One report from The Motley Fool pointed out that $10 of bitcoin … [Read More...]

Self-driving cars face their Achilles’ heel and may be targets of hackers

The market for self-driving cars is booming. Customers spent $22.22 billion on these autonomous vehicles in 2021 and they will likely spend more in … [Read More...]

How to avoid scams with bitcoin and other cryptocurrencies

Cryptocurrencies got a bad reputation when scams multiplied like ants on a piece of cake. Even today many people associate bitcoin and other … [Read More...]

WHAT IS CLOUD COMPUTING AND WHAT ARE ITS MAIN BENEFITS?

Users are Increasingly using cloud computing to store their information, which is replacing local storage. The business digitization process goes … [Read More...]

Guides

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

Recent Posts

Bit-Depth Printers

When a scanner converts something into digital form, it looks at the image pixel by pixel and records what it sees. That part … [Read More...]

EIDE Interfaces

In 1993 Western Digital brought EIDE (Enhanced IDE) onto the market. EIDE is a standard designed to overcome the constraints … [Read More...]

Expansion interfaces for laptops, notebooks and other mobile computers

In the early 1990s, the rapid growth of mobile computing drove the development of smaller, lighter, and more portable tools for information … [Read More...]

[footer_backtotop]

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