Computer Science Canada

C++ Architecture

Author:  Mr. Gruntsworthy [ Thu Nov 23, 2006 5:02 pm ]
Post subject:  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.

Author:  wtd [ Thu Nov 23, 2006 5:17 pm ]
Post subject: 

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.

Author:  Mr. Gruntsworthy [ Thu Nov 23, 2006 5:24 pm ]
Post subject: 

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.

Author:  wtd [ Thu Nov 23, 2006 5:33 pm ]
Post subject: 

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.

Author:  Mr. Gruntsworthy [ Thu Nov 23, 2006 5:47 pm ]
Post subject: 

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

Author:  wtd [ Thu Nov 23, 2006 5:53 pm ]
Post subject: 

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.

Author:  Mr. Gruntsworthy [ Thu Nov 23, 2006 6:12 pm ]
Post subject: 

alright, ill give it a shot; here's what i would write; minus the main() function:

#indlude <cmath>
#include <iostream>
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.

Author:  Clayton [ Thu Nov 23, 2006 6:14 pm ]
Post subject: 

please excuse me while I brush up on some C++ stuff (haven't done anything in it for about a month)

c++:

#include <iostream>

int foo(int num)
{
    return (int + 3) * 47;
}

int main()
{
    std::cout < foo(7);
    return 0;
}

Author:  wtd [ Thu Nov 23, 2006 6:16 pm ]
Post subject: 

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

Author:  Clayton [ Thu Nov 23, 2006 6:20 pm ]
Post subject: 

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.

c++:

//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;
}

Author:  Clayton [ Thu Nov 23, 2006 6:23 pm ]
Post subject: 

Also note that the following code in this case provides no use:

c++:

#indlude <cmath>
#include <iostream>
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.

Author:  Mr. Gruntsworthy [ Thu Nov 23, 2006 6:42 pm ]
Post subject: 

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.

Author:  Clayton [ Thu Nov 23, 2006 6:46 pm ]
Post subject: 

Thats fine, now read wtd's second problem.

The reason this forum exists is to help people to learn to program Very Happy

Author:  Mr. Gruntsworthy [ Thu Nov 23, 2006 7:09 pm ]
Post subject: 

hm... okay, let me try my hand at this.
Quote:

#include <iostream>
using namespace std;

int foo (int x, int y)
{
return ((x * x) * y) + 1
}

//outputs the final result
int main()
{
cout << "The final result is " << foo <<endl;
return 0;
}

based on what the previous problem incurred, im guessing that all i had to do was modify it to fit the question at hand.[/quote]

Author:  wtd [ Thu Nov 23, 2006 9:16 pm ]
Post subject: 

You came close. The only thing you're missing is that you haven't supplied any arguments to foo when calling it.

code:
int main()
{
   cout << "The final result is " << foo(2, 3) <<endl;
   return 0;
}


Please indent the content of functions for readability.

Next: Rewrite foo to accomplish the same task, but as an intermediate step, store the square of the first argument in a locally scoped argument called "bar". Smile

Author:  Clayton [ Thu Nov 23, 2006 9:19 pm ]
Post subject: 

wtd wrote:

store the square of the first argument in a locally scoped argument called "bar".


I'm sure I know what you meant by this, but do you mean create a local variable to the function foo? (I'm pretty sure thats what you meant, just making sure and clarifying it for Mr.Gruntsworthy just in case Razz)

Author:  wtd [ Thu Nov 23, 2006 9:37 pm ]
Post subject: 

Freakman wrote:
wtd wrote:

store the square of the first argument in a locally scoped argument called "bar".


I'm sure I know what you meant by this, but do you mean create a local variable to the function foo? (I'm pretty sure thats what you meant, just making sure and clarifying it for Mr.Gruntsworthy just in case Razz)


Yes.

Author:  Clayton [ Thu Nov 23, 2006 10:00 pm ]
Post subject: 

Any specific reason we are doing that then? Is this going to be something to be built upon eventually?

Author:  wtd [ Fri Nov 24, 2006 12:01 am ]
Post subject: 

Scope is a very important concept.

Author:  Mr. Gruntsworthy [ Fri Nov 24, 2006 8:13 am ]
Post subject: 

please explain a little further; it's morning and my brain hasn't booted up yet, lol.....

Author:  wtd [ Fri Nov 24, 2006 6:48 pm ]
Post subject: 

You have your function:

code:
int foo(int x, int y)
{
   return ((x * x) * y) + 1
}


I would like you to rewrite this such that the result of:

code:
x * x


is stored in a variable called "bar". This variable must be scoped locally to the function "foo". That is, it should not exist outside of the "foo" function.

Author:  Mr. Gruntsworthy [ Fri Nov 24, 2006 10:33 pm ]
Post subject: 

damnit i hate it when people use such high levels of tech talk and can't use simple english. My brain is on 'safe mode' and has been for the last week.

Author:  wtd [ Sat Nov 25, 2006 12:11 am ]
Post subject: 

Let me demonstrate.

code:
void a()
{
   some_type b = some_value;

   do_something_to(b);
}


Here the variable b is scoped as local to the function a.

Author:  Mr. Gruntsworthy [ Sat Nov 25, 2006 10:54 am ]
Post subject: 

I think i see....

I have alot to learn before i start attending the International Academy of Design and Tecnology in July.

Author:  wtd [ Sat Nov 25, 2006 5:26 pm ]
Post subject: 

Mr. Gruntsworthy wrote:
I think i see....

I have alot to learn before i start attending the International Academy of Design and Tecnology in July.


Yes, but you have a fair amount of time. Think of it as a challenge, and you will do well.

What you need to do is write code. Write a lot of code. That doesn't mean it has to be huge programs, but rather lots of small projects.

Submit those small programs for frequent critique. Such critiques can be pretty critical, but that's the point.

Author:  Mr. Gruntsworthy [ Sun Nov 26, 2006 1:18 pm ]
Post subject: 

well, if i find time to submit anything i've written, ill do so.


: