Hi Hayden,

There is actually a huge difference in object oriented programming in
procedural programming. Unfortunately, this is going to be tough
explaining it in a way that gets the point across without getting too
technical, and I can't explain it without showing you a bit of code
for comparison. So bare with me.

In Procedural programming most of your functions, variables, etc will
be in the public/global scope. For example, at the top of your program
you might declare a number of global variables and then use those
variables in a function something like this.

// Declare global enemy variables
float g_enemyDirection = 0;
int g_enemyHealth = 0;
bool g_enemyKilled = false;
float g_enemyX = 0;
float g_enemyY = 0;
float g_enemyZ = 0;

// Declare global player variables
float g_playerDirection = 0;
int g_playerHealth = 0;
bool g_playerKilled = false;
float g_playerX = 0;
float g_playerY = 0;
float g_playerZ = 0;

// Name: StartGame (void).
// Description: Resets the global variables and
// starts the game.
void StartGame ()
{

// Initialize enemy
    g_enemyDirection = 180;
    g_enemyHealth = 100;
    g_enemyKilled = false;
    g_enemyX = 50;
    g_enemyY = 1;
    g_enemyZ = 75;

// Initialize player
    g_playerDirection = 0;
    g_playerHealth = 100;
    g_playerKilled = false;
    g_playerX = 50;
    g_playerY = 1;
    g_playerZ = 25;
}

What we basically did above is create several global enemy and player
variables and then initialized them in or StartGame() function or
procedure.  This style of programming is called procedural programming
because instead of classifying these variables as belonging to a
specific type of person, place, or thing they are simply globally
added to the stack and used free style. For short programs this style
of programming is okay, but it is also very simplistic. There is a
more advanced and better way to design our program, and that is called
object oriented programming.

In object oriented programming we stop thinking of our enemy and
player as a set of global variables and start thinking of them as
specific objects. We begin thinking of our variables and functions as
members belonging to a type of object. In other words we begin
breaking our program down into specific people, places, and things. We
begin to organize both our variables and functions down as to
belonging to these people, places, and things and classifying them
that way. The advantage of doing it this way is we only need do it
once, and regardless how many times you use the class to create a new
object you will use the exact same functions and variables over and
over again. Here is a simple rewrite of the code above using oop.

// Declare a player class structure
// to declare our player type variables
class Player
{

public: // Public class members

    float Direction;
    int Health;
    bool Killed;
    float X;
    float Y;
    float z;
};

// Declare game player objects
Player g_enemy;
Player g_player;

// Name: StartGame (void).
// Description: Initializes the player objects
// and starts a new game.
void StartGame ()
{

// Initialize enemy objects
    g_enemy.Direction = 180;
    g_enemy.Health = 100;
    g_enemy.Killed = false;
    g_enemy.X = 50;
    g_enemy.Y = 1;
g_enemy.Z = 75;

// Initialize player objects
    g_player.Direction = 0;
    g_player.Health = 100;
    g_player.Killed = false;
    g_player.X = 50;
    g_player.Y = 1;
g_player.Z = 25;
}

So what is the big deal? Lots of things. For starts as you might have
noticed after I created the player class both the enemy and player
objects shared the same direction, health, killed, x, y, and z
variables. This is possible because when you declare a new object of
type Player it actually creates a new set of variables for that object
on the heap behind the scenes. This means you only have to declare
your variables only once and use them  for every object of type Player
that exists in your program. This way you can easily create anything
from a single object of type Player to 100 objects of type Player. It
doesn't really matter because you created a class structure to handle
an unlimited number of game players. With procedural programming if
you want 100 game players you will have to create 100 identical global
variables for 100 players manually, or use a type of structure called
a struct which is something like a class. Which brings us to our
second advantage of object oriented programming.

With a struct it allows C/C++ procedural programmers a type of object
oriented programming, but it is still limited. With structs all of the
variables are public as I did in my sample class as above, and structs
can not be inherited or extended. Both are serious disadvantages that
object oriented programming resolves.

In object oriented programming class members can be either public,
protected, or private. This has huge advantages over a struct or
global variables used in procedural programming because you can set
the access level of a member function or variable. If you have some
variables only used internally by the class you don't want the rest of
your program to access simply add it to the private section of the
class and your program will ignore it and only other members inside
your class will have access to it. This will help from causing bugs by
not allowing access to something that should not otherwise be visible
to your program.

The other huge advantage is that classes can be inherited or extended
by other classes. For example, if you wanted to create specific types
of game players you could do so like this.

class Hero:public Player
{
// Hero code goes here
};

class Monster:public Player
{
// Monster code goes here
};

This way you can create special types of player's without actually
rewriting the basic code we have already written somewhere else in our
program. We Simply have extended or expanded the class structure we
have in place and both the Hero and Monster classes still use the
Player class's variables as well as their own. This technique is
called inheritance, and is a very valuable programming technique in
languages like C++, Java, and Visual Basic .NET. Just to name a
fewobject oriented languages.

Finally, we have pollimorphism. What this term basically means is that
a subclass, I.E. an inherited class, can override or redefine
something that is declared somewhere else in your program. I generally
don't use this more than necessary, but there are times when the
behavior of a certain function doesn't exactly work the way it should
in this certain case and you can override its functionality by using
pollimorphism. The best case I can think of to explain this one is in
.NET or Java when you have to override something like a keydown event
or an application closed event. Instead of using the default ones in
the classes used to create the window you can outright override the
keyboard and closed events to do some sort of specialized task such as
stop your internal threads, dispose of audio, accept keyboard,
commands etc. All you are doing as I said is redefining what that
function or event does in this special case. The advantage of
pollimorphism is you can have your cake and eat it too. You can have
the standard class member do one thing, and create another one with
the same name that does something else. Make sense?

To sum things up the principle difference between object oriented
programming is you are programming from the point of view of a
specific object or objects in mind. Most of your functions, variables,
arrays, whatever will be placed in classes that are shared by all
other objects of that same type be it a person, place, or thing.
Procedural programming is more free style just writing code without
any organization by type or classification. Everything is more or less
global or public to be used by everything else. I hope this
explanation has answered your question as object oriented programming
isn't a difficult concept, but is hard to explain if your audience
doesn't have a programming background.

Cheers!


On 12/16/10, Hayden Presley <hdpres...@hotmail.com> wrote:
> Hi Thomas
> If I may ask...what is the difference between Object Oriented and
> Proceederal programming?
>
> Best Regards,
> Hayden

---
Gamers mailing list __ Gamers@audyssey.org
If you want to leave the list, send E-mail to gamers-unsubscr...@audyssey.org.
You can make changes or update your subscription via the web, at
http://audyssey.org/mailman/listinfo/gamers_audyssey.org.
All messages are archived and can be searched and read at
http://www.mail-archive.com/gamers@audyssey.org.
If you have any questions or concerns regarding the management of the list,
please send E-mail to gamers-ow...@audyssey.org.

Reply via email to