Wacko4X![]() Member Posts: 92 From: Bellvue, WA, USA Registered: 08-21-2002 |
I know Object Oriented Programming is a fairly new and rather complex topic but is there anyone who could give me a basic layout of what it is and its pros/cons?
[This message has been edited by Wacko4X (edited February 07, 2005).] |
|||||
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
Object oriented programming generally lets you treat your concepts as "objects" - an group of related data and actions (usually contained in a struct or class). The data is stored as variables, the actions are functions that manipulate the data. There are generally three characteristics of an object oriented language. These words are big, but their concepts are simple: - Encapsulation - data and actions are grouped together, rather than having the data and actions separate. You can "encapsulate" (isolate and protect) an object's data and internal algorithms, and only provide an interface (the public member functions) that are the only way to manipulate the object. Like a CD player, the speed of the CD, the position of the laser lense, the layout of the transitors and resistors on the circuit board, etc., are all encapsulated away from the user, and the interface is simply the play/stop/skip buttons and the volume control. - Inheritance - the definition of one object can be extended, or "inherited", into another. For example, you might have a class called "Creature" which encapsulates "age", "numberoflegs", "weight", etc. You could create another class that "inherits" the Creature class and automatically receives all of a Creature's member data and member functions. E.g. a "Dog" class derived from "Creature" would contain all the above data and any member functions, plus you could add more, like "owner", "breed". You could further derive a class "Poodle" from "Dog", which would inherit all the member data and functions from both Dog and Creature, and you could add more, like "poofiness" and "yappiness". - PolyMorphism - allows you to treat different kinds of objects as if they were the same. For example, you could have a base class "Shape" with a member function "Draw". Derived from Shape, you could have "Circle", "Square", and "Triangle" classes, each overriding the "Draw" member function. You could create a "Shape" pointer, and point it at a Square object, a Triangle object, or the Circle object: Circle c; pShape = &c; // point to the circle Note all calls to draw ("pShape->Draw();") are the same. That's polymorphism in action. [This message has been edited by BrianT (edited February 08, 2005).] |
|||||
CobraA1![]() Member Posts: 926 From: MN Registered: 02-19-2001 |
A good language to learn OOP in, in my opinion, is Java. No need to deal with all those pointers and stuff. ------------------ Switch Mayhem now available! Get it here |
|||||
Wacko4X![]() Member Posts: 92 From: Bellvue, WA, USA Registered: 08-21-2002 |
Thanks Brian! That was extremly helpful, excellent explanation! Any online tutorials or books that you would recommend for really getting into the nitty-gritty of OOP? Wacko |
|||||
ArchAngel Member Posts: 3450 From: SV, CA, USA Registered: 01-29-2002 |
Java is a great language to begin with. partially idiot-proof. oop. simple. no pointers. lol. ------------------ |
|||||
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
Don't be dissin' da pointers. ![]() I'm reluctant to move to Java simply because I don't want to give up pointers! |
|||||
ArchAngel Member Posts: 3450 From: SV, CA, USA Registered: 01-29-2002 |
quote: are you serious? lol the one thing I don't like about java is that it runs slower and less powerful, having to go through the JVM and all. it is cross-platform, tho... basically, OOP is a programming style modeled after the real world by use of objects. [This message has been edited by ArchAngel (edited February 09, 2005).] |
|||||
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
quote: Well, not really, but I do like pointers. |
|||||
CobraA1![]() Member Posts: 926 From: MN Registered: 02-19-2001 |
quote: I used to think that way also When you say: When you say: Shape B; Now B is also a reference to the same shape. If you change Shape B, you also change shape A, because they are both "pointers" to the same shape.
quote: Sun's latest JVMs' are very fast, and ever since JIT came out, they actually do compile your code to native code. The HotSpot compiler also further optimizes often-used sections of code while the code is running - often used loops will become as fast as (some even claim faster than) C++. ------------------ Switch Mayhem now available! Get it here |
|||||
Klumsy![]() Administrator Posts: 1061 From: Port Angeles, WA, USA Registered: 10-25-2001 |
latest JVM and JIT do speed it up ALOT , and maybe this hotspot compiler might be able to compile faster than some lame C++ compilers, but compared to a good c++ compiler, i don't think so.. I'd say that JVM with even JIT is still slower than compiled Visual Basic, and thats saying something. But yeah i also love pointers and the power of low level stuff, i mean C++ isn't enough, to be able to do cool stuff with some inline assembler is so beneficial.. Mostly though i only use ASM for things that compilers like C++ don't optomise or use well, and that is stuff like MMX, SSE the SIMD stuff that does things in parralel. But for most of my business stuff i don't need pointers and all, and i do dislike C++ for using pointers when its not even needed thus making something look more complicatd that it needs be, but i do like to have the OPTION of pointers ![]() 10 Print "i was a dummy" ------------------ |
|||||
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
quote: That is very bizarre. So how do you know you have a single object with two names, verses two objects? Also, in the following code: Shape B; I'm assuming there is a Shape object (and not just a pointer) created after the first line. If the second line makes B point to the same object as A is pointing to, what happens to the object created in the first line??? In C++, the second line would call B's operator=, so that the two objects are identical, but still to separate objects that can still change totally independent of each other. How would you do that in Java? |
|||||
Klumsy![]() Administrator Posts: 1061 From: Port Angeles, WA, USA Registered: 10-25-2001 |
actually it IS a pointer, its just hidden from users ![]() if you specify an object, it can hold a NULL,nothing value, which means there isn't an object in memory alocated to it (think of pointer to class) so B at first points to nothing when you do ------------------ |
|||||
CobraA1![]() Member Posts: 926 From: MN Registered: 02-19-2001 |
Yeah, Java uses the term "reference" - but for all practical purposes, "reference"=="pointer".
quote: Compare them, just like comparing pointers. if(A==B)
quote: You assume wrong. An object must specifically be created with the "new" statement. B contains null after the first line, and contains A (whatever that is) after the second line.
quote: I'm assuming you're thinking of a more complete example: Shape A = new Shape(); And wondering what happens to the object that used to be in B created in the second line? That depends on whether there are any more references to it. If there's a C pointing to it somewhere, nothing happens. But if it loses all references pointing to it, it then gets put into Java's garbage collector.
quote: Good question - how does one make a copy of a class? Well, the makers of Java were intelligent to take care of that. You see, the class Object is implicitly inherited by all objects. There are two finctions (called "methods" in Java) that Object has, that all objects inherit - clone() and equals(). clone() creates a seperate object, but with the same data. B = A.clone(); // is what you are looking for equals() compares the data inside the objects (remember that a direct comprison just tels whether the pointers point to the same object)
------------------ Switch Mayhem now available! Get it here |
|||||
ArchAngel Member Posts: 3450 From: SV, CA, USA Registered: 01-29-2002 |
although these two greater coders than I just explained it, I'm gonna add my own piece so I can feel smart without actually being smart. say, you make an object out of the class Person: Person fool = new Person(); fool is a reference variable, pointing to an object you made, which exists somewhere in your memory. you access that object through the reference variable. it's kinda a gateway. Person idiot = fool; your not making a new object equal to fool. you do that by: the statement " Person idiot = fool" is pretty just making a new gateway to the object which fool is pointing too. I'm finished repeating what Cobra and Klumsy already said. ------------------ |
|||||
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
Very interesting. I can see how your brain would really need to change gears when switching between C++ and Java. Thanks for the mini tutorial, guys! ![]() |
|||||
Klumsy![]() Administrator Posts: 1061 From: Port Angeles, WA, USA Registered: 10-25-2001 |
actually it would probably be better to make a subclass / decendant class of a person, for the fool, for a fool has some unique characteristics :P ------------------ |
|||||
ArchAngel Member Posts: 3450 From: SV, CA, USA Registered: 01-29-2002 |
true. so... // ( name, age, IQ) ------------------ |
|||||
en972![]() Member Posts: 562 From: NOT TELLING! Registered: 08-27-2004 |
ugh...I'm confused, I'm still trying to get down labels ------------------ |
|||||
ArchAngel Member Posts: 3450 From: SV, CA, USA Registered: 01-29-2002 |
Labels? ------------------ |
|||||
en972![]() Member Posts: 562 From: NOT TELLING! Registered: 08-27-2004 |
You know, I told you on aim ------------------ |
|||||
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
Lables aren't used much in C/C++. The only place you'll usually see them is when gotos are used in the code, so the goto can be told where to, well, go to. ![]() |
|||||
ArchAngel Member Posts: 3450 From: SV, CA, USA Registered: 01-29-2002 |
en, you were talking about reference variables. ------------------ |
|||||
en972![]() Member Posts: 562 From: NOT TELLING! Registered: 08-27-2004 |
i thought we were talking about java ------------------ |
|||||
Klumsy![]() Administrator Posts: 1061 From: Port Angeles, WA, USA Registered: 10-25-2001 |
no we were talking about decaf right. archangel, ------------------ |
|||||
ArchAngel Member Posts: 3450 From: SV, CA, USA Registered: 01-29-2002 |
if jimbob even knows how to use the phone to call his lawyer.... I never defined him with any phone-using method. [This message has been edited by ArchAngel (edited February 13, 2005).] |
|||||
bwoogie![]() Member Posts: 380 From: kansas usa Registered: 03-12-2005 |
i don't know c++ or java... what's a pointer exactly? i'm guessing its creating a new variable and setting a value to it? but if so, how can you not have them? i mean, every program has variables. uhg.. im confused... [vb] ------------------ |
|||||
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
quote: Simply, a pointer is a type of variable whose value is a memory address. The address can be anywhere in memory, but normally you want to set it to something useful, such as another variable, allocated memory, or even a function. For example: int fred; // an integer variable mypointer = &fred; // set mypointer to the address of fred ("&fred") I can provide more complex examples if you want more info. Brian |
|||||
bwoogie![]() Member Posts: 380 From: kansas usa Registered: 03-12-2005 |
so, even though you didnt change the value of mypointer, by setting fred to 10, mypointer automatically changes to 10 also? so instead of setting fred and fredjr to 10, just use a pointer to use less code or something? ------------------ |
|||||
Klumsy![]() Administrator Posts: 1061 From: Port Angeles, WA, USA Registered: 10-25-2001 |
think of variables as a box.. where you can put something in it.. in the real world you have a box of different shapes (computer types) you can put different things in.. you have a CD-BOX that can contain a CD, or a videocassette box you can put a VIDEO CASSETTE in. In computers you have a variables, which are such boxes, ones shaped to put integers, or strings etc.. well a pointer is like a link, to the real box.. if you act this out in real life it should be easy.. grab a pen to use as a pointer and some cd cases, and throw a peice of paper in each. where you can write something on.. so lets start int var1 ; //this is your first CD case //pointer one is not pointing at anything in particular now.. lets start var1 = 10 ; //now you are being the computer, go write 10 on the peice of paper in the first cd case pointer1 = &var1 ; //now POINT the pen towards the first CD case //so now the pointer is pointing to the first CD case.. if we try to find out the value of pointer, we will just get its direction, (or computer address) where it is pointing at.. so if we want to get the value of WHAT IT IS POINTING at we use * var3 = *pointer1 ; //so here we see what pointer1 (the pen) is pointing at.. its pointing at the first CD case, so we open it up.. look and see that it has 10 on it.. then we write that in var3, the third cd case.. pointer1 = &var2 ; //now we changing the direction of where the pointer (PEN ) is pointing. so now it is pointing at the second Cd case.. so now if we do the same line of code as above. var3 = *pointer1 ; //now the pen is pointing at the 2nd cd case, so we look inside and it has 20 in then, so we go write that in var3 (actually var3 already got 10, so we OVERWRITE that value.. so now var3 has 20 written var2 = var2 + 20; //now we go to the 2nd cd case.. see that it already has 20 on it, we add 20... which equals 40 and write that.. var3 = *pointer1 ; //now we do this again.. the pointer is still pointing at the 2nd cd case, but when we look inside, the value has changed.. now it is 40, so we get that number and write it in the third cd case.. if you don't get this by reading it, then act it out.. you can see that pointers can change where they are pointing, and also you can have more than one pointer pointing at the same thing (i.e 2 or 3 pens pointing at hte same cd case.. so when you change what is in the CD case, it seems that all these pointers values have changed, but really they are just pointers.. to see what value something is.. you follow the pointer and look inside the variable/ CD case to see what is there) Karl
mypointer = &fred; // set mypointer to the address of fred ("&fred") grab 3 cd ------------------ |
|||||
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
quote: Sort of. mypointer is "pointing to" fred: i.e. the value of mypointer is the address in memory of where fred lives. So by setting fred to a value, the value of mypointer does not change (it still contains the memory address of where fred lives), but the value it "points to" (i.e. fred's value) does (i.e. fred changes). E.g.
Here's another example;
quote: Not necessarily less code (although sometimes), but a more efficient way of dealing with memory. One area this happens is when passing data to/from functions. With small variables like ints, you usually don't gain much, but with larger variables the efficiency really adds up. For example, in C/C++, you can pass variables to functions "by value" or "by reference". "By value" means you pass the entire value of the variable (not the variable itself, but a COPY of its value), "by reference" means you pass the address of the variable itself. Let's say you had a large structure like this:
To pass it "by value", you would do this:
That would create a copy of the entire large myStruct variable on the stack (and calls constructors, etc., if compiling in C++) accessible by the VerifyVal function. VerifyVal could modify it's copy all it wants, and when it returns, myStruct.val would still = 100, untouched. However, if we passed it by reference:
We're not really passing "myStruct" to VerifyVal, but instead we're passing the "address of" ("&") myStruct. I.e. we're passing a pointer to myStruct. 4 bytes are created on the stack (instead of sizeof(MyStruct)) to hold the pointer (and thus already more efficient), but also no constructors or anything are called if compiled in C++ (because a new object is not being created, we're just referring to an existing object). Also, VerifyVal now operates on the original variable, NOT a copy, so when it returns, myStruct.val will be equal to whatever VerifyVal set it to. This behavior of allowing functions to modify variables is not possible without pointers. In other languages (like Java, I think) passing "by value" (passing a copy of the value) is not possible and passing "by reference" happens automatically behind the scenes. In C/C++ you have to be explicit about which you desire. Here is a pretty good explanation and short tutorial on pointers: There's tons more if you google. [This message has been edited by BrianT (edited March 13, 2005).] |
|||||
bwoogie![]() Member Posts: 380 From: kansas usa Registered: 03-12-2005 |
whoa, those are quite some explainations! thanks. i think i understand now, or at least a hint... ------------------ |
|||||
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
Wait until you start allocating pointers to arrays of pointers. ![]() ![]() [This message has been edited by BrianT (edited March 14, 2005).] |
|||||
bwoogie![]() Member Posts: 380 From: kansas usa Registered: 03-12-2005 |
well, i gotta learn c++ first haha! ------------------ |