Game Programming and Development Tools

OOP – Wacko4X

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?


Also, what languages partially or fully utalize OOP?

[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;
Triangle t;
Square s;
Shape *pShape;

pShape = &c; // point to the circle
pShape->Draw(); // calls Circle:: Draw
pShape = &t; // point to the triangle
pShape->Draw(); // calls Triangle:: Draw
pShape = &s; // point to the square
pShape->Draw(); // calls Square:: Draw

Note all calls to draw ("pShape->Draw();") are the same. That's polymorphism in action.

------------------
Brian

[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.

------------------
"The very idea of freedom presupposes some objective moral law which overarches rulers and ruled alike." -- C. S. Lewis (1898 - 1963), "The Poison of Subjectivism" (from Christian Reflections; p. 108)

Switch Mayhem now available! Get it here
Codename: Roler - hoping to get more done over the holidays . . .

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.

------------------
Soterion Studios

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! I can't imagine working without them. Pointers are my Secret Power (tm).

------------------
Brian

ArchAngel

Member

Posts: 3450
From: SV, CA, USA
Registered: 01-29-2002
quote:
I'm reluctant to move to Java simply because I don't want to give up pointers!

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.
like, things happen in the real world by certain objects doing things with other objects,etc. well, OOP takes that concept.
------------------
Soterion Studios

[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:
Originally posted by ArchAngel:
are you serious? lol

Well, not really, but I do like pointers.

------------------
Brian

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
quote:
I'm reluctant to move to Java simply because I don't want to give up pointers! I can't imagine working without them. Pointers are my Secret Power (tm).

I used to think that way also . But you pretty much do the same stuff in Java as you do C++. Whenever you do something like pass an object to a function or whatnot, you're actually passing its reference. All Java objects pretty much act like pointers.

When you say:
Shape A = new Shape();
A is now a reference to a new instance of Shape

When you say:

Shape B;
B = A;

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:
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.

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++.

------------------
"The very idea of freedom presupposes some objective moral law which overarches rulers and ruled alike." -- C. S. Lewis (1898 - 1963), "The Poison of Subjectivism" (from Christian Reflections; p. 108)

Switch Mayhem now available! Get it here
Codename: Roler - hoping to get more done over the holidays . . .

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 . However once upon a time when i first learnt C-64 basic, before i learnt assembler or pascal or C, i though how could somebody program without line numbers..

10 Print "i was a dummy"
20 goto 10

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
quote:

Whenever you do something like pass an object to a function or whatnot, you're actually passing its reference. All Java objects pretty much act like pointers.

When you say:
Shape A = new Shape();
A is now a reference to a new instance of Shape

When you say:

Shape B;
B = A;

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.


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;
B = A;

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?

------------------
Brian

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
then when you do
B = A
bother a and b point to the same physical object. so really such a thing is a pointer to an object, or a reference to an object..

when you do
B = A you are Not cloning the object.. unless you do something like
B = A.clone();

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
Yeah, Java uses the term "reference" - but for all practical purposes, "reference"=="pointer".

quote:
So how do you know you have a single object with two names, verses two objects?

Compare them, just like comparing pointers.

if(A==B)
{ System.out.println("A and B are the same object");
} else
{ System.out.println("A and B are two different objects");
}

quote:

Shape B;
B = A;

I'm assuming there is a Shape object (and not just a pointer) created after the first line.


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:
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???

I'm assuming you're thinking of a more complete example:

Shape A = new Shape();
Shape B = new Shape();
B=A;

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:
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?

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)


if(A==B)
{ System.out.println("A and B are the same object");
} else if(A.equals(B))
{ System.out.println("A and B are two different objects with the same data");
} else
{ System.out.println("A and B are two different objects with the different data");
}

------------------
"The very idea of freedom presupposes some objective moral law which overarches rulers and ruled alike." -- C. S. Lewis (1898 - 1963), "The Poison of Subjectivism" (from Christian Reflections; p. 108)

Switch Mayhem now available! Get it here
Codename: Roler - hoping to get more done over the holidays . . .

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.
if you go.

Person idiot = fool; your not making a new object equal to fool. you do that by:
Person idiot = fool.clone(); (but that's a different story. and you need to define the clone method.)

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.

------------------
Soterion Studios

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!

------------------
Brian

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

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

ArchAngel

Member

Posts: 3450
From: SV, CA, USA
Registered: 01-29-2002
true. so...

// ( name, age, IQ)
Person JimBob = new fool("JimBob," 23, 45 );

------------------
Soterion Studios

en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
ugh...I'm confused, I'm still trying to get down labels

------------------
Keep the holy day sacred.......halllllllukan

ArchAngel

Member

Posts: 3450
From: SV, CA, USA
Registered: 01-29-2002
Labels?

------------------
Soterion Studios

en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
You know, I told you on aim

------------------
Keep the holy day sacred.......halllllllukan

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.

------------------
Brian

ArchAngel

Member

Posts: 3450
From: SV, CA, USA
Registered: 01-29-2002
en, you were talking about reference variables.

------------------
Soterion Studios

en972

Member

Posts: 562
From: NOT TELLING!
Registered: 08-27-2004
i thought we were talking about java

------------------
Keep the holy day sacred.......halllllllukan

Klumsy

Administrator

Posts: 1061
From: Port Angeles, WA, USA
Registered: 10-25-2001
no we were talking about decaf right.

archangel, - though jimbob may sue for defamation.

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

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.
------------------
Soterion Studios

[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]
if confused = true then
msgbox("Confused = very yes!")
end if
[/vb]

------------------
~~~boogie woogie woogie~~~

Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
quote:
Originally posted by bwoogie:
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...

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
int wilma; // an integer variable
int *mypointer; // a pointer (to an int) variable

mypointer = &fred; // set mypointer to the address of fred ("&fred")
fred = 10; // *mypointer ("the value contained at where mypointer points to") is 10
wilma = 20;
mypointer = &wilma; // set mypointer to the address of wilma, *mypointer is now 20.

I can provide more complex examples if you want more info.

Brian

------------------
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?

------------------
~~~boogie woogie woogie~~~

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
int var2 ; //this is your second CD case
int var3 ;//this is the third cd case
int *pointer1 ; // this is a pen used as a pointer.
int *pointer2 ; // this is a pen used as a pointer.

//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
var2 = 20 ; //as above

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


int fred; // an integer variable
int wilma; // an integer variable
int *mypointer; // a pointer (to an int) variable

mypointer = &fred; // set mypointer to the address of fred ("&fred")
fred = 10; // *mypointer ("the value contained at where mypointer points to") is 10
wilma = 20;
mypointer = &wilma; // set mypointer to the address of wilma, *mypointer is now 20.

grab 3 cd

------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
quote:
Originally posted by bwoogie:
so, even though you didnt change the value of mypointer, by setting fred to 10, mypointer automatically changes to 10 also?

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.


mypointer = &fred; // mypointer is set to the "address of" ("&") fred
fred = 10; // fred, and thus the value that mypointer points to, = 10
*mypointer = 20; // what mypointer points to ("*"), and thus fred, = 20

Here's another example;


char color[] = "purple";
char *cp = NULL; // a pointer to a character, currently pointing to NULL (memory addess 0)

cp = color; // these two lines are essentially the same. cp is set to
cp = &(color[0]); // the address of the first character in the char array
// i.e. cp == the memory address of the first character
// and *cp == the first character itself ('p')

cp++; // increment cp, i.e. add one to its value (which is an
// address). cp now equals the memory address of the
// second character, and *cp equals the second
// character itself ('u')
*cp = toupper(*cp); // pass what cp points to ('u') to the toupper
// function, and store the result ('U') in what cp
// points to. After this call, the color string will
// be "pUrple".
cp += 2; // add 2 to cp's value (i.e. add 2 to the address),
// cp now points to the fourth character, *cp equals 'p'
*cp = 'g'; // the color string is now "pUrgle"

quote:

so instead of setting fred and fredjr to 10, just use a pointer to use less code or something?

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:


struct MyStruct
{
int val;
float x, y, z;
char name[100];
};

To pass it "by value", you would do this:


MyStruct myStruct;
myStruct.val = 100;
VerifyVal(myStruct); // passing by value

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:


MyStruct myStruct;
myStruct.val = 100;
VerifyVal(&myStruct); // passing 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:
http://www.cplusplus.com/doc/tutorial/tut3-3.html

There's tons more if you google.


------------------
Brian

[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...

------------------
~~~boogie woogie woogie~~~

Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
Wait until you start allocating pointers to arrays of pointers. A little hard to wrap your head around it at first, but after doing it a few times, you'll be Mr. Pointer Guru.

------------------
Brian

[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!

------------------
~~~boogie woogie woogie~~~