okie-dokie, good job
Compilers. I personally use Visual C++ 6.0, I've never used any other compiler for windows-based apps. Back at school we used the gnu compiler (I think), but only for DOS apps, and even then I did my homework on Visual C++. So that's more or less the only one I'm capable of talking about - not that it really matters I suppose.
I'm warning you and anyone else that the transition from DOS programming to windows programming is not easy at all. There are a lot of new things to get to grips with, such as messaging and window structure, which are essential to any windows program, which have to be learned from scratch. That doesn't mean it can't be done, of course it can! It's just a big jump.
So I'll start by covering "standard" c++, that is DOS-based. These techniques will of course be used in windows apps too.
The first, most important part of c++ to understand is the scope and the stack. A scope is delimited by curly braces { like this }. Any variable declared and allocated in a scope will be freed when that scope is exited. It doesn't matter whether the scope is a class, a function, a for loop, an if... or even an unnamed scope. Therefore this:
void SomeFunction()
{
int a = 1;
if (a==1)
{
int b=2;
}
else
{
b=4;
}
cout << b;
}
Will not work, because b is not defined when we try to access it.
Bear in mind that whenever you create a variable inside a scope, that variable will override any other of the same name from a different scope, until the scope is exited. Example:
int a = 4;
{ // Unnamed scope
int a = 6;
}
cout << a;
This will print 4... a is only 6 within the unnamed scope. Actually that sounds misleading - it's better to say that the letter "a" does not mean the same thing inside and outside that inner scope. In this case they are both ints, but this would be exactly the same:
int a = 4;
{ // Unnamed scope
float a = 3.1415927;
}
cout << a;
Ok, that handles scopes. A quick word on comments. There are 2 types of comment in c++ - single-line comments and free comments. Single-line comments are like the example above: you put a double-slash ("//") and everything on the rest of the line is a comment. Free comments are much more flexible. They start with "/*" (slash-star) and end with "*/" (star-slash). Everything in between these markers is a comment, even if it's on several lines. Here's a nasty example I remember from school:
int a = 0;
a += 1; /* Increment by one
a += 2; * Increment by two
a += 3; * Increment by three
a += 4; * Increment by four */
What's a worth at the end of this code? If you're still counting, then you're wrong - a is worth 1, because only the first increment is used, the rest are all comments.
Of course, with syntax colouring in most IDEs now this isn't a problem, but you should still be aware of it
A quick word on control structures. Control structures change the
flow of program execution. In c++, there's if, switch, for, do...while and while (
exceptions also obey this definition, but that's for another time).
"if" is simple. The syntax is : if (test-expression)
The CODE part can be an instruction or a scope. Remember than the test-expression must always be enclosed in parentheses.
You can also use "else" with if. else must logically be the next expression in the same scope as if, and it is used the same way. example:
if (a==3)
{
cout << "Something";
}
else
cout << "Something else";
same as
if (a==3)
cout << "Something";
else
{
cout << "Something else";
}
switch is if's big brother. It allows multiple values to be queried and branches differently for each. switch is a very efficient structure (it usually compiles to a jump table), and is far faster than a series of if/else structures.
example of switch:
switch (x)
{
case 1:
case 3:
case 5:
cout << "x is odd";
break;
case 2:
case 4:
case 6:
cout << "x is even";
break;
default:
cout << "Invalid value for x";
break;
}
A few things to remember : never forget the break statement. In a switch statement, wherever you jump to, flow continues until it is told to stop. You can exit with a break, a return, or other methods but if you don't tell it to stop, it won't.
The "default" branch, isn as you've probably guessed, where execution goes if no specific label for the value is there.
I should also say that the decision variable in switch should always be an integer (int, short, long... any integer). Pointers, variables, floats won't work (although simple chars will, because they really are integers anyway).
Another thing is variable declaration. Lexically, all the "case" fields (branches) are in the same scope, which means that they share the variable namespace. Therefore, this generates an error:
switch (x)
{
case 1:
int y = x * 4;
cout << y;
break;
case 2:
int y = 1+ x * 3;
cout << y;
break;
}
because y is being defined twice within the same scope (remember, I said scoping was important!). The solution is to make a new scope for each branch, like so:
switch (x)
{
case 1:
{
int y = x * 4;
cout << y;
break;
}
case 2:
{
int y = 1+ x * 3;
cout << y;
break;
}
}
Scoping rules will become even more important when you use objects and classes.
Phew, that's long enough for now... even though I haven't covered much. Next time, for/while, arrays and pointers.
Questions, class? (grin). I'm deliberately not presenting this as a lesson, rather as a summary of little bits I've had to learn the hard way. If I've skipped anything, if something is unclear, just say...