OOP

Questions about programming languages and debugging
Post Reply
User avatar
Swan
Knight of the Sword
Knight of the Sword
Posts: 827
Joined: 18 Oct 2006, 16:00
17
Contact:

OOP

Post by Swan »

Object orientated programming (OOP) was something always turned me off and made me lose interest, primarily because at first I could not quite appreciate the easiness or indeed the power of it. OOP is a truly innovative aspect of coding, and reduces a significant proportion of the workload of the coder, and so it is certainly a worthwhile feature to become accustomed to.

Programming languages are broadly divided into procedural languages:
languages which are not 100% OOP languages but which utilise some degree of OOP PHP and Ruby to name just two. Some programs are fully OO with the likes of Flash and Java. Personally, I think a procedural language such as PHP or Ruby is an excellent way for the novice to become better accustomed to OOP before immersing themselves completely.

OOP has a number of unique components, individual to it. Broadly speaking there are 2 main entities in OOP, and the way I imagine it as is a door and a key.

Data (which happens to be door) consists of a number of variables, which in the OOP world are called properties.
A collection and groups of Functions (which are the keys of OOP) with these functions used to modify the data, or properties. In the OOP world, these are called methods.

The collective name for properties and methods combined are classes and just to recap, methods are used to alter the properties. With me so far?
Whenever you define a class (bring it into existence in your code) unlike ordinary coding, you do not just create an object. Instead, you create a template for that object, so instead of creating an apple, you would have a basket of apples, or an apple tree, allowing you to reuse that “object” over and over again. Because you use objects derived from the template, you can then easily modify and alter the objects to your suiting.

Classes come in a hierarchy and for ease I will talk about PHP context. The classes are ranked according to their level of detail and sophistication and indeed precision, the standard class (which in PHP is called stdClass)is the most generic and broad of all the classes. Each of the classes beneath it will become steadily more narrow and specialised.

The class below a class is called the child, and the one on top is the parent. That is a clumsy way of describing it, but remember this basic rule, the child class is ALWAYS more specific, specialised and detailed than its parent class counterpart.

Code: Select all

class member_list { 
var $members;

function update($members) {
...
}
} 
Ok.

The class keyword is used to create/initiate a new class. member_list is the name of the class and the curly brace { is used to open the class. Classes therefore, are quite similar in syntax and form to functions.

var$members is used to create a variable called members, and the var keyword in the context of classes is used to declare properties.

If you are confused by the 4 curly braces let me explain:

Code: Select all

class member_list {   //Used to begin class
var $members;

function update($members) { //Used to begin function 
...
} //End function
}//End class
You can also assign values to property so:

Code: Select all

class member_list { 
var $members= “Swan”;

function update($members) {
...
}
} 
By no means exhaustive or even detailed, but will update as soon as I learn more. Was as much a revision exercise for me.

bubzuru
.net coder
.net coder
Posts: 700
Joined: 17 Apr 2007, 16:00
17
Contact:

Post by bubzuru »

i think this is a better sample to show OOP

it is a simple person class , it has two property's
name , age and one function say hello

Code: Select all

Public Class Person

    Private pname As String
    Private page As Integer

    Public Property Name()
        Get
            Name = pname
        End Get
        Set(ByVal value)
            pname = value
        End Set
    End Property

    Public Property Age()
        Get
            Age = page
        End Get
        Set(ByVal value)
            page = value
        End Set
    End Property

    Public Sub SayHello()
        MsgBox("Hello my name is " & pname & " and i am " & page)
    End Sub

End Class

Code: Select all

Dim paul As New Person
paul.Name = "Paul"
paul.Age = 17

paul.SayHello()

[img]http://www.slackware.com/~msimons/slackware/grfx/shared/greymtlSW.jpg[/img]

User avatar
Big-E
Administrator
Administrator
Posts: 1332
Joined: 16 May 2007, 16:00
16
Location: IN UR ____ , ____ING UR _____ .
Contact:

Post by Big-E »

Eww @ proprietary Microsoft language

I think you missed the whole 'constructor' part of this, before you can create the object you must have a constructor method in that class, so say you have the 'AppleTree class', you will need to create the 'AppleTree' which in return creates various 'apples' - an example

Code: Select all

class appleTree{
    varApple1;
    varApple2;
 
    function appleTree(int a){  //this would eb the constructor
        int rand = a; 
        varApple1 = 2 * rand;
        varApple2 = 1 * rand;
    }

}


--------------------------------

Now here is a new class
which is implementing appleTree class

var appleTreeVar = new appleTree(8);
...then like you said, you can create other methods like getAppleVar1() ..setappleVar1() ...etc all depending on how you want to manipulate that object. Then there are also the principals of inheritance and polymorphism which is a little bit more advanced, I suggest getting a grasp of objects/classes/methods before divulging into that.

Post Reply