Why Object Oriented Programming is so Important
Posted on March 29th, 2009 in Object Oriented Programming |
The number one question asked by newbie OOP-ers has to be: “Why do I need object oriented programming? Can’t I just use functions???” Well, maybe I should explain a little as to enlighten you.
First, one must consider the purpose of classes to understand them. Classes were created as to help programs more closely resemble reality. Classes make things called objects, which have properties and can be manipulated. This makes any application easier to build.
Consider this example: You need to build a farm game. You have a library(like ALLEGRO) for graphics, one for sound, one for controls, etc. Now, you want to make it so the user may create animals. If you were doing this through functions, you would have to make one for pigs, one for cows, etc. Each of these would have the properties of the respected animal. However, a dog and a cow both have hair; they share that property. This is where OOP comes to the rescue. Object Oriented Programming allows something called inheritance. One class can detract from another, and in doing so, gets(or in a more sophisticated way: “inherits”) it’s properties.
This means, that you have to write once, use as many as necessary. This allows to better code that is less prone to bugs.
Now this is a pretty minimalistic example. I hope to come back to this subject later and give a larger scale presentation.
Until next time
~mike