OOP basic concept?

Dile

Newbie
Joined
Jun 13, 2003
Messages
420
Reaction score
0
Just a quick q about OOP: Whats the basic concept?
Why is it better than FOP? How is it executed?

Thanks in advance.
 
Object Oriented Programming (OOP) is what it sounds like. Treat things within the application as though they were objects in the real world.

Let's say you're making a racing game and you want to have all kinds of vehicles (cars, trucks, motorcyles, airplanes, jetskis, etc.). You could create separate chunks of code for each type of vehicle, or you could realize that there are certain properties and behaviors that they all have in common. For example, each vehicle has an engine of some kind. Vehicles that move on land all have tires (some have more tires, some have less). With this knowledge, you can create a 'base' vehicle that has the simplest form and then derrive the other vehicles from that base form. The base object might not even exist in the real work (for example, a vehicle with an engine that holds 1 person, but has 0 tires and can't float). From the base object, you create other objects and add additional properties (like headlights, or CD player), and give it additional behaviors (like it has an autopilot or it can go in reverse, etc.). You keep creating new objects that are based on already existing objects where the only difference is new properties and new behaviors that the previous objects didn't have.

There is a fairly simple test to determine if objects can be created from other objects. The "IS A" relationship is a good way to tell this. If object 'X' IS A 'Y' (for example if German Shepard IS A Dog, then both of these can be objects). Other examples are "Airplane IS A Vehicle", "Shotgun IS A Weapon", "Skateboard IS A Wheeled Vehicle", etc.

You want to create a model of your universe where things are treated as objects and each of these objects has specific properties (like color, size, shape, material it's made out of, cost, weight, etc.) and they each have specific behaviors (like how fast it goes, how much it can carry, how is it controlled, etc.).
 
There really isn't a quick answer to you questions. When i went from learning procedural at college to OO at Uni, it wasnt till the end of the second year that i started to grasp OO properly. (stuck in the procedural mindset)

There are loads of concepts that make up OOP

a small list (not exhaustive)
- classes
- objects
- polymorphism
- encapsulation
- inheritance (single/multiply depending on the language/framework u use)


im not going to go into them all here in fact i dont think i could if i wanted to.

Basically your Data and the Operations (methods/functions) that can manipulate that data are "encapsulated" in an Object. This basically mens that only functions in that object can change/retrieve (get/set) that data. Global variables anyone?


When ur program is first executed it will start at the "root".

Main() here is the "root" of the program that will start everything off.

PHP:
// A "Hello World!" program in C#
class Hello 
{
   static void Main() 
   {
      System.Console.WriteLine("Hello World!");
   }
}

One of the first things u are likely todo is create objects from the classes in ur program.

A class is basically a blueprint for an object.
PHP:
class Hello
{
   static void Main() 
   {

      MyClass MyObject = new MyClass();

      string objectname = MyObject.GetObjectName();  

      System.Console.WriteLine(objectname);

   }
}

class MyClass
{
     private string GetObjectName()
     {
          return ("GoodBye world!");
     }

}

Instantiating your class to create the Object.
PHP:
     MyClass MyObject = new MyClass();

You can then access that OBJECT using the MyObject name. Lets say GetObjectName(for want of a better name) is a method in the MyObject Class.

PHP:
     string objectname = MyObject.GetObjectName();

This will return the string "GoodBye world!" and display it on ur console screen. (i hope :D)

One of the main advantages of OO over procedural (FOP?function Orientated Programming,? thats a guess ive not heard of FOP befor) is supposed to be code reuse.



ok ive just noticed botman has posted befor me, il post this anyway if it helps.

Any crap in here please shoot down i dont want to screw anyones efforts at understanding OO shit.

Jamoe
 
Wow, thanks for both of you, indeed :cheers:

Originally posted by Jamoe
There really isn't a quick answer to you questions. When i went from learning procedural at college to OO at Uni, it wasnt till the end of the second year that i started to grasp OO properly. (stuck in the procedural mindset)
Yeah, i think thats my problem too...
Originally posted by Jamoe

FOP?function Orientated Programming,? thats a guess ive not heard of FOP befor

Yeah, you got it right. Actually, I haven't heard a word for it in english (I'm hungarian, you know), so I used my translating skills and translated "Funkció Orientált Programozás"(its in my biiig c/c++ book)to "Function Oriented Programming".



After all it seems I'm going to have lots of long nights to understand it...
 
Originally posted by khaki
A little write-up of FOP.

http://www.cat.utexas.edu/fop.html

I wouldnt mind seeing an example of this working. At the moment its not very clear to me.

I see that you have say 10 features, with each feature wrapping a number of classes (it says FOP augments OOP).

How does this actualy effect the programming? or is it just a an organisational thing. I.e these classes are used for this feature etc. Almost like classes can be wrapped in clusters or namespaces but aimed at the feature description side?

I dont know sounds interesting though. If only i was back at Uni nand had more time :)

Jamoe
 
Originally posted by Dile
Why is it better than FOP? How is it executed?

Its a popular misconception that OOP is automagicaly better/worse than anything else, as with all programming methods the best one is the one which best fits the problem at hand.
Learn about them all by all means, but dont crowbar EVERYTHING into one method as sometimes its just not the correct way to visualise a problem
 
Originally posted by Dile

Why is it better than FOP? How is it executed?

My guess is that OOP is meant to be easier for humans to learn and understand, yet still have the features of a real programming language.
 
Back
Top