C++ Multi questions

Started by
6 comments, last by lord_balron 16 years, 12 months ago
Ok I have a few questions I've been working though the learn C++ in 21 days but I have a few basic questions that could us answering. BTW I am on day nine of the book 1)How do I go about making a GUI for my program? 2) when declaring a fucntion at the top of the code what does the VOID mean? 3)when passing a value by refrence I would say call swap(x,y) and does it then goto void swap (int &rx, int &ry). Yes im assuming. Ok, so does it take x, and define it as &Rx for that function? sorry for the crappy wording in this question. 4)Once I finish this book were will I be at as far as game design, what will be my next step "Do I need to buy a "ENGINE" I have no idea what that is either. Thanks, Glyvin101
Advertisement
1) Depends on your OS, for windows you could take a look at msdn.microsoft.com
2) nothing :) (void means that there is no parameter or return value)
3) huh ?
4) If c++ is your first language then you're still quite far off.
You don't need to buy an engine, especially not for your first games (for simple games its easier to combine the game and engine than to use a large complex engine).

There are free engines avaliable but i would strongly recomend that you atleast make some simple games such as tetris, nibbles/snake, pacman, asteroids, etc before going for anything complex enough to require a proper engine.

You'll know when you're ready.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Quote:Original post by glyvin
1)How do I go about making a GUI for my program?


I recommend you use a GUI library, such as wxWidgets. You could also learn win32 programming.

Quote:Original post by glyvin
2) when declaring a fucntion at the top of the code what does the VOID mean?

In that context void means nothing, ie the function does not return a value.

Quote:Original post by glyvin
3)when passing a value by refrence I would say call swap(x,y) and does it then goto void swap (int &rx, int &ry). Yes im assuming. Ok, so does it take x, and define it as &Rx for that function? sorry for the crappy wording in this question.

For a function void swap (int &rx, int &ry), the variable names to use in the scope of the function are rx and ry. The & is not included in the variable name, you could think of it as a variable rx with type int&.

Quote:Original post by glyvin
4)Once I finish this book were will I be at as far as game design, what will be my next step "Do I need to buy a "ENGINE" I have no idea what that is either.


Do you actually mean game programming? Since game design isn't directly related to C++. I would say the next step is to learn more, this book will only cover some very basic stuff. You could for example start by writing a few simple applications.
Best regards, Omid
#include <iostream.h>
#include <stdlib.h>

void swap(int &x,int &y);

int main()
{
int x=5, y=10;
int a;

cout << "Main. Before swap, X: " << x << " Y: " << y << "\n";
swap(x,y);
cout << "Main. After swap, X: " << x << " Y: " << y << "\n";
cin >> a;
return 0;
}

void swap (int &rx, int &ry)
{
int temp;

cout << "Swap. Before swap, rx: " << rx << " ry: " << ry << "\n";

temp = rx;
rx = ry;
ry = temp;

cout << "Swap. After swap, rx: " << rx << " ry: " << ry << "\n";

}


this is the code for which I am refering like how void is used I don't understand what it means and for number 3 you see how x and y are turned into rx and ry.

Also for my engine question I don't see how you can make a tetris in straight C++ since its all done in the dos prompt.

Glyvin101
Quote:Original post by glyvin
1)How do I go about making a GUI for my program?

This depends on how you want to approach things. A simpler way may be to use your OS's built in GUI support. For Windows, this would mean Win32/MFC/WinForms. Alternatively, you could look into a rendering API such as OpenGL or DirectX.

Quote:2) when declaring a fucntion at the top of the code what does the VOID mean?

There are two places you could be using void: 1) At the beginning of the function declaration, which means the function has no return value, and 2) In the parameter list of the function, which would mean the function doesn't take any arguments when it is called.

Quote:3)when passing a value by refrence I would say call swap(x,y) and does it then goto void swap (int &rx, int &ry). Yes im assuming. Ok, so does it take x, and define it as &Rx for that function? sorry for the crappy wording in this question.

When passing by reference vs. value, how something is passed is based on the function declaration. If, for example, the function is defined as void swap(int& rx, int& ry), then you will be passing by reference. If it's void swap(int rx, int ry), however, you'll be passing by value. So... yes? [smile]

Quote:4)Once I finish this book were will I be at as far as game design, what will be my next step "Do I need to buy a "ENGINE" I have no idea what that is either.

Once you finish this book, make sure you have NO questions. If you do, read the book again (or at least the relevant parts), decide what, exactly, your question is, and ask it! Next, use what you've learned as much as you can. Make program after program after program. If you run out of ideas, small games are always good -- guess the number, tic-tac-toe, pong, tetris, etc. Whatever you do, when you're happy with what you've done, post it here to get some feedback. This is a very helpful step. The experts here can point out what you did well, but more importantly, what you didn't. There will inevitably be areas of your code that could have been approached differently, and its helpful to know what this is when starting to plan for your next project.

No, don't buy an engine. Not until you know why you need it, anyway. [smile]
-jouley

[Edit: I didn't use to think I was a slow typer.]
Quote:Original post by glyvin
this is the code for which I am refering like how void is used I don't understand what it means and for number 3 you see how x and y are turned into rx and ry.


You don't seem to have understood the basics of functions yet. When you call a function you pass it values for it's parameters. For instance consider the function
void show_int(int a){  std::cout << a << std::endl;}

This function has one parameter which I've chosen to call a. In the scope of this function, that is any code which goes between the brackets { } the variable a will be accessible. Now the value for this variable is given by the code which calls this function. For instance let's say I call this function from main
int main(int argc, char *argv[]){  show_int(5);}

Now the code in the show_int will be run with the variable a given the value 5.
Or I could do:
int main(int argc, char *argv[]){  int x = 10;  show_int(x);}

Now when the code is called a will have the value which x had when it was passed to the function, so a = 10. But note that a IS NOT THE SAME AS x, they just have the same value. If I change a, x will not change.
That's why you need references, since sometimes you want the function to change the value of the variable passed in, in this case x.
void edit_var(int& a){  a = 10;}


Now I'm telling it to instead of passing the value of a variable to the function to pass a reference to the actual variable, note that the name of the variable will be a inside the function, regardless of what the name of the variable which is passed to the function is! Now if I where to call this function:
int main(int argc, char *argv[]){  int x = 5;  edit_var(x);}

x is first assigned a value of 5, but is then passed by reference to the function edit_var, which means that after the function x will now equal 10, not 5.
As for void, you see functions have return values. For example, consider this function which adds two integers and returns the result.
int add(int a, int b){  return a + b;}


You see the return type of the function is int, as in integer, which means that it returns an integer value, in this case the result of applying the + operator on the two integers a and b. Thus I could call this function from a another function, for example main like this

int result = add(10, 5);


result will now be equal to 15.

Consider this other function
string my_name(){  return "John";}


The return type here is a string, which means that this function will return a string. The implementation of the function suggests that the returned string will one containing the word "John". I could for example do

std::string my_name(){  return "John";}int main(int argc, char *argv[]){  std::cout << my_name();}

Which would print out "John" to the console.

Now if a function's return type is void, this means that the function will not return a value. For example:
void print_number(int a){  std::cout << a << std::endl;}


Calling this function will only execute it's content, but the function will not return a value, ie I can't do my_variable = print_number; since print_number will not return a value.

Quote:Original post by glyvin
Also for my engine question I don't see how you can make a tetris in straight C++ since its all done in the dos prompt.

Well, of course you don't since you don't know C++ yet [smile] In a few months or maybe a few years you will.

Best regards, Omid
Quote:Original post by glyvin
Ok I have a few questions I've been working though the learn C++ in 21 days but I have a few basic questions that could us answering. BTW I am on day nine of the book

1)How do I go about making a GUI for my program?


Patience. One step at a time.

Quote:
2) when declaring a fucntion at the top of the code what does the VOID mean?


It's 'void' in lowercase, unless otherwise defined. In the return-type position, it means "actually nothing is returned at all". You really, really shouldn't have been able to get this far into a beginner's C++ book without understanding this :/

Quote:
3)when passing a value by refrence I would say call swap(x,y) and does it then goto void swap (int &rx, int &ry). Yes im assuming. Ok, so does it take x, and define it as &Rx for that function? sorry for the crappy wording in this question.


Suppose our function is defined:

void swap(int &x, int &y) {  int z = x;  y = z;  x = y;}


When you write 'swap(a, b)' as an expression, this makes a call to the swap function. The variable 'a', in the current context, gets passed by reference to the swap function, and has the name 'x' within the function. Similarly, 'b' becomes 'y'.

When you pass things by value, a copy is made. That would mean that the swap function would have its own variable 'x' initialized with a copy of the value in 'a' in the caller, and similarly for 'y' and 'b'. The function would fiddle around with those values, and at the end of the function, they would simply disappear, just like z.

When you pass things by reference, the parameters become *aliases* for the caller's variables. That is, 'x' IS the caller's 'a', and 'y' IS the caller's 'b', for this specific call to the function. The function will make its own local 'z', use it to swap around 'x' and 'y' - therefore swapping around the caller's 'a' and 'b', because those are other names for the same thing - and then 'z' disappears. The caller then continues on, with 'a' and 'b' now being swapped relative to what they were before.

You must be able to make a clear conceptual distinction between things being identical (the "same thing" in the sense that two names refer to one object) and equal (the "same thing" in the sense that two objects are *currently* not distinguishable from one another).

Quote:
4)Once I finish this book were will I be at as far as game design, what will be my next step "Do I need to buy a "ENGINE" I have no idea what that is either.


There are many possible "next steps". Programming is best seen as an art or craft, and like any other art or craft, you should expect lots of opportunity for exploration, and a very long time before you've really figured it out.
You're questions are similar to mine in the beginning. It will all unfold in the end!

Sorry for bumpin'

This topic is closed to new replies.

Advertisement