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

Motherboard Audio

Finally, some motherboards provide a pin header that allows connection to a front-mounted audio connector, designed to support stereo audio output (headphone or amplified speakers) and a monochrome microphone input. The Intel Front Panel Connectivity Guide recommends the following design for … [Read More...]

eset nod32 antivirus

Eset nod 32 Anti-Virus 4 Review

PROS: The Eset Nod 32 Anti-virus 4 program is a great alternative to well known software. It assists computer systems in detoxing and protects users from malicious programs. CONS: Eset Nod 32 Anti-Virus protects personal computer users from phishing attacks as well as harmful programs that can … [Read More...]

DynaSheet

Imagine a newspaper that sits neatly in your pocket, is environmentally friendly and gets updated every day. That's the dream that Toshiba unveiled in early 2000 in the shape of its DynaSheet. DynaSheet is a concept product that Toshiba believes could become reality by as soon as 2005. … [Read More...]

Gaming Laptop Security Guide: Protecting Your High-End Hardware Investment in 2025

Since Jacob took over PC Tech Guide, we’ve looked at how tech intersects with personal well-being and digital safety. Gaming laptops are now … [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...]

Guides

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

Recent Posts

How to Remove Win 7 Security 2012

Easiest Removal Option: Remote Repair: Paying an expert to remove this threat is always the easiest option. It's also the most expensive. … [Read More...]

New Software Breakthroughs Leverage Benefits of Online Lending

Each of us faces the urgent need to get relatively small but at the same time quite a serious amount of money. And then you have to choose whether to … [Read More...]

Coupcoup Malware Removal

Coupcoup is another adware application that is put out by JustPlugIt, and it is sure to annoy you as much as the other pieces of computer junk that is … [Read More...]

[footer_backtotop]

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