OOP (Object-Oriented Programming) is arguably one of the most important things in programming (besides the obvious basics: if statements, math, etc.). It allows you to create multiple objects of the same type that can have operations performed on them without having to use functions in the program-scope.

Simply speaking, OOP is a way to control multiple objects as opposed to the program as a whole. Using OOP allows for modularity, more versatile code, and other neat little features.

Let's imagine that you're creating a game (this is a pretty easy parallel for me to draw as I designed a game at one point that used no OOP whatsoever; biggest mistake of my life). Imagine that you have a player character that has an inventory capable of carrying 32 items. Perhaps you have an Adrenaline Syringe that boosts your speed, an AK-47, pistol, and grenade that are all wieldable, and a rock that has no purpose. In theory, we'd like to tell the computer that if the user has the inventory open and clicks on an item, to perform a generic "Use" action on it.

Without OOP, this is a fairly stupid task to accomplish. It means that most likely have to create five functions (just for these five items):

void Use_Adrenaline_Syringe() {
    // Some code that plays an animation, destroys the inventory item, and boosts your speed.
}

void Use_AK47() {
    // Some code that plays an animation, destroys the inventory item, and places it in your hand.
}

void Use_Pistol() {
    // Some code that plays an animation, destroys the inventory item, and places it in your hand.
}

void Use_Grenade() {
    // Some code that plays an animation, destroys the inventory item, and places it in your hand.
}

void Use_Rock() {
    // Some code that displays a message coincident to "This item cannot be used."
}

You can see that the code in the three middle functions would be pretty redundant. Instead, when doing something with OOP, you can create classes that act as a single object that can be duplicated and modified over and over. In this case, you may have something like this:

class Item {
    Item() {};
    void Use();
    void Remove_From_Inventory() {
        // Some code that removes this item from the inventory.
    };
};

class Item_Powerup : public Item {
    Item_Powerup() {};
    void Use() {
        // Some code that plays an animation and boosts your speed.
        this->Remove_From_Inventory();
    };
};

class Item_Weapon : public Item {
    Item_Weapon() {};
    void Use() {
        // Some code that plays an animation and places the weapon in your hand.
        this->Remove_From_Inventory();
    };
};

class Item_Useless : public Item {
    Item_Useless() {};
    void Use() {
        // Some code that displays a message coincident to "This item cannot be used."
    };
};

void On_Game_Start() {
    Item* Adrenaline = (Item*)(new Item_Powerup());
    Item* AK47 = (Item*)(new Item_Weapon());
    Item* Pistol = (Item*)(new Item_Weapon());
    Item* Grenade = (Item*)(new Item_Weapon());
    Item* Rock = (Item*)(new Item_Useless());
}

void On_Item_Used(Item* item) {
    // This function would be called whenever an item was clicked to be "Used."
    item->Use();
}

Now, by line count, the second code is far larger. But think about when you have to add forty more weapons, maybe some body armor, all kinds of things. Now if you have 600 items in the game, you don't need 600 functions; you can instead sort them into categories (Powerup, Weapon, Armor, etc.) and base object off of a generic class. Then, as in the On_Game_Start function, create each object from its base class.

This perhaps isn't the most expansive example in the world, but it demonstrates the point. Perhaps it's better to think of a thousand players or AI in a game; do you really want to have functions for each one of those thousand characters? Not really.

OOP expands beyond games, obviously. wxWidgets, a great GUI maker, is OOP-based. Every single frame, button, checkbox/radiobox, text field, and so on are all objects based on a class (wxFrame, wxButton, etc.). A while ago I created a Matrix Calculator in Python; I created a single class called "Matrix" which houses functions like Transpose(), Inverse(), Add(), Multiply(), and so on. It would have been quite a nightmare to give the program justifyable data management if not for OOP.

More and more scripting languages (which differentiate from programming languages) are using OOP; Python, PHP, and Ruby all support it, and they all server their purpose well (though I cannot attest to Ruby's implementation as I have not more than seen it). CLEO (the PHP framework I designed for this web project), for example, is entirely OOP-based:

# This is the CLEO version of a socket connect to Google:
echo var_dump(clSock::Make("74.125.53.104")->SetPort(80)->Connect()->Connected()); // Echoes: bool(true)

# The same code using PHP's default code:
$socket = socket_create(AF_INET,SOCK_STREAM,"tcp");
$success = socket_connect($socket,"74.125.53.104",80);
echo var_dump($success); // Echoes: bool(true)

The power of CLEO's OOP processing goes beyond this; I can perform hundreds of string operations on one line (conversions from upper to lower case, getting substrings, etc.), or perform color operations (converting from RGB to HSL and Hex), and so on.

As should be apparent by now, the power of OOP cannot be expressed in a single blog post. It would take hundreds of these things for me to tell the world about all the fantasic features of polymorphism (OOP).

If you're interested in more, here's a few links you can take a look at:

  • http://wiki.tcl.tk/13398
  • http://pj.freefaculty.org/Swarm/Beta/SwarmUserGuide/swarm.user.user1.02-obj-adv.sect1.html
  • http://siber.cankaya.edu.tr/AdvancedProgramming/fall2004/ceng241/node11.html
  • http://www.geekinterview.com/question_details/37213
  • http://www.oop.esmartkid.com/
  • http://www.google.ca/search?hl=en&q=OOP

Next Post Previous Post