Αναζήτηση αυτού του ιστολογίου

OOP - Object Oriented Programming for Beginners

OOP For Old School Programmers
Just remember how we use to make programs in the old days, before OOP, using i.e Pascal.

We use to first create a section with declaration of variables/functions and then the main programm was started.



A Typical Pascal programm was like this:


In pascla we use to declare functions/procedures that can be called anytime from the main code.
Or a procedure / function could call another procedure / function.



In tradittional programming, functions return results on their name.
Procedures return results as arguments next to them.
Example:
Function Pythagoras(A: Real; B: Real) : Real; {The pythagoras theorem}
Begin 
Pythagoras := SQRT(A*A + B*B);
End 

From your main program , the variable "Pythagoras" has a value.
For example Writeln(Pythagoras(5,8)); Prints the calculated results of 5*5+8*8 just by printing out the function's name.

With Procedures the example bellow becomes:
Procedure Pythagoras(A: Real; B: Real ; Var Result : Real);
Begin
Result := SQRT(A*A +B*B);
End;

In this case you need to call from main programm the procedure like Pythagoras(5,8,Res)
You need to declare and pass a variable Res , and this variable Res carry the result : Writeln(Res)
The global value Res you send is assigned to the procedure private variable Result.
You can no call directly the variable Result in your code.

Off course you can built complex programms in Pascal and call procedures inside procedures (by respecting some pascal's rules).

Okay, what about Records? Do you remember Pascal Records?
Book = Record
Title: String;
Author: String;
Price : Real;
End;

By delcaring such a record, we could assign to a new variable in our program above record like this:
var magazine:book;
And we can make use of the record elements in our variable by referring to them as
magazine.title:="Evening News"; or writeln(magazine.title);

A more sophisticated edition of Pascal Records are the Pascal Objects which have been replaced by Pascal class.

As a programming side of view, we could say that OOP use advanced records with a lot of procedures/functions inside .

OOP for non programmers
I learned traditionall programming in high school. Teacher use to explain things in computers using real world examples.
The best real life example to describe OOP is this one:
http://programmers.stackexchange.com/questions/34584/how-to-explain-oop-concepts-to-a-non-technical-person#_=_

Let's talk about vehicles , cars and trucks.
Vehicles a general term is what is called by OOP a class, (a wrapper) and contains general properties and events applicable to all vehicles (i.e color property, driver property,  movement event)

Car and Truck has vehicle properties plus some other particular properties for each vehicle.

So you can particulary defind under Vehicle class a car object what will have the properties of the vehicle (inheritance) plus some other dedicated properties (i.e number of wheels property, weight property, number of doors).

Similary you can define a truck object, an airplane object, and more "vehicles".

What confuses me is that in the vehicles example we could say that even the car properties could be declared as vehicle properties . Only the values will be different.
For example the "weight" property could be a class property. In car will be 3000 kg, in truck will be 10000kg. But it is a common property. Same story for wheels, doors, etc.

This is probably the reason that class is usually described as an object it self.

An example in Python using OOP:
(Full code here: https://github.com/gevasiliou/PythonTests/blob/master/RightClick.py)



def =define
add_finger(self, slot) -> self refers to the class itself, slot is a variable that will be sent in function.
Main programm will call this function as ObjectName.add_finger(value to send)

In the code given at github, you can see after the last class def section, that the main program begins using :
def initiate_gesture_find():
<.... initiate_gesture_find codes....>

You can observe that gesture find is at the same indent level of tracked event class , and in Python indentation matters (traditional programming: first global declarations, procedures and functions, then the main program).

Scrolling down you will see that a new variable called MT_Event is declared , with value either None (none = an empty object) or MT_Event=TrackedEvent() -> The class object that has been declared in the beginning is assigned to the variable MT_Event.