article

A C++ Tutorial for Complete Beginners #2

Email
Submitted on: 1/4/2015 11:07:00 AM
By: Jared Devall (from psc cd)  
Level: Beginner
User Rating: By 68 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 3794
 
     This is the followup tutorial to "A C++ Tutorial for Complete Beginners #1". This article explains Variables!


 
				


Well, Hello World, Again! It's been quite a while since my previous Tutorial; time for a new one!

This tutorial will assume you've read " A C++ Tutorial for Complete Beginners #1" as this is #2. This tutorial will also assume you understand the concepts presented in the previous tutorial and can make use out of it. :)

*Note: I've since changed to using MS Visual C++ 6.0

Notes:
- Blue denotes a keyword
- Term Dictionary still not in use. ( Links don't work. )

I'm going to cover variables in this article. So, get ready! :)

Variables:
Variables are aliases for memory locations. That is, they hold some kind of data that you put inside of it. You declare a variable by choosing a data type, and choose a name or keyword to assign to the variable. There are, however, reserved KEYWORDS that you cannot use, as they are critical to C++ Programming.

A list of Data Types:
  • char - 1 byte - This represents ONE character. e.g. b
  • int - 2 or 4 bytes - This for a number. e.g. 500 or -500
  • short - 2 bytes - This is also a number, but is smaller than int.
  • long - 4 or 8 bytes - This is the largest number type.
  • bool - 1 byte - This is a boolean value. ( true or false )
Data types can change from system to system, but these are the 'normal' sizes for these types. There can be both signed, and unsigned numbers. Signed meaning the number can be negative, and unsigned meaning the lowest value the variable can have is 0.

Size Sign Minimum Value Maximum Value
1 signed -128 127
1 unsigned 0 255 (normal char)
2 signed -32768 32768 (normal short/int)
2 unsigned 0 65535
4 signed -2147483548 2147483647 (normal long)
4 unsigned 0 4294967295

Types default to signed unless you specify that it is unsigned, like so:
unsigned int

C++ is a very CASE-SENSITIVE language. What this means is that if you have 3 variables:
TestVariable
testVariable
testvariable


These variables are different. It's important to develop a consistent style of naming variables. I normally keep variables lowercase, with an uppercase specifying a new word, like so:
testVariable

Now that you know what data types and variables are you can declare them. You declare them like so:
int myNumber; *Note: You must have the semi-colon at the end!
You can also define them in their declaration:
int myNumber = 2002;

Char's are done close to the same way, except when you define them you must put the letter in single quotations ( ' ). Character strings, however, must be done a different way since the char type can only hold ONE character at a time. ( Yes, that mean it can only hold one A or one B at a time! ). Strings will be discussed in a later tutorial.

A bool(ean) can only be true or false, or their counterparts: 0 and 1. Booleans are normally used in if statements and other C++ goodies.

Okay, wipe that sweat from your brow! This was a difficult and large chunk of information to swallow. Let's end this up with a small program that utilizes what was presented here.


Open up whatever program you used in the last tutorial and follow the steps on compiling I showed you before. Again, remove the numbers from the source code as they are only there to help analyze the code! Save as tutorial.cpp


1. #include
2.
3. int main( )
4. {
5. int myNumber;
6. long myNumber2 = 5;
7. char myCharacter;
8. char myCharacter2 = 'c';
9. bool myBoolean;
10. bool myBoolean2 = true;
11.
12.
13. myNumber = 3678;
14. myCharacter = 'a';
15. myBoolean = false;
16.
17. cout << "myNumber = " << myNumber << endl;
18. cout << "myNumber2 = " << myNumber2 << endl << endl;
19.
20. cout << "myCharater = " << myCharacter << endl;
21. cout << "myCharacter2 = " << myCharacter2 << endl << endl;
22.
23. return 0;
24. }


Okay, now, the line-by-line analysis!

Line 1: This includes the iostream.h header. This, ofcourse, is needed for cout and endl.

Line 2: This is just whitespace.

Line 3: This is the main function mentioned in the last tutorial. You will always see one of these in any C++ program.

Line 4: This is just the opening brace for the main function.

Line 5 & 6: These two lines declare one int and one long variable. myNumber2 is also defined on this line with the value 5.

Line 7 & 8: These two lines both declare a char variable. myCharacter2 also defines itself with the value c.

Line 9 & 10: These two lines both declare a bool variable. myBoolean2 also defines itself to true

Line 11 & 12: Both of these lines are cosmetics: a.k.a. whitespace.

Line 13 - 15: These lines define these variables to a specific value.

Line 16: Whitespace again!

Line 17 & 18: These lines display the variables' values to the screen, sending an 'endl' to go to the next line.

Line 19: Guess what this is! Yup, you guessed it, whitespace!

Line 20 & 21: These lines also display the variables' values to the screen, sending an 'endl' to the next line.

Line 22: ...whitespace...

Line 23: This is the return statement. This will be explained in the next tutorial, along with the mystical "function".

Line 24: This is simply the closing brace. This ends the function, and effectively ends our program. ( When main is done, our program is done! )


For ease of explanation, whitespace will not be mentioned in future tutorials. I think we all know what it is. :)

This program simply declares and defines a few simple variables. It then takes those variables with printable values and prints them to the screen with cout. Again, this program will most likely close immediately upon execution. Just open up some kind of command-line and run it from there.

This concludes "A C++ Tutorial for Complete Beginners #2". As always, if anything is too complicated to understand or if I screwd up, be sure to let me know. Please let me know if you would like something explained a little more indepth, or whatever else. ( Please have it pertain to the topics discussed in this tutorial. ) I'll try to get the fixes when I can!

- Jared


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.