11
Posted January 12, 2011 by Spyros in General Programming
 
 

What is Object Oriented Programming and Why You Need to Use It

animals
animals

Writing a small program every now and then is what most programmers do all the time. However, there are occasions when we need to create something bigger. Imagine that you wanted to create a small RPG game. When you are into this type of programming, you certainly undestand that this will take lots of hours to write and probably your code needs to be pretty well organized. The point is that you cannot just go about writing an RPG game without any previous preparation.

How to Code Large Programs Properly

This is where you need to be aware of object oriented programming. You are certainly hearing that everywhere and it may seem fancy and difficult but it is really not so. What OOP(object oriented programming) stands for, is just a way to properly organize your code into logical components that are easier to program.

Take as an example the RPG game. Picture that you have a main character, let’s say RPGMan and then, there are some dungeons that your character will visit. Also, there are some monsters hidden in those dungeons and some chests guarded by those monsters.

Now, this is becoming a bit complicated. How will you go about coding all these sorts of different things ? Well, this is where OOP come to the rescue. The most important idea about OOP is classes.

What is a Class ?

A class is quite simply a logical component of your game (in this ocassion). Each class will be responsible for handling a different part of your game elements. For example, there will be a class named MainCharacter, representing your main RPG character. Then, there will be a class called Monster, a class called Dungeon and another one called Chest.

A class has certain attributes and functions. The standard way to create a class is allow it to have some private variables that can be accessed only by using public or private functions inside that class. Let’s take the Monster class as an example. At some point in your program, you need to create a monster and assign a certain aggresiveness level to it. Our Monster class could be like :

Monster Class

private int aggressiveness;
public function setAggressiveness(int aggresiveness);

At that point, you see that our class contains a non directly accessible aggressiveness variable that can only be changed via the setAggressiveness function. Somewhere in our program, we could be having something like :

Monster gargoyle = new Monster();

We create a monster object here and maybe after some more things are executed in our code, we now need to change its aggresiveness level. So, later in our program we have :

gargoyle->setAggressiveness(10);

This simplifies our code and we now easily identify that we have set the aggressiveness level of the gargoyle monster to be 10.

A Short Description of Polymorphism and Inheritance

I won’t go into great detail on what class polymorphism is and how it works, in this tutorial, but i will provide a short explanation. Let’s think of the Monster class again. Not every monster is the same. Some of them, for instance, may have to consume some materials in order to be faster or stronger in the game. Therefore, the Monster class has to be pretty versatile. This is where inheritance and polymorphism will help. A monster class just describes the basic monster attributes. However, when we need to make it more tied to a certain monster, we should better create a new class, say the Gargoyle class, and have it inherit the Monster class attributes. This is the main inheritance idea.

So, now our Gargoyle class can do pretty much what its basic class(Monster) can do. However, we need to extend it a little bit more. If our main Monster class contains a virtual function named feed(), we can now decide how a gargoyle is going to be fed. A virtual function in Monster is just a function that can be changed when inherited by another class. Therefore, we can design it specifically for every new monster type that we create. This is the most important part of polymorphism, the ability to change the basic class functionality in our extended class.

This concludes the basic things that you need to know about object oriented programming. There can be lots of different things discussed on that very subject. I will end this post here though and will write more about this in later posts. Thanx for reading this one and i hope that it helped you understand the basics of OOP.


Spyros