Posted: Mon Nov 09, 2009 7:27 pm Post subject: What is Object Oriented Programing?!
Hey, i was wondering if someone could explain to me who OOP works, i believe my current style is procedural programming but im not sure 100% what either is, any clarifications would help. I have wikipedia'd it but its not making much sense to me so if anybody knows how to explain it an a easy-to-understand way, i would greatly appreciate it
Sponsor Sponsor
Tony
Posted: Mon Nov 09, 2009 7:54 pm Post subject: RE:What is Object Oriented Programing?!
Super short version:
In "procedural" paradigm, you have a world (your program) filled with global types and functions. You might have something like:
code:
function cat_speak(my_cat : cat)
put my_cat.name, " says: LOL"
end cat_speak
function dog_speak(my_dog : dog)
put my_dog.name, " says: ROFL"
end dog_speak
OOP allows you to create a system of classes that models the world in terms of "objects". Everything is an "object" and those objects can perform "methods". Ultimately it will allow you to write code in a form of
code:
my_pet.speak()
And the pet will figure out on its own if it's a cat or a dog, and how it should speak.
---------------
Obviously the above leaves out most of the details, but ultimately different programming paradigms (see http://en.wikipedia.org/wiki/Programming_paradigm) are about different ways of representing elements of the program, and defining their interactions.
Posted: Mon Nov 09, 2009 7:56 pm Post subject: RE:What is Object Oriented Programing?!
I like to think of OOP as the ability to box off different groups of information.
For example, you can think of water as an object. You can drink the water, boil the water, put the water in a container, mix other liquids with it, etc. Unless you are a chemist, you don't care how it is composed, why it does what it does or why it even exists. All you care about is that it can keep you hydrated and you are able to use it to create something else.
Another example is creating a "Rectangle" object. The rectangle could of been created using four lines, a fill colour and used an external class. However, all you care about is that it can be drawn to screen, you can move it around, change the colour, change the size, etc.
Basically each class is able to create an object that you can do stuff with. You do not want/need to worry about how the object was created, or why it works (unless you are expanding on a class), as it does what it is supposed to do.
There is a lot of different concepts in OOP, so if you have a specific problem I can provide a more specific analogy/answer.
B-Man 31
Posted: Mon Nov 09, 2009 7:59 pm Post subject: RE:What is Object Oriented Programing?!
no, i dont have any more questions, is there any tutorials on how i could learn OOP here on compsci, i heard its a lot more efficient. And i think im starting to understand. as much info would be appreciated. Thanks Tony and Superskull85 for you input.
Insectoid
Posted: Mon Nov 09, 2009 8:08 pm Post subject: RE:What is Object Oriented Programing?!
That's a question that has stumped many a new programmer.
You've been doing procedural programming. That means you have one program that holds every variable, every procedure and function. Almost anything can do anything to anything. It's rather linear, and you can follow through with it from the top down.
Object-oriented programming is slightly different, in that there is a main program which controls most things, as well as 'objects'. These can be thought of a sub-programs that you can use in your code. These objects are 'imprints' if you will of a class. a class is a template for an object. A common example is the String class. Classes can contain functions (also called methods and other things), as well as straight data (aka variables). An object is an instance of a class. Kind of like a cookie cutter. A class is the cutter, and it can make many cookies.
For example, we have made a Circle class. It has a variable for the radius. It also has several functions to calculate the circumference and area. With this, we can create a number of different circles of different radii and it will do the rest of the work for us. Let's have some pseudo code:
code:
class Circle (radius:double){
function circumference{
return 3.14*radius**2
}
function area {
return 2*3.14*radius
}
}
Now, we can create an instance of this class, an Object:
code:
var foo = Circle.new (5)
We have now created a circle with a radius of 5. We can access this information with dot notation. This is different from procedural programming. You're probably used to:
dot notation uses a dot (.) to call functions within a class
code:
var x = foo.area
Now, let's make another Circle;
code:
var baz = Circle.new (10) //create a circle of radius 10
var y = baz.area
I'm calling the area function, but from the baz object. It will return 2*3.14*10, whereas foo.area will return 2*3.14*5.
This is a very brief explanation, I hope I got something across.
I think the equations might be mixed up. I can never keep them straight.
DtY
Posted: Mon Nov 09, 2009 8:09 pm Post subject: Re: RE:What is Object Oriented Programing?!
B-Man 31 @ Mon Nov 09, 2009 7:59 pm wrote:
no, i dont have any more questions, is there any tutorials on how i could learn OOP here on compsci, i heard its a lot more efficient. And i think im starting to understand. as much info would be appreciated. Thanks Tony and Superskull85 for you input.
What language do you want to learn? Do you already know a programming lamguage, it might already have some OOP.
If you don't already know a language that has OOP, or want to start fresh, learn Ruby. In my experience, it is the "most" OOP language.
I'm not sure where to find it, but my first experience with Ruby was an online Ruby interpreter that ran in the browser, and had a tutorial that walked you through various things you can do, without having to download anything. If anyone knows where to find that, it would be appreciated.
Insectoid
Posted: Mon Nov 09, 2009 8:10 pm Post subject: RE:What is Object Oriented Programing?!
Ah hell, you guys fit 3 posts in while I was typing up mine? I am getting slow. Or long-winded. I dunno which.
DtY
Posted: Mon Nov 09, 2009 8:12 pm Post subject: RE:What is Object Oriented Programing?!
insectoid's description is really good, but I feel the need to point out that that is specifically class based OOP, which is probably what you will be working with, unless you use Javascript. (Which is prototype based OOP)
Sponsor Sponsor
Insectoid
Posted: Mon Nov 09, 2009 8:14 pm Post subject: RE:What is Object Oriented Programing?!
Well, I've only ever used class-based OOP. And I recommend learning OOP with Ruby. Java was a pain in the ass for me, as it tries to do far too much for you and confuses the hell out of you. Ruby is simple, and yet somehow superior to the far more popular (and criticized) Java.
Tony
Posted: Mon Nov 09, 2009 8:16 pm Post subject: Re: RE:What is Object Oriented Programing?!
DtY @ Mon Nov 09, 2009 8:09 pm wrote:
an online Ruby interpreter that ran in the browser
Posted: Mon Nov 09, 2009 8:30 pm Post subject: Re: What is Object Oriented Programing?!
instectiod, thank you so much, im starting to understand even though the code you wrote it in is C++ i believe, it still makes sense because i know basic C++, i was mainly wondering for Turing because i am getting bored in class and its seems too easy. no matter how many things i try to add. i decided learning OOP might be a good way to challenge myself. You guys seem to know quite well what your doing so in turing your liitle program would be like:
Turing:
class circle
export circumference, area
function circumference (radius :real):real result3.14* radius ** 2 end circumference
function area (radius :real):real result2*3.14* radius
end area
end circle
var test :pointerto circle
new circle, test
test -> area (5.12)
put test.area
This doesn't seem to work so can anyone tell me what im doing wrong, thanks
Tony
Posted: Mon Nov 09, 2009 8:35 pm Post subject: RE:What is Object Oriented Programing?!
Posted: Mon Nov 09, 2009 8:38 pm Post subject: Re: RE:What is Object Oriented Programing?!
Tony @ Mon Nov 09, 2009 8:35 pm wrote:
I think you might be looking for something like
code:
new circle, test
put test -> area (5.12)
Why Thank you, that did the trick
This is basically what i have now, this is OOP then (very simply of course)
Turing:
class circle
export circumference, area
function circumference (radius :real):real result3.14* radius ** 2 end circumference
function area (radius :real):real result2*3.14* radius
end area
end circle
var radius :real
var test :pointerto circle
loop new circle, test
put"please enter radius: "..
get radius
put"the area is: ", test -> area (radius) put"the circumference is: ", test -> circumference (radius),skip endloop
[EDIT:] Also can anyone tell me the advantages and disadvantages of OOP?
Insectoid
Posted: Mon Nov 09, 2009 9:07 pm Post subject: RE:What is Object Oriented Programing?!
I find OOP is a lot more organized if used well. You know exactly what is what and what can do what. Procedural programming can quickly turn into a mess since there's not much in the way of organization.
B-Man 31
Posted: Mon Nov 09, 2009 9:12 pm Post subject: RE:What is Object Oriented Programing?!
now i am starting to understand for basics like this circle class, but what if you wanted to make a more visual and bigger harder program, how would you use OOP/classes?