Ok, I attempted to begin these sorts of lessons awhile ago (though they were to cover C#, not C++) but my first lesson ended up being huge and I was bogged down by the size. This go around I'm going to keep each lesson shorter and simpler (or at least more focused).
I'm going to begin with a brief overview of what programs are. Programs, in the most basic sense, are what makes the computer actually do some task. In the lowest level this means the processor is processing information. In the lowest level of "programming" programs are only code that is passed to the processor to execute an instruction (via a series of 1's and 0's). The processor handles only one instruction at a time (unless you have a PC with more than one core, such as new dual or quad core processors). The types of instructions the processor handles are varied, but almost all of them deal with manipulating information of some sort or sending or retrieving this information to or from other hardware, in particular the system's memory which is most often where program data is kept.
I'm not trying to go over anyone's head by delving into speaking about the processor, but it's important to understand that C++ is a low level language. Each instruction, in some way, gets interpreted into machine code. When you're working with C++ you are in reality working with low level programming.
So, on to variables. I mentioned that the processor mainly deals with manipulating information, and variables are how a program keeps track of information to manipulate. I'll give some brief examples of variables here:
int a;
unsigned int b = 0;
Don't fret about not understanding it if you've never seen C++ code before. In a nutshell, the ; symbol marks the end of a line of code. In other words, every line of code in C++ ends with a ; symbol. It's merely a way for the compiler (which handles turning C++ into machine code (1's and 0's) that the processor can understand) to know when each line of code is complete.
The format for declaring a variable is [Variable type] [Variable name] [Any initialization to take place]
Take a look again at "int a;" Given the format I described this means "int" is a type of variable and a is the name to give the variable. The easy one out of the way first: the name is simply whatever name you want to give your new variable. The compiler will then keep track of this variable in the code via it's name.
The variable type is a bit trickier. At this point I'm going to remind you that the processor does alot of manipulation of data and that this data is most often kept in the system's memory. The variable type, therefore, does two things: tells the compiler how to handle this variable when it appears in code and how much memory to set aside in the system memory for this variable. If you look up information about variable types on MSDN (http://msdn2.microsoft.com/en-us/library/s3f49ktz(VS.80).aspx) you'll see that "int" causes the compiler to set aside 4 bytes of memory for your program. You can then fill this memory with whatever data is allowed.
Remember that the compiler also understands how to use the variable because of the type you specify. Variables can have different uses depending on the type you specify. "int", or integer as it's called sometimes, tells the compiler that this variable will hold a number. "char" on the other hand, a different variable type, tells the compiler that this variable will hold a letter such as "a" or "b".
The second line "unsigned b = 0;" is even trickier. I'll cover the " = 0" part, but covering the unsigned part is a bit advanced so I'll only briefly mention here that "unsigned" tells the compiler this number can't be a negative, such as -1 or -7. As for the " = 0" part, this is call initialization. Variables don't hold any information before you initialize them, but you can give them a value as soon as you declare the variable, such as I've done with b. The variable a, on the other hand, would require another line such as this:
int a;
a = 7;
Advanced (skip this paragraph if you want): [The "unsigned" keyword is actually so the compiler can give a specific instruction to the processor itself. Understand that ultimately all variables are simply numbers, even "char" variables. However, the processor has 2 flags it can set to handle two specific kinds of numbers: negative and decimal. When a processor recieves a number these two flags can either be set or unset, letting the processor know whether the number has a negative sign or if it has a decimal place. ]
Conclusion:
I'm taking a rather unorthodox approach to teaching this, but I think it's important to get a bit of a peek behind the scenes to understand what's truly going on. Understanding variables from the standpoint of the processor and memory helps when dealing with the concept of pointers.
Pointers, the last thing I'll mention, hold memory addresses rather than data. I could declare a pointer using the * symbol like this:
a = 7:
int b* = &a;
What this does is assign the memory address (a number that tells the processor where the data for "a" is kept in memory) of "a" to b so that b will reference the data of a every time it's used. The & symbol in front of "a" instructs the compiler to get the memory address of "a" rather than the data it holds.
That's the end of lesson 1. This is alot of work, and I have no idea who will or will not understand it. I'm putting this out there though, for the heck of it!
Sunday, January 13
as provided to you by Bob @~~~ 12:11 PM
Subscribe to:
Post Comments (Atom)
0 \comm\e.nts:
Post a Comment