Game Programming and Development Tools

C++ – arissa_nightblade

arissa_nightblade

Member

Posts: 70
From:
Registered: 02-10-2007
Hey! I'm getting a programming test for a potential job interview and I need to brush up on my C++ skill before then. I understand the basic concept of things like libraries and such, but I just want to make sure I'm 100% in everything.
I've worked with 3D Game Studio a lot as of late, so I'm used to some of the Lite-C syntax which is a little different than C++.

Just some basic newbie questions:

For example: If I have a file called "maingame.h" and I include libraries inside this file such as: "#include player.h" I can use all the same variables and things I declared in the player.h file in maingame.h?

What if I make a file called gamestart.cpp and I use #include "maingame.h" inside it? does this mean I can use variables I declared from both maingame.h and player.h? or am I just limited to stuff used in maingame.h only?

I just want to know if C++ works the same as 3DGS's C-Scripting concept-wise. I realize the syntax will be different, but if the concept is still there, then I guess I won't have too much problem. The test is supposed to be in C++, so that's why I was worried.

There are some libraries I'm not sure about, but I know they are needed. I already know about iostream.h, but I've seen stdio.h and stdlib.h used in a lot of programs, but I'm not sure why it's needed.

Does anyone know of any sample C++ games that I can study from and see how things are done? Thanks in advance @_@

Sorry if this post sounds confusing...

------------------

arissa_nightblade

Member

Posts: 70
From:
Registered: 02-10-2007
Also, what is the difference between putting something in a .h file than putting something in a .cpp file? Suppose I have a file called player.h and one called player.cpp. If I was making a game, I would put the attributes in things for a player character in the player.h file, right? so what would the player.cpp file be used for?

or is player.h nothing more than just -defining- the attributes in there (like putting stuff like "float player_size," "char player_name," etc...)
But I still don't understand what you would need an extra c++ file for... I thought everything gets put together in 1 .CPP file and all you do is define all the libraries??

Yes, I'm stressing over this test >.>

------------------

samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
I can't explain it all right now, but it would do you well to understand the actual c compilation process. The C Preprocessor is what deals with .h files and any line that starts with #

http://en.wikipedia.org/wiki/C_preprocessor

------------------
Sam Washburn

Check out my CCN SpeedGame 2 Blog

Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
OK, here's a simple break-down:

.h file: a "header" file, should only contain things like function [u]declarations[/u] (i.e. the function "prototypes", i.e. the explanations of what functions are available), common #defines, etc. Don't put definitions (implementations) in an .h file, so that multiple .cpp files can #include the same .h file without ending up with multiple copies of function bodies and variables (which modern compilers will complain about anyway). I.e. this is why you don't #include any .cpp files (well, you can if you know what you're doing, but DON'T because you know what you're doing. ).

.cpp file: an "implementation" file, contains the function [u]definitions[/u] (i.e. implementations, i.e. function bodies) of the functions declared in the corresponding .h file, global variables, etc. You can also put declarations here as well, but other .cpp files will be unable to see them.

.lib file: a "library" file, is a compiled/linked binary file made up of one or more third-party .cpp files (containing their function bodies, etc.)

For example:

TheirHeader.h


int TheirFunction1(); // declaration only, body is in TheirLib.lib
int TheirFunction2(); // declaration only, body is in TheirLib.lib

MyApp.h


#define GOOD 0
#define BAD -1
int MyFunction1(); // declaration only, body is in MyApp.cpp
int MyFunction2(); // declaration only, body is in MyApp.cpp

MyApp.cpp


#include "TheirHeader.h" // so my app *knows* about TheirFunction1()
#include "MyApp.h"

int MyFunction1()
{
return GOOD;
}

int MyFunction2()
{
return BAD;
}

int main()
{
MyFunction1();
TheirFuction1(); // my app *calls* TheirFunction1 which lives in TheirLib.lib
}

When you compile/link your program, you'll have to make sure that their .lib file (TheirLib.lib or whatever) is linked in, so that when you call "TheirFunction1()" it knows what to do. Similarly, if you later make a "MyApp2.cpp" file, you can simply #include "MyApp.h" to have GOOD and BAD defined, and know about and thus call MyFunction1 and MyFunction2.

Let me know if that helps, and if you have other questions.


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

"OOP programmers have a lot of class"

Check out this webhost! Fantastic prices, features and support!

[This message has been edited by BrianT (edited October 17, 2007).]

Briant

Member

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

For example: If I have a file called "maingame.h" and I include libraries inside this file such as: "#include player.h" I can use all the same variables and things I declared in the player.h file in maingame.h?

What if I make a file called gamestart.cpp and I use #include "maingame.h" inside it? does this mean I can use variables I declared from both maingame.h and player.h?


Yes, include files (.h files) are nestable like that. You can think of "#include fred.h" as telling the precompiler to "take the contents of the whole fred.h and pretend it's all right here in this .cpp file instead". So if fred.h includes wilma.h, and wilma.h includes betty.h, you get betty and wilma by including fred.

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

"OOP programmers have a lot of class"

Check out this webhost! Fantastic prices, features and support!

[This message has been edited by BrianT (edited October 17, 2007).]

arissa_nightblade

Member

Posts: 70
From:
Registered: 02-10-2007
Thanks so much! It sounds a lot like the C-script, then! ^_^
I just have one question in regards to one of your examples.

In the Myapp.h and Myapp.cpp, why do you define int MyFunction1() twice?? I thought once you define a function you don't have to define it again as long as you include the .h file that has it in there. Wouldn't you declare Myfunction in the MyApp.cpp one instead? I'm just wondering why you do that.

I thought prototype functions were functions that had the & symbol in them and those would be the ones oyu would have to predefine? (ie: "myFunction(&test)" )

[This message has been edited by arissa_nightblade (edited October 17, 2007).]

Briant

Member

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

In the Myapp.h and Myapp.cpp, why do you define int MyFunction1() twice?? I thought once you define a function you don't have to define it again as long as you include the .h file that has it in there. Wouldn't you declare Myfunction in the MyApp.cpp one instead? I'm just wondering why you do that.

OK, I hope I can clarify. The terminology catches a lot of people: I declared MyFunction1 in the .h file, and I defined it in the .cpp file. A declaration is used just so the compiler knows argument variable types and return types so it can flag errors on passing bad data etc., while a definition is the "body" of the function itself. A function should only have one body (one definition), but it has to be "declared" in every .cpp file that calls that function (which is why the declaration goes in the .h file, so that any .cpp file that needs to call that function can #include the .h file to see the declaration).

Clear as mud?

quote:

I thought prototype functions were functions that had the & symbol in them and those would be the ones oyu would have to predefine? (ie: "myFunction(&test)" )

No, that's something different. The & symbol means "the address of" (i.e. a pointer to that variable) when calling a function, or "a reference to" in a function prototype. Two different things that confuse a lot of people (and I can clarify if you want), but it's basically unrelated to the .h/.cpp question.

Just FYI, here is an .h and .cpp with functions that use the & symbol:

blah.h


int Blah1(int *pInt); // function receives a pointer to an integer
int Blah2(int &rInt); // function receives an integer reference
int Blah3(int nInt); // function receives an integer

blah.cpp


#include "blah.h"

int main()
{
int b1 = 7;
int b2 = 9;
int b3 = 11;

Blah1(&b1); // calling Blah1, passing the address of b1
Blah2(b2); // calling Blah2, function works on a reference to b2
Blah3(b3); // calling Blah3, function works on a *copy* of b3
return 0;
}

int Blah1(int *pInt)
{
printf("Your Blah1 value is %d\n", *pInt); // * here means "the value pointed to"
*pInt++; // b1 in main() is incremented
return 0;
}

int Blah2(int &rInt)
{
printf("Your Blah2 value is %d\n", rInt); // no & here!
rInt++; // the b2 from main() is incremented
return 0;
}

int Blah3(int nInt)
{
printf("Your Blah3 value is %d\n", nInt);
nInt++; // only the local copy is incremented! b3 in main is NOT incremented
return 0;
}

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

"OOP programmers have a lot of class"

Check out this webhost! Fantastic prices, features and support!

[This message has been edited by BrianT (edited October 17, 2007).]

arissa_nightblade

Member

Posts: 70
From:
Registered: 02-10-2007
Ok I think I get it.. So any function you use, you have to write them out twice? Write out the function first in the .h file then write it again in the CPP file but include the body of the function also?

------------------

Briant

Member

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

Ok I think I get it.. So any function you use, you have to write them out twice? Write out the function first in the .h file then write it again in the CPP file but include the body of the function also?

You got it. Note the only reason for putting it in the .h file is so that multiple .cpp files can know about it. If you only use a function in one .cpp file, you don't have to put it in a .h file (but then you either have A. put its body *above* where it's called from, or B. put its declaration *above* where it's called from and its body *below* where it's called from.

Basically, the whole thing boils down to this: the compiler is stupid and you have to let it know what parameters a function receives *above* where you call that function. The compiler looks at files top-to-bottom, not all-at-once. If it comes to a call to a function it doesn't know about yet, you get a compile error.

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

"OOP programmers have a lot of class"

Check out this webhost! Fantastic prices, features and support!

[This message has been edited by BrianT (edited October 17, 2007).]

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
quote:
Originally posted by arissa_nightblade:
Hey! I'm getting a programming test for a potential job interview and I need to brush up on my C++ skill before then. I understand the basic concept of things like libraries and such, but I just want to make sure I'm 100% in everything.
I've worked with 3D Game Studio a lot as of late, so I'm used to some of the Lite-C syntax which is a little different than C++.

Just some basic newbie questions:

For example: If I have a file called "maingame.h" and I include libraries inside this file such as: "#include player.h" I can use all the same variables and things I declared in the player.h file in maingame.h?

What if I make a file called gamestart.cpp and I use #include "maingame.h" inside it? does this mean I can use variables I declared from both maingame.h and player.h? or am I just limited to stuff used in maingame.h only?

I just want to know if C++ works the same as 3DGS's C-Scripting concept-wise. I realize the syntax will be different, but if the concept is still there, then I guess I won't have too much problem. The test is supposed to be in C++, so that's why I was worried.

There are some libraries I'm not sure about, but I know they are needed. I already know about iostream.h, but I've seen stdio.h and stdlib.h used in a lot of programs, but I'm not sure why it's needed.

Does anyone know of any sample C++ games that I can study from and see how things are done? Thanks in advance @_@


Hope you (or others) don't take offense to this... but...

Potential job as a programmer... I do hope you have a good year or two before this test.

------------------
jonwarner.net

arissa_nightblade

Member

Posts: 70
From:
Registered: 02-10-2007
quote:
Originally posted by dartsman:
Hope you (or others) don't take offense to this... but...

Potential job as a programmer... I do hope you have a good year or two before this test.


I've never had an actual job as a programmer. The only programming I've really done over the years was hobby/in my spare time and the classes from college, but they only taught the very basics of C++ which I already knew most of it from high school. I am sure the job will be asking advanced questions and I will have to do stuff I've never done before, so that's why I want to brush up on this stuff and make sure I at least have the concepts done. I've been exploring other languages besides C++.

------------------

arissa_nightblade

Member

Posts: 70
From:
Registered: 02-10-2007
Random question... I've looked around gamedev.net and noticed a lot of C++ programs use either Directx or SDL in addition to C++.... are those the libraries you need in order to display graphics on the screen? Which one is better?

------------------

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
quote:
Originally posted by arissa_nightblade:
Random question... I've looked around gamedev.net and noticed a lot of C++ programs use either Directx or SDL in addition to C++.... are those the libraries you need in order to display graphics on the screen? Which one is better?

O_o which company are you applying for??? lol

------------------
jonwarner.net

arissa_nightblade

Member

Posts: 70
From:
Registered: 02-10-2007
quote:
Originally posted by dartsman:
O_o which company are you applying for??? lol


A company called Big Huge Games.
The random question was for my own curiosity, though. I really don't know what I'm going to be asked.

------------------

MastaLlama

Member

Posts: 671
From: Houston, TX USA
Registered: 08-10-2005
Is that the same Big Huge Games that put out the Rise of Nations series?
arissa_nightblade

Member

Posts: 70
From:
Registered: 02-10-2007
Yes
So now you know why I'm stressing over this test x_x

------------------

SSquared

Member

Posts: 654
From: Pacific Northwest
Registered: 03-22-2005
That's cool. Do you need C or C++ help? Most of your questions and the answers have been in regards to C. You may want to look here: http://www.parashift.com/c++-faq-lite/
For some reason, the site has been down. Try doing a search for "C++ FAQ Lite". It's a nice look at various questions and how-to's in C++. And looking at GameDev was a smart idea too.

DirectX and SDL are just two of the MANY possibilities out there. DirectX is fairly low level while SDL is on top of DirectX/OpenGL. The best advice is to find out what engine the company actually uses. Is it something comercially available or built in house? If publically available, then do research on the product and understand its components. If they use something in-house, then dig a little deeper and find out if there is ANYTHING available online regarding their engine.

I have a set of various C++/OO questions I ask during interviews based on skill level and understanding. Here are a few:

1) What is the difference between a class and an instance?
2) Can you tell me the three foundations of OO?
3) Using class diagrams, psuedo-code, whatever, give me an example of polymorphism up on the board.
4) What is the difference between a struct and a class?
5) I have a class defined with a static member variable 'staticMemberA' and two instances A and B. If I change A's staticMemberA to be 42, what value will staticMemberA be in B?
6) Explain the keyword 'virtual'.
7) Why would you want to make a destructor virtual?

arissa_nightblade

Member

Posts: 70
From:
Registered: 02-10-2007
Thank you for the help! Well to my understanding, I think the test will be in C++, so I've been trying to get familiar with that language again. I spent about 3-4 months working on a game project in C, so all I know is C right now. I used C++ a lot a while ago, but I've forgotten some of the stuff and need a brain refresher

Thanks for the explanation on the SDL/Directx thing. That question was just for my own curiosity. I don't know if the company uses that or not, but I will inquire about it more if/when I manage to pass this test. I think for now, however, it's just straight c++ coding without the use of anything too complex like including graphics -- though I could be wrong >.>

Thanks again for your help and advice! ^_^ I will study those questions and continue my research. This has helped me a lot and a lot of things are starting to come back to me again.

[This message has been edited by arissa_nightblade (edited October 18, 2007).]

MastaLlama

Member

Posts: 671
From: Houston, TX USA
Registered: 08-10-2005
Big Huge Games uses an in-house engine called Big Huge Engine