|
|
|
Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. |
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 4 submission(s) by this author
|
|
|
Report Bad Submission |
|
|
Your Vote! |
See Voting Log |
|
Other User Comments |
10/19/2002 12:05:37 AM:Isaac.Luxford Looks pretty good, but I didn't see a
line 32. I wish I had this when I was
learning C++ though. 5*
|
10/19/2002 10:11:26 AM:[SpiderMan] Perfect work! Just like the ver #1 of
this tutorial, only that this one is
better :) 5 globes again!
|
10/19/2002 10:32:41 AM:Thorgrim Nice tutorial :)
I really hope you
will continue writing !
|
10/19/2002 7:10:10 PM: Thank u
now I want programs about
OOP
and Application project
|
10/19/2002 7:20:17 PM: Thank u Jared Devell
I want to level
2 or medile level
and some Application
Project
other time thank u
|
10/22/2002 6:44:58 AM: Thanx for your help bro!
|
11/9/2002 2:38:16 AM: I'm using these tuts of yours to learn
c++ and these are great. I've given
both 5 globes.
Would you be able to
write a tutorial on calling a routine
from inside the main() routine?
I was
just trying to see if I could do so,
but I cannot figure it out without a
bit of help.
|
11/14/2002 4:18:09 AM:José Pablo Ramírez Vargas I can easily see that you put a lot of
effort in doing this. It is very
good.
I am a VB programmer and
started with C++ a few days ago. I
understand most basic parts of the
language now, but there are still
things I don't get.
For example,
what's the difference between using a
compiled header file, or not using one?
Or what does "extern" do? Or, in
general, a tutorial on preprocessor
keywords.
Or something about
structures. What is the difference
between these syntaxes?
1. struct
struct-name
{
list-of-members
};
2. typedef
struct
[tag]struct-name
{
list-of-members
}
tag-modifiers;
What is the tag all
about? Also, a tutorial on typedef
would be good. Anyway, just a few
ideas for tutorials. :)
|
11/16/2002 11:16:27 PM:Patabugen thanks again! Looking forward to the
next one!
|
1/8/2003 11:09:55 PM: I think it's good so far.
|
1/23/2003 10:15:11 PM: Hi there! Thanks for all these helps
pages. I've been taking C++ since the
2nd quarter in school. Do you know how
to write a program to convert binary
number to decimal and decimal to
binary? If so, can you please send it
to my email? Thanks a lot!
I'm
using Microsoft Visual C++.
|
2/5/2003 8:24:53 PM: Can you send me a program microsoft
visual C++ to me by email thanks to
your
tutorial,I want to try out that
program
so can you send it by email?
|
2/6/2003 9:12:21 AM: It would be good to mention that
declaring variables usually assignes
them by default. This default-value can
be compiler-dependent (usually 0 for
integers). Using unassigned variables
is a common cause of unpredicted
program behavior. I suggest outputting
some variables before the first
assignement, to see the defaults in
this example.
|
2/13/2003 2:55:32 PM: hmm is it me or the 2 boolean variable
are useless?
nice job :)
|
2/20/2003 9:55:57 PM: Jared... I am having a difficult time
understanding the coding for the
variables. At one point myNumber is
defined as an int (which I thought had
only one character), but then it is
defined as a four-digit number. In
either case, the four digit number does
not show up when I run the program. Am
I missing something? Also, you
described
|
2/20/2003 9:57:51 PM: Also, you set the long variable to "5",
but I thought this was for 4-8
characters. Could you please help me
understand?
|
3/2/2003 1:15:05 PM: ok im completely lost. i tried it your
origional way as you showed and im
getting these
errors:
"C:/Dev-Cpp/include/c++/bits/st
l_alloc.h: At global
scope:
C:/Dev-Cpp/include/c++/bits/stl_
alloc.h:575: syntax error before `;'
token
C:/Dev-Cpp/include/c++/bits/stl_a
lloc.h:575: confused by earlier errors,
bailing out
Execution
terminated"
any help?
|
3/6/2003 1:30:28 AM: Very good, better than your first one.
I'm learning *beams*
One problem. In
line 1, you failed to mention what
#include is including. I got an error
and had to refer to your original
tutorial. I don't mind, it was good
practice for me, but others might be
frusturated.
|
3/22/2003 1:28:02 AM: Great tutorial! and with a name like
DEVall ;) it must be easy. 5 globes.
|
4/22/2003 5:09:47 PM: you need to write a book. i like the
way you explain what your doing and why
|
5/3/2003 10:53:29 PM:Sarafraz Singh Johl Correction: Range of Signed Integers is
from -32768 to 32767.
|
7/18/2003 1:25:46 PM: Ah your tutorials are awesome! I
acually learning C++ =D =D =D
|
8/6/2003 10:03:31 AM: How do you compile in Visual C++? I
keep getting strange errors I don't get
in DEV.
|
8/7/2003 5:15:34 PM: none of the bool stuff is working for
me... bool doesnt even turn
blue
????????????????????????????
|
8/7/2003 6:43:49 PM: nevermind, i just needed the new
version
note: u forgot <iostream.h>
after #include in your example
|
8/17/2003 9:55:34 AM: Man im really lost in all this. I want
to make a video game and im trying to
figure out how to use this c++ stuff.
can u email me at
blisteringbunz@hotmail.com and explain
how i can use this to make a game? thx
|
9/25/2003 1:06:51 AM:Jared Devall Answers and Questions to the Tutorial &
Comments! Ask and Answer:
www.yuidesigns.net/~rendelven
|
9/30/2003 4:00:25 PM: I still got this odd problem... Even
though the program works, it runs
itself in DOS, it doesn't freeze itself
but it doesn't show the text. Also, you
forgot to add the "iostream.h" after
the #include. But again, good job on it.
|
11/12/2003 1:30:26 AM: i guess c++ really is case sensitive im
not really clear on variables but im
gonna keep on
|
11/17/2003 1:51:28 PM:James Dunne Great Tutorial Jared 5 Globes, Helped
me alot!
|
11/18/2003 6:19:21 PM: thanks, it helped me alot:)
|
|
Add Your Feedback! |
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. |
|