Posted: Tue Mar 23, 2004 10:38 pm Post subject: (No subject)
personaly i do this
code:
using std::cout;
using std::endl;
but that's just me
Sponsor Sponsor
jonos
Posted: Tue Mar 23, 2004 10:44 pm Post subject: (No subject)
would i also have to do that for:
code:
using std::cin;
or does cout cover that?
alos, do i have to do that for all the structures like if statements and loops?
wtd
Posted: Tue Mar 23, 2004 10:46 pm Post subject: (No subject)
jonos wrote:
would i also have to do that for:
code:
using std::cin;
or does cout cover that?
No, cout and cin are each single variables (objects).
jonos wrote:
also, do i have to do that for all the structures like if statements and loops?
No, those are part of the C++ language itself. One of the first things you should do is make sure you understand where the language stops and the vocabulary (libraries) begin.
jonos
Posted: Tue Mar 23, 2004 10:50 pm Post subject: (No subject)
ive never thought of it that way, ive been doing c++ for probably less than a month pretty seriously, but i never thought of it that way... ive never seen any reference refering to libraries and the c++ language itself, and they don't offer c++ at my school, so maybe someone should do a tut on that
wtd
Posted: Tue Mar 23, 2004 11:13 pm Post subject: (No subject)
Well, types that are part of the language itself:
code:
int // usually 32-bit
char // usually 8-bit
short // usually 16-bit
long // usually 32-bit
long long // usually 64-bit
float // 32-bit floating point number
double // 64-bit floating point number
long double // sometimes available, 80-bit floating point
Of course, these types will also vary depending on what kind of machine you're programming for.
There's lots of syntax that's part of the C++ language itself.
code:
// For loop
for ( ... ; ... ; ... ) { ... }
// do while loop (for looping at least once)
do { ... } while ( ... );
// dereferencing a pointer
some_type value = *ptr;
// getting a pointer to a variable
some_type * ptr = &value;
And there's much much more, but my fingers are about to wear out.
Thuged_Out_G
Posted: Thu Mar 25, 2004 5:19 pm Post subject: (No subject)
if you wouldnt mind, could you please explain these few topics a little more, i dont quite understand them
for loop, switch, some block, label, namespace.
i know what a for loop is, i just didnt quite understand the syntax you showed for it . i have seen label before in turing, but never used it, or looked into to it to find out its use. namespace, all i know about that is "using namespace std;" lol
thanks
wtd
Posted: Thu Mar 25, 2004 6:04 pm Post subject: (No subject)
Well, in any loop you have:
Initialization (of variables).
Test
Update (variables)
The for loop conveniently combines these into one piece of syntax:
code:
for ( initilization ; test ; update ) { ... }
A switch is essentially a way to streamline long if/else structures. In C/C++/Java/PHP it only works with integers (including characters), though.
Essentially, a single variable or function call is input, and then multiple values are compared against it.
So instead of:
code:
int i = 42;
if (i == 42)
something();
else if (i == 54)
something_else();
else if (i == 12)
something_entirely_different();
else
why_bother();
I could write:
code:
int i = 42;
switch (i) {
case 42:
something();
break;
case 54:
something_else();
break;
case 12:
something_entirely_different();
break;
default:
why_bother();
}
The break; is necessary because of the way switch statements behave. Once something matches, it will continue evaluatng all of the code below it until it either hits the end of the switch or a break;. If I didn't have break after "case 42:", all four functions would be run, rather than the one I wanted.
This can come in handy though, for doing the same thing for multiple matches. Let's say I want to execute the something() function if i is 42 or i is 27.
code:
int i = 42;
switch (i) {
case 42: case 27:
something();
break;
case 54:
something_else();
break;
case 12:
something_entirely_different();
break;
default:
why_bother();
}
Blocks of code are handy because they introduce a new "scope" that can have its own set of variables. Let's say for instance that I have a variable named couner already declared, but I don't need to use it for a small section of code. I do, however, want to use it for a loop.
This is because the first counter we declared wasn't altered by the code inside the block. Not necessarily a good practice, but handy to know it's possible.
A label is a name given to a particular location in a program. For instance, an if statement might be written as:
code:
#include <iostream>
int main()
{
int i = 42;
if (i == 42) goto IF42;
goto NOT42;
IF42:
std::cout << "Ok" << std::endl;
goto END;
NOT42:
std::cout << "Doh!" << std::endl;
END:
return 0;
}
If you do this, however, there are any number of people who will han you up by your toenails. It's that bad an idea.
Namespaces, however, are not a bad idea. Let me take a page from another topic and suppose we want to write a Rock, Paper, Scissors game. There will be a number of functions involved. In PHP, which lacks namespaces, I started every function name with "rps_". But instead, how about I just goup them all in one "rps" namespace?
code:
namespace rps {
std::string get_user_input() {
// ... do something here
}
}
Now, to use this function, I an either use:
code:
int main()
{
std::string user_input = rps::get_user_input();
}
Or...
code:
int main()
{
using namespace rps;
std::string user_input = get_user_input();
}
Now of course that latter example would look a lot better if you had a lot more calls to functions in therps namespace.
Hope this has helped.
jonos
Posted: Thu Mar 25, 2004 8:35 pm Post subject: (No subject)
man, that was really helpful, you should write a book, or at least a pdf on all this stuff. thats awesome. thanks a lot you really clarified a lot of things, even though i didn't request it i used it so i think thug_g should request a lot more things