Public Group

#### Archived

This topic is now archived and is closed to further replies.

# C++ classes, functions, pointers

This topic is 5426 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

As the title says I'm currently reading a few online tutorials but I have not got very far yet. including the one on howstuffworks and the one here Teach Yourself C++ on InformIT.com I am totally confused when I get to the classes area could somebody try to straighten things up for me? I am also confused on why in the hell would anyone ever use pointers? I just dont get it. Functions are alittle fuzzy...but someone told me it should be at the point I am at..... Well I'm hoping this doesnt start a flaming thread.... Thanks in advanced Joey, Owner of Max'd Gaming (obviously NOT programming for it) (doing sounds and 2d art NOT PROGRAMMING) (By the way I AM NOT PROGRAMMING) (DID I TELL YOU I AM NOT PROGRAMMING) EDIT: Fixed subject from the 'WARNING TOTAL MORON ON THE LOOSE!!!' to the current, more descriptive one. Edited the URL to prevent horizontal scrolling. And moved this to the For Beginners forum. -- Kylotan. [edited by - Kylotan on August 11, 2003 11:05:53 AM]

RTFM

##### Share on other sites
First, show a little dignity and change the thread title. That might get more people to read your post.

edit - I didn't bother to copy and paste the link. What language are you using?

[edited by - jmg5 on August 10, 2003 10:52:47 PM]

##### Share on other sites
if you are NOT PROGRAMMING then why are you asking this.

##### Share on other sites
The link is to Sam''s Teach Yourself C++ in 21 days. And geez, don''t be asses about it, AP in particular...

IMO, classes in C++ made more sense after using Java where everything is a class. Functions... I don''t really know how to explain anything to you without a better explanation of what you''re fuzzy about. And pointers still piss me off.

##### Share on other sites
This thread screams "Don''t help me," you know that right?

##### Share on other sites
Well, the questions don't make you a moron, we've all been there and we've all asked those questions. The easiest way that I can think of explaining why we want to use pointers is this:

Let's say that you have a class in your program called Character.

class CHARACTER{     public:          char CharacterName[10];          int PlayerPositionX;          int PlayerPositionY;                    CHARACTER();          ~CHARACTER();};

This class holds the CharacterName and the x and y coordinate. All members (variables) and functions are public to make this easier to explain. A class works just like a structure except you can have private or public variables and functions. I'm not sure if you could have private variables in a structure (which wouldn't make sense, so I don't think that you can) and you couldn't have functions. This just allows you to group like things together instead of having a mess of global variables and loose variables strewn about your program.

Now, lets say that you have your CheckKeys() and GameMain functions here that checks to see if a user has pressed a key and changes the coordinate variables:

int GameMain(){     CHARACTER Player;     BOOL Continue = true;     while(Continue)     {          CheckKeys();     }     return 0;}void CheckKeys(){     if(VK_LEFT)  // Pseudo code     {          // Change character coordinate left     }      if(VK_RIGHT)   // Pseudo code     {          // Change character coordinate right     }}

Well, you need to somehow get the character data over to the CheckKeys function so that it can manipulate the character coordinates based in the key that was pressed.

If we change the CheckKeys() function to this:

void CheckKeys(CHARACTER Player){     if(VK_LEFT)  // Pseudo code     {          Player.x -= 1;     }      if(VK_RIGHT)   // Pseudo code     {          Player.x += 1;     }}

We actually run into a problem. You now have two CHARACTER classes in memory. One from the GameMain() function and now one in the CheckKeys() function. When you pass Player from the GameMain() function:

int GameMain(){     CHARACTER Player;     BOOL Continue = true;     while(Continue)     {          CheckKeys(Player);     }     return 0;}

This copies the values that are in the Player class in the GameMain() function to the Player class in the CheckKeys function. Thus you have two classes just with the same information in them. When you change the data in the CheckKeys() Player class, it only changes that one. The original in the GameMain() function is left unchanged.

If you change the CheckKeys() function to this:

void CheckKeys(CHARACTER *Player){     if(VK_LEFT)  // Pseudo code     {          Player->x -= 1;     }      if(VK_RIGHT)   // Pseudo code     {          Player->x += 1;         }}

Now the changes you make to the Player class in the CheckKeys() function are actually changing values in the Player class in the GameMain() function. It's basically saying, I want you to make changes to this variable and not just copy the data.

So the following allows CheckKeys() to change the Player class in GameMain():

int GameMain(){     CHARACTER Player;     BOOL Continue = true;     while(Continue)     {          CheckKeys(&Player);     }     return 0;}void CheckKeys(CHARACTER *Player){     if(VK_LEFT)  // Pseudo code     {          Player->x -= 1;     }      if(VK_RIGHT)   // Pseudo code     {          Player->x += 1;         }}

As you noticed in the previous example I used a '.' to in Player.x . Well, the reason for the change is really simple, the class is now a pointer. When you have a pointer to a class you just use -> instead of . and then you don't have to use a *. Make sense?

Now, I know your probably thinking, well why not just pass it normally, without a pointer, and have the funtion return the changed class? Well, what if you have to pass two classes or sets of variables? It's basically giving a function permission to change a variable that is in the calling function. This is so you can spread your code out to several or more functions, thus reducing the size of each function and making them easier to read and follow. And it helps you organize things.

As for the classes, you just have to stick with it and really just try things out. You may not understand all of it as you read, but code up the books examples and play with them while reading through that section again. Once you learn them, you can't stop using them. They are very useful and I can't really code a program without them anymore.

[edited by - Daishi on August 10, 2003 11:40:11 PM]

##### Share on other sites
You said you weren't completely sure about what a function was so I'll give you a quick example. Heres the source code to a function that prints out the variable you pass into it--

void printOut( int x )
{
cout << "The Variable Passed In Is Equal To " << x << endl;
}

you would just type something like:

int x=5;
printOut( x );

and the output would be,
"The Variable Passed In Is Equal To 5"
assuming you gave it an integer, otherwise you'll get a compile error unless you create a separate function for characters, etc.

Heres an example of a function that returns a value:

int SecondPower( int i )
{
return ( i * i )
}

then you would type something like:

int x=5;
int y=0;
y = SecondPower( x ); // Y Is Now Set To x*x since the function returns the number passed in times itself
cout << x << " to the second power is: " << y << endl;

You CAN think of a function that returns a value as kind of a variable. You can type y = SecondPower( x ); and y gets set to whatever is returned from the function.

Just to give you an idea of what Object Oriented programming is, heres a really simplified object. ( But if your still not grasping functions this is only going to be harder )

You know what it means when you type, "int x;" correct? Your creating an integer called x.

Lets say you typed "MyInt x;" Well, now your creating your own user-defined object ( which can work a lot like an integer if you want it to ) and your calling it x. Heres the code for MyInt:

class MyInt {
public:
MyInt() { i = 0 }; // This Is Called A Constructor
int i;
};

Your placing the integer "i" inside of the class MyInt. When you create a MyInt by typing "MyInt x;" the first thing that happens is the constructor ( which you can think of as a function ) is called. This sets the variable i equal to 0. Now whenever you type "MyInt x;" It will create an object with an integer called "i" inside of it, and it will call the constructor which initializes "i" to zero.

In order to get "i" out from inside you have to use the .(dot) operator. It's weird but it works well. So heres the code:

MyInt x; // Constructor Is Called Right Here and x.i is set to 0
cout << x.i << endl; // The output will be, 0 ( and not garbage like an integer would normally have since the constructor set it to 0 for you )

You created an object called MyInt which has a variable inside of it called "i". Then the object automatically set it's internal variable "i" to 0 and you printed it out.

There are ways to get around needing to use the dot to pull out the variables but it's a little advanced and it can still get messy--you just need to get the concept of object oriented programming first. Your creating your own "Objects" and letting them handle themselves.

Onto pointers! I'm not going to try to explain them fully in text since sometimes you just need to see a diagram. Just remember that all a pointer is, is a variable that POINTS to the location where another variable is. But heres a good reason why you'd need to use one:
Lets say your making a game called...for lack of a better name, DinoAttack. In DinoAttack you have giant dinosaurs that you shoot at with crosshairs moved by the mouse.
Lets say you have a 1 megabyte Image your loading from disk, which contains the picture of a T-Rex. ( yeah, its a big t-rex ).

Now you want 100 of these guys running around ( on and off the screen ) for you to shoot at. Thats a whopping 100 megabytes of ram alltogether! If you make it so that each t-rex has a pointer to the t-rex picture then the pointer hardly takes up any ram, but it tells you where that picture is stored in memory, and each time all 100 t-rexes are rendered(drawn to the screen), they all use the same picture in RAM. You only need to store ONE picture of the t-rex and you can have as many as you want on the screen without using an obscene amount of ram.

Pointers can be used for a lot of other things, but one of the big ones is saving ram.

If you have any questions or want some better examples or something send me an e-mail at InTheSackMan@yahoo.com

-------------------------------------------------------------
And to you idiots that came in here and posted useless crap: If you don't have something worth saying, DONT SAY IT. Nobody wants to hear your crap. The guy asked for help, not for a couple of noobs to come in and make fun of him.

[edited by - InTheSackMan on August 11, 2003 6:49:22 AM]

Nice essays!

##### Share on other sites
Actually, I think his totally right. Pointers don''t really serve a purpose in C++ except for increasing performance in a non algoritm based way. So the compiler should be able to care of it.

• 37
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631360
• Total Posts
2999556
×