Huacati’s C++
Tutorials
OpSciFi
Okay, this
first tutorial is for people who have not
yet touched C++. If you haven’t, or
just want to brush up on your skills, read on.
{ NOTE: To go through these tutorials properly, I
suggest installing a C++ Compiler program (recommended: Visual C++ Development
Studio). Check www.cplusplus.com for a list of
compilers.)
(About
Questions: Whenever you see QUESTION, this is for you to test
yourself on what you’ve learned so far.
To see if you’re right, highlight the hidden text after Answer: )
Difficulty: Beginner
Will Learn: The basics
Won’t Learn: Advanced basics :P
Will Make: Hello World
START
Comments
are in red
Code
is in blue
My text is
black.
Okay, I
know Hello World is very simple, but it’s a good way to start. All this program will do is bring up a console window that simply has Hello World.
Now, I’m sure
you’ve all heard of an integer? It’s
simply a byte (piece of computer memory) that holds a bunch of numbers. And you’ve all heard of a char (same thing,
just with letters). These are using to
define the programs functions.
All
functions start out empty. A function
that stores a value NEEDS to take it
back somewhere. For example, a function
can only work if it brings in a number that is greater than one and leaves with
a number that is also greater than one.
So, you
say. What happens if it tries to take
back 0? Very simply, the program EXITS. This is the way you end a program.
Right, so
how do we start a class?
First, the
class must be contained in a .cpp file. This should be the same name as your
program.
When you start
your compiler, try to make a win32 application.
If you can’t, look for another compiler :P
So call it HelloWorld.cpp. (The compiler should do this for you
automatically).
So, the
main function that starts the program is always an INTEGER. In C++, and integer is shortened to int.
So:
int HelloWorld()
^ ^
The integer | The name of the function.
Note the ().
This is an area to add special values (more on this later).
So this is the
start of the function. Not that hard,
was it?
Now, that’s not
all. A program has to have a START &
and END. This is where { & } come into play.
{ mean the start of the
code area of the function.
}; means the end of the code area of the function.
int HelloWorld()
{
return 0; // Remember that returning 0
exits the program.
};
Okay, so now you
know how to start a class. For a bit
more reference, here are some more STORAGE TYPES (eg.
Integer)
int = Integer (numbers)
char = Character (bunch of
letters)
long int = Long Integer (an even longer storage-integer type.)
short int = Short Integer (an even shorter storage-integer type.)
float = Float (a type of integer,
made up like currency. E.g. 3.13, 5.22)
void = Void (this simply does the function, and
nothing else. It does not need to return
anything)
Okay, the one that is most
interesting there is void. When a program is a void, you don’t need to
return anything. It just ends the function
once it reaches the end of the code.
----------------
QUESTION
----------------
Void, and any other non-number storage types cannot
be used for the main class. Why?
Answer: Because if
they were assigned, the function could not return a 0, thus meaning you can’t
exit the program, creating an endless loop which crashes the program!
Now, we’ll learn how to print
to the screen.
Printing to the screen isn’t
very hard. But, this will only work in a
Win32 Console Application (the one you’re making now).
Now, there are two types of
things that are contained in the code of a function, the COMMANDS & the
VARIABLES.
COMMANDS are basically build
in functions that tell the compiler what to do.
For example, we are about to use printf(), which tells the compiler to print the following to the
screen.
VARIABLES are special values
that are made up from the storage-types (int, char,
etc.), and are referenced so you can easily find them again.
For example, you’re making an simple text-based RPG.
Now, you want the character to have a Health value. The way this is stored is by declaring it’s storage type, then the name of the variable.
Examples:
int phealth;
int Health;
int PlayerH;
int HP;
Wow, not that hard. But did you notice something special in those
example declarations? If you did, give
yourself a pat on the back.
What you should have noticed
is the ; .
; is used to
say to the compiler that this is the end of the command. EVERY COMMAND MUST END WITH
; .
If this is not done, the
compiler will move onto the next command, and cause an error.
(NOTE: This is one of the most common C++
mistakes. If you have errors, check and
see if a ;
is missing from the end of one of your commands.)
Now, there is
also a few more types of things you can stick in your code. FUNCTION-CALLS, POINTERS, ARRAYS,
FOR, IF & WHILE statements.
These will be covered later on.
Now, we finally print to the
screen!
As stated earlier, the
function to print to the screen is printf(). Time to put this in your code.
int HelloWorld()
{
printf();
return 0;
};
Okay, nearly there. Now we have the function, the command and the
return. Though, where does the compiler
know to say Hello World?
That’s what the () is for in printf. These are
the COMMAND CALLS. Inside the () is
where the compiler is told what to do.
Before we do that, note that anything
inside “” is text. So, to say Hello
World!, we’d put printf(“Hello
World!”);
And that’s it!
int HelloWorld()
{
printf(“Hello World!”);
return 0;
};
And, that’s it. You’re done!
Now just compile the program,
and watch the magic fly.
Okay, I may have stated earlier
that creating an empty project will work.
Well, I was wrong.
You’re compiler MUST be able to build a Win32 application for this project
to work. It must create the main HelloWorld.cpp, plus two other files, STDAFX.cpp
& STDAFX.h
That is why I recommend Microsoft’s Visual C++ Development Studio.
Try to find a copy, I’d recommend it.
NEXT TUTORIAL: Advanced Basics, Commands, .h files,
plus storing and receiving files in an variables and
calling multiple functions.