
-----------------------------------
Mr. Gruntsworthy
Thu Nov 23, 2006 5:02 pm

C++ Architecture
-----------------------------------
I've recently begun to learn C++, in my preparation for my College course at International Academy of Design and Technology, for my career in Video Game Design. 

C++ is an interesting language, and has a wide range of things it can do; but the one thing that i can't seem to find an explanation of (or in english anways, if you get what i mean) how the overall architecture of C++ works. 

Like, for example, in Turing it executes on line before another, so its done in order.

But with C++, I read that it's modular, which means modules are self contained. So does that mean you can have your normal 'int main()', then have say 'void Game_Init' as a seperate module? 

for instance:

int main()
{
the statements of main()
}

and then, completely seperate, have 

void Game_Init
{
statements of Game_Init
}
would that have an error, even if Game_Init isn't called upon?

also, how do you call upon a function from within main()? 

I've only been learning this language for a week, and my only experience is Turing during Grade 9 and HTML during 9/10.

Any help would be greatly appreciated. Thanks.

-----------------------------------
wtd
Thu Nov 23, 2006 5:17 pm


-----------------------------------
Executable code in C++ is structured into functions.  You may have any number of functions.

The "main" function, thoug is very special.  It serves as the "entry point" for the program.  In other words, when your program is run, execution starts at the beginning of the main function.

I suspect that your education in Turing has not left you with a string understanding of scope.  This is a concept with which you will want to be very familiar.

-----------------------------------
Mr. Gruntsworthy
Thu Nov 23, 2006 5:24 pm


-----------------------------------
well, my idiot comptuer engineering teacher said to me, in quote, "You dont have to understand what you're programming. Why re-write something when you can just find something thats already been written and use it for your own needs (he didnt mean piracy, but sample programs and stuff for certain functions)?"

I strongly disagree with him; you've only truly learned a language when you've learned how it works. Like in learning French, you dont just learn the translation of the words, you learn where the words, prefixes, and so on go, and what the proper suffix is for an object. 

You see what i mean? I want to learn HOW C++ works, not just why it works, so that i can actually begin to write my own things rather than simply modifying sample programs.

-----------------------------------
wtd
Thu Nov 23, 2006 5:33 pm


-----------------------------------
What he means is to be able to take existing pieces of code, and use them in your own program, without necessarily having to know how they work.  A well-designed library can be used this way.  All you need to know is the interface, and you don't need to understand the implementation.

Consider for instance the Standard Template Library.  I don't really know how the vector or string classes are implemented, and yet I can write code with them.

-----------------------------------
Mr. Gruntsworthy
Thu Nov 23, 2006 5:47 pm


-----------------------------------
i have a 1120 paged C++ primer that im working through, as well as several books on game design. While ive learned a little on the architecture, i want to learn how the modules interact with eachother. Like i said, i dont just want to learn the code, i want to learn how to put a complete multifunction program together. 

So, if you could explain a little, or at least point me in the direction of someone who can? thanks. Oh, and take a look at my site: www.freewebs.com/mrgruntsworthy

-----------------------------------
wtd
Thu Nov 23, 2006 5:53 pm


-----------------------------------
Writing functions is easy.  You should get used to doing so.

For instance, write a function for me named "foo" that takes an integer argument, and returns that argument, plus three, multiplied by forty-seven.

-----------------------------------
Mr. Gruntsworthy
Thu Nov 23, 2006 6:12 pm


-----------------------------------
alright, ill give it a shot; here's what i would write; minus the main() function:

#indlude 
#include 
using namespace std;

void foo(int)
{
return foo(+3 * 47)
}

I have a strong feeling i did that wrong; please show me how it should look.

-----------------------------------
Clayton
Thu Nov 23, 2006 6:14 pm


-----------------------------------
please excuse me while I brush up on some C++ stuff (haven't done anything in it for about a month)


#include 

int foo(int num)
{
    return (int + 3) * 47;
}

int main()
{
    std::cout < foo(7);
    return 0;
}


-----------------------------------
wtd
Thu Nov 23, 2006 6:16 pm


-----------------------------------
int foo(int x) 
{
   return (x + 3) * 47;
}

Next: write a function which takes two integers.  It should find the product of the first argument squared, and the second argument; and then add one to that product.

-----------------------------------
Clayton
Thu Nov 23, 2006 6:20 pm


-----------------------------------
you can't really have foo return anything, as it is of the void type. To have it return something, you have to have it of some sort of returnable type, say, an int in this case. So we have to have our function "int foo" take in an integer parameter, and return that parameter + 3 multiplied by 47.


//declare out function foo with the type int
int foo(int num)
{
    //return our parameter num plus 3 multiplied by 47
    return (num + 3) * 47;
}


-----------------------------------
Clayton
Thu Nov 23, 2006 6:23 pm


-----------------------------------
Also note that the following code in this case provides no use:


#indlude 
#include 
using namespace std; 


nowhere are you using a function from the std namespace, nor are you using anything from the iostream class or the cmath class, you only need to include these files when you are using them, they are not a necessity for every program.

-----------------------------------
Mr. Gruntsworthy
Thu Nov 23, 2006 6:42 pm


-----------------------------------
okay, i understand that part. Remember, ive only been learning for about a week. I understand how to return a value to main; but im still struggling to understand C++'s architecture.

-----------------------------------
Clayton
Thu Nov 23, 2006 6:46 pm


-----------------------------------
Thats fine, now read wtd's second problem.

The reason this forum exists is to help people to learn to program :D

-----------------------------------
Mr. Gruntsworthy
Thu Nov 23, 2006 7:09 pm


-----------------------------------
hm... okay, let me try my hand at this.

#include 
using namespace std;

int foo (int x, int y)
{
return ((x * x) * y) + 1
}

//outputs the final result
int main()
{
cout 