L34N TEH C++ Lesson 2

Cunbelin

Newbie
Joined
Aug 3, 2003
Messages
281
Reaction score
0
A new stipulation I need to add, I don't garuantee I won't lose interest and stop these at some point, or that life won't intervene and prevent me from doing them, and though I am going to do my best to keep this daily, I can't garauntee I'll always make it. Now I have a clarification I want to make concernting the first lesson, in that lesson I talked about the iostream.h as being a library, this isn't true, what you are actually including is what is called a header file, I apologize for the mixup on the terms. I'll cover the differences between a library and a header file at a later date, but for now, I'm going to leave it at that.

Lesson 2
WI KANT I MAKE QWAK3 4 YET!?!

Today we have a pretty complex concept to work on, variables, but rather than jumping right in I'm going to give a little bit of information about how computer memory works. I think a good way to consider your computer memory is a huge warehouse, filled with unmarked boxes, the only way to find a paticular box is to know it's lookup number, it's address if you will, and what's more the contents of one box may or may not be connected with the boxes next to it, so you also need to know how many boxes the contents take up. So to find an item in this warehouse you need the address and how many boxes it takes up. In the case of your computer this means the address of the first element of the data, and what kind of data is there(how many boxes it takes up). Alright so that is all really nice but what does it have to do with us ? Well alot of what you are going to be writing is code that I'd term as memory management, and understanding how memory works will help you alot when we get to inheritence, and data structures are a vital part of advanced programs.

OK one more thing about how this works, we need to use a special instruction called an instantiation, most people don't call it this, and for now we are going to call it a declaration, because we are declaring a variable. So the pattern for declaring(or instantiating) a variable is as follows:

type name;

pretty simple, just the type of variable(more on this in a bit) and it's name, there are a couple of restrictions on the name. First one is that you can only use a-z, A-Z, 0-9, or an Underscore in a name. Second one, you can't use a number for the first character. Third is that you can't use a C++ reserved word. Fourth only the first 31 characters will be used. Fifth no spaces in the name(a given but just good to reiterate this one). There is one more restriction, and that is that a variable has to have it's own name within it's scope, for now let's just say you shouldn't have two variables with the same name.

Now here are a few conventions I use, you don't have to. First only lower case letters a-z, second, at least 3 characters, third if the name is made up of two words seperate the words with an underscore, fourth avoid numbers in the name if at all possible. Now these conventions are all breakable and in fact I bend and break them whenever it is convenient for me, but for now I'd suggest you pick a convention and stick with it until you better understand where you would want to deviate from the conventions.

So a quick reveiw:

Restrictions
  1. Must start with an alphabetic character or underscore
  2. Can only have alpanumaric characters or an underscore
  3. Can only be 31 characters long
  4. Can't be the same as a C++ reserved word
  5. Can't be the same as another variable
    [/list=1]
    Conventions
    1. always use lower case
    2. At least 3 characters
    3. seperate words with an underscore
    4. avoid numbers in the name
      [/list=1]

      If you are interested here a few more conventions that others may use:
      • start all words with a capital letter except the first one, and don't use an underscore
      • use all caps and underscore for spaces
      (if you have a convention please feel free to post it)

      Alright lets talk type now, first thing I want you to do is remember the boxes, well the official term for each box is a byte, and each byte is made up of 8 bits. That's the easy part, now the hard, C++ has some predefined types, unfortunately C++ only lays out their names, not their sizes (how many bytes). Here are the first three types and their most common number of bytes:

      char 1 byte
      short 2 bytes
      int 4 bytes

      These are probably the most common sizes you are going to see for these types.

      Alright time for a quick example:

      PHP:
      int an_int; //so we see here my amazing naming skills

      So I've just reserved four boxes(bytes) for an_int, and any time I want to look at what is inside those boxes, or put something in those boxes I use an_int to get to them. Alright well what good does that do us ? On it's own not alot lets kick it up a notch:

      PHP:
      #include <iostream.h> //same old same old
      //I know I said declarations go here, but I lied
      //there are very special declarations that are going to go here
      //for variable declarations we are going to put them in main for now
      int main ()
      {
      	int an_int; //there we go the declaration nice and easy
      	an_int = 7; //OMGWTF IZ THIZ
      	cout << an_int; //OMG WER R THI QUOTEZ
      	cout << "\n"; //WTF IZ THIS!?!?
      }

      Alright I've marked the new bits, lets talk about them, the first new item is this line:
      an_int = 7;
      I assume you probably figured out what happens here, an_int is made to equal 7, this is pretty much right, but I want you to forget that '=' ever was equals, I want you to read it like this:
      an_int "is assigned to" 7;
      Why, well first off the '=' is actually called the assignment operator, why it is called that, and why I want you to think of it that way will become clear later on.

      Ok next new thing is our first cout statement:
      cout << an_int;
      those of you who astutely noticed that there are no quotes get a cookie, basically we are doing the same thing as before with the quotes, but because an_int is a variable we can use it with cout without the quotes.

      Next one we have something new but in quotes:
      cout << "\n";
      Here it seems pretty obvious what we are doing, we are printing out a backslash and an n, actually what we are printing is a special character, the backslash is what we call an escape character, it allows you to use those keys that don't put a letter up on the screen, for now we are just going to use the \n sequence, if you want you can do a google or MSDN search to find other escape sequences you can use.

      Alright lets talk a bit more about the statement with the assignment operator:
      an_int = 7;
      The assignment operater is what is called a binary operator that means it requires two arguments, one on the left side and one on the right. there is one restriction on the kinds of arguments that can be put on the left, that is they have to be able to take whatever type the right side evaluates to, in this case there isn't really any evaluation, and as it so happens 7 is an int (being integer) so we're in good shape.

      Lets take a look at a more complex example

      PHP:
      #include <iostream.h> //whoa dejavu
      //first I read some code, and then I read some code just like it
      int main()
      {
      	int an_int; //was it the same code ?
      	an_int = 7; //I dunno could have been
      	cout << an_int; //why ?
      	cout << "\n"; //I apologize for the stupid matrix reference
      	an_int = 7 + 3; //OMG A PLUS SINE! ok pretty simple we evaluate the right side first
      	cout << an_int; //so that reads "an_int is assigned to 10"
      	cout << "\n";
      	an_int = an_int + 3; //What is this we are assigning an_int to an_int + 3!?
      	cout << an_int; //so if an_int starts out as 10 we add three to it...
      	cout << "\n";//and it reads "an_int is assigned to 13"
      	an_int += 3; //Ok this isn't a plus or an assignment, what happens here is 
      	cout << an_int;// we are doing the same thing as the last one just a little simpler
      	cout << "\n";// codewise
      }

      Alright well that is about it for today, a little longer than the last one. I have a few quick notes, first the official standard for binary operators(like the + or the =) is to have a space on either side of the operator I recommend you follow this for now but if you find code that doesn't don't be too suprised, secondly try using the multiply'*' and subtract'-' operators in place of the plus in the previous code, you can try the divide '/' but remember you can't divide anything by zero. Also always remember that you should first assign an_int to a number, your computer shares and reuses memory over and over again and it doesn't nicely clean itself out all the time. We'll be expanding on this concept of variables tomorrow and will write our first (questionably)useful program.
 
Gah missed the editing window, if I don't get to edit this I want to make sure anyone following this adds a 'return 0;' at the end of the main blocks I apologize I'm testing code on another machine and I forgot to copy it over. Here is what the updated code should look like:

PHP:
#include <iostream.h> //same old same old
//I know I said declarations go here, but I lied
//there are very special declarations that are going to go here
//for variable declarations we are going to put them in main for now
int main ()
{
	int an_int; //there we go the declaration nice and easy
	an_int = 7; //OMGWTF IZ THIZ
	cout << an_int; //OMG WER R THI QUOTEZ
	cout << "\n"; //WTF IZ THIS!?!?
	return 0;
}

and this one too:

PHP:
#include <iostream.h> //whoa dejavu
//first I read some code, and then I read some code just like it
int main()
{
	int an_int; //was it the same code ?
	an_int = 7; //I dunno could have been
	cout << an_int; //why ?
	cout << "\n"; //I apologize for the stupid matrix reference
	an_int = 7 + 3; //OMG A PLUS SINE! ok pretty simple we evaluate the right side first
	cout << an_int; //so that reads "an_int is assigned to 10"
	cout << "\n";
	an_int = an_int + 3; //What is this we are assigning an_int to an_int + 3!?
	cout << an_int; //so if an_int starts out as 10 we add three to it...
	cout << "\n";//and it reads "an_int is assigned to 13"
	an_int += 3; //Ok this isn't a plus or an assignment, what happens here is 
	cout << an_int;// we are doing the same thing as the last one just a little simpler
	cout << "\n";// codewise
	return 0;
}
 
A good naming convention is to use the type in the name:
iMyVariable
fFloatersRulezYoo
pLoveThyPointer

And correct me if I'm wrong, but cant one use cout << iMyVar << "\n";?
LOL, I'm really rusty :D
 
Originally posted by dawdler
A good naming convention is to use the type in the name:
iMyVariable
fFloatersRulezYoo
pLoveThyPointer

And correct me if I'm wrong, but cant one use cout << iMyVar << "\n";?
LOL, I'm really rusty :D

Yeah that is another common convention, and yes you can use cout like that, but to explain why that works you need to understand cout as an object, and also understand operator overloading. A bit much to cover at this point. Anyway time for me to head out, I'll probably be back around 2.
 
you could use loops on this too, but ill shut up till you get there :)
 
Next, you should do a bit of a lesson on proper code formatting. :p

Coding to at least some form of style is a necessity if you want to make readily readable code.
 
hehe nice ;) /me remebers his first c++ book/tutorial..

i prefer: cout<<an_int<<endl; same result ;)
 
Heh well next one we're going to finish up on variables and do conditionals, in addition to writing the most hideous app code known to man. I was going to do loops, but you really need conditionals to be a starting point before you deal with loops.

Yes I too prefer the endl; but that brings up even more issues, I mean we haven't even covered functions yet(Monday for that, then Tuesday for loops). And I still have mixed feelings about using constant strings at all without talking even a little about them, but strings really have to wait until you hit arrays.

As far as formatting, probably cover that in depth after we hit functions, where we really have something to organize.
 
Back
Top