Gw Temp

Menu

Tutorial - 'A simple C++ Tutorial' by bluemoogle

An item about Programming Languages posted on

Blurb

An excellent tutorial for beginners wishing to take up C++

Body

CONTENTS:

1. The Introduction
2. What is...?
3. The Newbie Guide
4. Now what...?
5. Conclusion



----------------------
1. The Introduction
----------------------

YOu've more than likely heard of C or its newer version, C++. If you haven't, then, wow. Anyways, C++ can be hard, boring and gruesome. Or, if you're lucky, it'll be a blast and you'll breeze through it. If you've learned computer languages before, you'll find learning this will easier in some ways, yet harder in others. If you've never touched coding in you life, you don't have have any benefits I guess. First thing is first, if you've programmed before, you've more than likely have a COMPILER. If not, its time to get a COMPILER!

========NEW WORD=======================
COMPILER: It takes computer code and
turns them into a usuable .EXE
=======================================

Now, you can just take ANY compiler and turn your masterpiece into an EXE, you need a C++ Compiler! There are 2 main ones that people prefer, and a LOT of other ones that are not well supported. The First one is VISUAL C++ by MICROSOFT, its by Microsoft, so you'll have the best tech support for any problems, but I think it costs some money. The other, which is free, is by Borland, is called BORLAND C++ COMPILER.

Visual C++: http://msdn.microsoft.com/visualc/
Borland C++: http://www.borland.com/cbuilder/index.html

If you don't like either ones, check out www.google.com and search for C++ compilers. As soon as you have one, return here and we'll start our lesson!



---------------------
2. What is...?
---------------------

You can skip this section if you don't care at all on what C++ is and all. Just to note, this is not required to learn C++, I just found it cool. How C++ came to being is an interesting story. Back when computers were first made, they didn't have super hard-drives or power video cards or 3D. They had paper with holes in them. These papers and the things that read these papers were worshipped like how we worship our Mighty Playstation 2s and Xboxs. These paper cards had holes that the computer were scan, and if they were placed in certain spots, the computer would do something. These holes were in two spots, the "0" spot, and the "1" spot.

Why not 2,3,4,5,6,7,8, and 9 you ask? Uhhhh.. I dunno. Maybe because 0 meant false, and 1 meant true and there isn't really any other kinds. Each "row" of holes could handle many questions, but only questions that could have a true/false answer. Like, "IS THIS GOING TO BE THE LETTER A"... so you would need many cards to program anything on a computer. When more advance computers came out, these holes were still around, only electronic. An electronic card was a "byte", capable of holding 8 holes, or values, which we call Bits. Now bytes weren't actual cards, but on a hard drive, with a microchip of somesort (now, I don't know for sure.)

But how does this relate to C++? When the bytes came, you couldn't program with cards anymore, so you needed a new way to program. It became known as ASSEMBLY. A keyboard was used to type in simple functions and commands, which an ASSEMBLER would take and turn into those little holes (0 or 1) so the computer could comprehend them. This saved a lot of time programming things. But Assembly was still pretty hard, an improvement, but still hard.

So THIRD Generation languages came into being. A compiler was programed in ASSEMBLY. This compiler than read words/numbers that was inputted by the keyboard, turned it into assembly, which as assembled into machine code (those little 0s and 1s). Great! Since a compiler was a lot easier to make than a assembler, you could make the commands and functions a LOT easier to work with, make new programmers happy!

So be happy that you don't have to program in 0s and 1s, or assembly! Oh, C++ is a third generation language, incase you didn't relize that... eh. Oh, this little history may not be totally true, since I got it off some website, not out of Bill Gate's diary, so don't blame me if I'm wrong in some areas.



----------------------
3. The Newbie's Guide
----------------------

You now have your compiler, and maybe the history of computer programming, but that means nothing to you. You probably feel like you're were thrown into a deep pool of water without knowing how to swim. Don't worry, I won't let you drown! If you are lucky, you got a compiler that lets you program inside it, if you are not, you'll be programming in NOTEPAD or Micrsoft word. If your compiler lets you program in it, just click the new button, and make a new .cpp file. If not, create a new .txt file, but rename it so it ends with .cpp.

Now open your .cpp file (if you don't have it open already) and you'll see its blank. DUH. Now lets start our very very basic program. Since this is only the first in my series, it'll feature the MOST basic commands. NOTE: These programs will run in DOS, or command prompt (matters what OS you're running), since I myself have to learn how to make it run in WINDOWS.

Now don't worry, it'll still RUN on your computer, and it won't harm it. Don't be so paranoid.

To start our program off, we need the commands to use. Yea, this is whats cool about C++, nothing is default, not even the COMMANDS! The compilers though give you some commands to work with, and the most basic ones are from iostream.h!

==========NEW WORD============
IOSTREAM.H: A script file
that includes a bunch of basic
commands.
==============================

To use the IOSTREAM.H file, you'll need to INCLUDE it into your program. To do so, type #include
Great! Now we can use Iostreams many commands, so we don't have to make them! Next is to make our MAIN part of the program, or the main FUNCTION. This will run the instant you load the program. First, go to the next line, then type everything out until it looks like this:

#include
main() {
}

Now to go over everything, you already know the iostream part, it lets you use the basic commands, so you want to include it. Now for the main function part. Main() means this is the main function. In other functions, you'll put things in the (), but for now, we won't. The { and } means "Begin this function" and "End this function", in other words, everything between those two brackets will be part of the main functions, and start when the program starts.

Now lets use an Iostream command! Its called "cout", and its the same as "print" in other languages. Its a big black box basically, I don't know how it works, it just takes what you type and puts it on the screen. Its magical! Cout has some rules though, its a black box with a lock, and it won't open unless you use the right key. The key for this is <<. Yes, it lets you use cout. Without it, cout won't work! Between the brackets of main(), type:

cout << "Hello World!";

You know what cout is, it lets you print things, the << is the cout key, and "Hello World" is what we want to so say. ; is used to end all commands, without it, the next line is thought to be part of the previous line. Now, if you had a cout, and a bracket, the bracket would be thought to be part of cout, making main() never end, giving you an error in some cases! SO NEVER FORGET ; WHEN YOU FINISH A COMMAND!

More onto the << thingie. Say you wanted to say three things on three lines. Instead of typing three lines of couts and stuff, you could simply put it on one like. Like this:

cout << "Hellow World!" << endl << "OH NO! I spelled it wrong!" << endl << "I Meant Hello World!" << endl;

You may of guessed that endl means to end the current line. NOTE where the ; is! If we put it in the middle, everything following the ; wouldn't be part of cout, and wouldn't be said or done! Now onto the Computers brain. Yes, your computer has a brain. Its called Variables. Your computers brain has two parts, just like yours. It has Short term memory, and a long term Memory.

In an instance of short term memory, you may think about getting some pizza, go get pizza, and return, remembering what you were doing, and why you got pizza. In an instance of a long term memory, you thought about pizza, when and got pizza, and returned, then 5 days later, you remembered you got pizza, and decided to get pizza again.

A computer can do the same. Both long term and short term variables ( I forget their real names ) have their uses. An example, a long term variable will never get deleted, so you can always use it. These could be things like Players current HP, MP, weapon, and so on. A short term variable could be used to hold a temporary value, like a decision on a quiz that would be immediatly used and changed, or something you don't plan on saving (in other words, this variable would be deleted).

So how do you make a variable short or long term? A short term variable is made inside a function (like inside the brackets of main() or bob() or something similar) and would be deleted (with its value) when the function ends. The other variable is made outside of a function (not inside { and }). It won't be deleted, nor will its value. If you "declare" a variable long term, you can still use it inside a function, and it will still not be deleted, and it will still keep its value! I know, its a little confusing, but variables are confusing, and I'm not the only teacher on C++, so check the millions of books that explain variables ALONE if you need some more help.

PHEW! The hard part was done! Now to make a variable. For this program, we'll need a short term one, so why not put it right under the cout line? To declare a variable, you need to first declare what subtype it is, what its name will be, and what its initial value will be.

int answer = "no";

Explaining.. humm.. INT is the subtype. A subtype basically means how many bytes this variable can store. Note, the bigger this subtype, the bigger the space this variable is going to take.. making your program larger.

Char = stores only 2 byes worth of info
Short = stores only 2 bytes worth of info (NOTE, this does not mean its a short term variable)
INT = Stores 4 bytes worth of info
Long = stores 4 bytes worth of info (NOTE< this does not mean its a long term variable)
double = stores 8 byes worth of info (capable of handling decimals)

Why so many 2s and 4s? I wondered that myself, but I'm thinking it has something to do with how it uses its bytes. For now lets juse use the default Int. It can't handle decimals, but thats ok, for this variable will be handling letters. Next is the name, answer, and from now on, when you type in answer, it means you're refering to the answer variable. The = sign is an operator, which basically is the variable command. "no" is what its value is. NOTE: Strings (words, letters, so on) must be put in "" or the compiler will think its a variable name. NUMBERS do not need be put in "" since NO VARIABLE can start with a number. Here are the operators:

=: The basic set option. Whatever is on the right, is instantly stored to the variable on the left.
--------------
+=: Adds the number on the right to the variable on the left.
-=: Subtracts the number on the right to the variable on the left.
*=: Multiplies the number on the right to the variable on the left.
/+: Divides the number on the right to the variable on the left.
%=: "mods" the number on the right to the number on the left.

NOTE: When initially setting a variable, you can only use the = operator.
Lets say we want to change the value of answer. Before we can change the value of answer, we have to make sure it was made first (so put the value change line AFTER the declare line). You can do something like this:

answer = "yes";

Note that we don't have to put int in front of it anymore! Yay! Finally, lets print answer on the screen!

cout << "The answer is " << answer << endl;

Tahdah! YOu did it! You made a program! Yay! Oh wait! One more step. Save your work, compile it, and open it up! If something doesn't work, try opening it up through command prompt or dos, and type in the location of the file. If it still doesn't work, bug test your program.



-----------------------
4. Now what...?
-----------------------

You've made the dumbest program on earth, but you must admit you're pretty proud of yourself. You know part of the basics, but even then, you still need to review, so take this quiz!

1. What is a Compiler?
2. What is Iostream.h?
3. What does cout do?
4. Whats the special key for cout and what does it do?
5. What are the two types of variables?
6. Can a variable begin with a number?
7. What do you end all commands with?
8. What are strings, and what do you need to make sure you put around them?
9. Whats the order of declaring a variable.
10. What is main()

*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************
*******************
********************
*********************
**********************
*********************
********************
*******************
******************
*****************
****************
***************
**************
*************
************
***********
**********
*********
********
*******
******
*****
****
***
**
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************
*******************
********************
*********************
**********************
*********************
********************
*******************
******************
*****************
****************
***************
**************
*************
************
***********
**********
*********
********
*******
******
*****
****
***
**
*

Q. What is a Compiler?
A: Its a program used to turn code into a usuable EXE.

Q. What is Iostream.h?
A: A file that includes all the basic C++ commands

Q. What does cout do?
A: It takes things and prints them on the screen

Q. Whats the special key for cout and what does it do?
A: << and it seperates the parts inside of cout.

Q. What are the two types of variables?
A: Deletable (short) and Global (Long)

Q. Can a variable begin with a number?
A: NO!

Q. What do you end all commands with?
A: ; and don't forget it!

Q. What are strings, and what do you need to make sure you put around them?
A: They are a group of letters or words, and MUST have quotations (") surronding them.

Q. Whats the order of declaring a variable.
A: subtype name = value;

Q. What is main()
A: The function that runs when the program starts.



-----------------------
5. CONCLUSION
-----------------------

I hope you answered atleast 7 of the questions above right. If not, don't worry, just practice a little, and maybe re-read parts you didn't understand... or totally ignore me and just run over to wal-mart and buy a book that explains everything. Whatever you choose, good luck, and maybe you'll be programming the next Warcraft or Grand Theft Auto!