Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 C++ Architecture
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Mr. Gruntsworthy




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Nov 23, 2006 5:17 pm   Post subject: (No 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.
Mr. Gruntsworthy




PostPosted: Thu Nov 23, 2006 5:24 pm   Post subject: (No 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.
wtd




PostPosted: Thu Nov 23, 2006 5:33 pm   Post subject: (No 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.
Mr. Gruntsworthy




PostPosted: Thu Nov 23, 2006 5:47 pm   Post subject: (No 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
wtd




PostPosted: Thu Nov 23, 2006 5:53 pm   Post subject: (No 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.
Mr. Gruntsworthy




PostPosted: Thu Nov 23, 2006 6:12 pm   Post subject: (No 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.
Clayton




PostPosted: Thu Nov 23, 2006 6:14 pm   Post subject: (No 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;
}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Nov 23, 2006 6:16 pm   Post subject: (No 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.
Clayton




PostPosted: Thu Nov 23, 2006 6:20 pm   Post subject: (No 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;
}
Clayton




PostPosted: Thu Nov 23, 2006 6:23 pm   Post subject: (No 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.
Mr. Gruntsworthy




PostPosted: Thu Nov 23, 2006 6:42 pm   Post subject: (No 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.
Clayton




PostPosted: Thu Nov 23, 2006 6:46 pm   Post subject: (No subject)

Thats fine, now read wtd's second problem.

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




PostPosted: Thu Nov 23, 2006 7:09 pm   Post subject: (No 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]
wtd




PostPosted: Thu Nov 23, 2006 9:16 pm   Post subject: (No 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
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 26 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: