|
|
|
|
Back to Bluesfear |
|
i. style
Computers
read binary, humans don't. Computers don't care how the data is styled, humans
do. Thus, style your code. If you don't, somebody will kill you. Actually, in
all seriousness, style is an important part of programming. The idea is to make
your code readable so that if you ever have to go back and debug, you're not
going to rip out your hair or punt your dog in the process. Common practice
amoung programmers these days is to indent your blocks and use lots of
whitespace. Whitespace are things like spaces or enter lines. To demonstrate
why style is so important, consider the following blocks of code (if you don't
understand them, don't worry, I'm just making a point).
for
(int x=0;x<10;x++) {
cout
<< x << endl;
if
(x<5) cout << "Lower Half" << endl;
else
if (x==5) cout << "Middle"
<< endl;
else
cout << "Upper Half" << endl;
}
for
(int x = 0; x < 10; x++) {
cout << x << endl;
if (x <
5)
cout << "Lower Half"
<< endl;
else if (x == 5)
cout << "Middle"
<< endl;
else
cout << "Upper Half"
<< endl;
}
Note that
with the addition of whitespace and formatting the code becomes much more
readable. Also, you can quickly tell what code belongs to what block. For
example, the cout << "Lower Half" << endl; is executed if
the condition from it's parent logic block is executed as true. For indentation
there are several methods. Some people use two spaces to indent, some use
three, some use tab spaces. I believe all Microsoft compiler IDEs use a tab
spacing that you can set. I myself normally use two spaces (to keep more code
on the page) but in the interest of readability (or until I forget) I'll be
using tab spaces.
One last
thing, make sure to comment you code. This is very important especially if you
have a terrible memory like me, or other people will be reading you code. A
well documented program is a happy program. In C++ there are two kinds of
commenting methods. You can comment a single line using // or you can comment a
group of lines by surrounding the group with /* ... */. Lines that are
commented are not read by the compiler, they are completely skipped over so you
can put as many as you like and it will not change your program size at all (if
anybody even cares about that anymore).
Example:
// This line is commented
int
x = 5; // this is a variable, more on this later
/*
this group
of lines
will not be read by the compiler
*/
// x = 5;
Note that
while the last line is valid C++ code, it is commented out and thus will not be
read by the compiler.
ii.
beginning your program
By now
you hopefully have a compiler and an IDE in which to program. All C++ programs
follow roughly the same guildlines. Here is a rough format for a basic C++
program
#include
<...>
#include
"..."
(constant
declarations)
(function
declarations)
int
main (void) {
...
return 0;
}
(function
implementation)
To
explain, I'll go through it line by line.
·
#include <...> - These are your standard header file includes. C++ has
functions premade to help you with things like screen IO and math functions so
you don't have to write them yourself. A SIN function is annoying to write and
if somebody else has already done it for you, then why waste your time?
·
#include "..." - These are your own include files that you have made. The
only difference is that the file name (ending in .h for header). More on how to
create your own include files later.
·
constant declarations - This is where you declare your global constants...
variables that your entire program recognizes. You can also declare global
variables here but that is generally frowned upon. People like programs that
are modular these days and it makes sense to try to code a program well enough
that you don't have to rely on globals.
·
function declarations - this is where you put the declarations to your functions.
This is so the compiler can recognize that there are functions comming and so
you can put the functions in any order. Other programming languages such as
pascal don't have this so you would have to make sure that any function that
called another function was written before the one to call it. Confusing,
annoying, stupid. Just declare your functions :)
·
int main (void) { } - this is the main function, the entry point for all C++ programs
(unless you are doing a non-console program (which we aren't so there). You
call your functions from here and guide your program from here. There are several
ways to write the main function (which will become more apparent when we
discuss functions in general) but for now, stick to this format. Also, be sure
to include the return 0 line at the bottom. Again, this will be explained
later.
·
function implementation - this is where you actually write your functions that you
have declared above.
Now that
you have all that down, here's an example program (don't worry about
understanding) just so you can see how a basic program fits together. I'll fall
back to the ever famous "Hello World" example to demonstrate.
#include
<iostream.h>
#define
PI 3.141592654
void
dispText (const char
*text);
int
main(void) {
cout << "Hello World!"
<< endl;
cout << "The value of the
constant PI is: " << PI << endl;
dispText("HELP ME!");
return 0;
}
void
dispText (const char
*text) {
cout << "We are now in the
dispText function!" << endl;
cout << text << endl;
}
Please
note the structure and formatting, this is what your program should end up
looking like. Each person develops their own style, but try to keep it
ledgible. Also, you may have noticed (if you haven't programmed much before)
that there are semi-colons at the end of almost every line. This signifies to
the compiler that the line is finished. We don't put these after certain
commands because we want the compiler to know that there is other data that
follows relavent to the above command. An example is functions or if-blocks.
Most compilers these days are pretty good at screaming at you when you miss a semi-colon
so it's not a big deal, but be aware of it so you're not utterly confused when
these errors do crop up, and get used to putting them at the end of your lines
because they must be there.
iii.
simple io
In case
any of you don't know what io means, it stands for input output and if we're
going to do anything, it's helpful to see what it is we are doing. Say for
example we create a variable and do an arithmetic operation on it, then we want
to see the result. Well, we need to somehow output that to the screen. In C++,
the easiest way to "talk" to the screen is with an io stream. First,
you'll need to include the appropriate file. The objects (functions) we will
need for our io are located within the file called iostream.h. This is a header
file, or more appropriately, a library file. It contains the definitions to the
library of functions and objects we will be using. To use them in our file we
use the include statement discussed in above. There are several forms of io,
but we are going to stick to the simple ones for now. It isn't essential to be
able to format you text and such while we are learning the principles of C++ so
we'll just get some basic input output going.
There are
two objects we will talk about. One is cout, and the other is cin.
Before we can discuss how these objects work, we must discuss a certain pair of
operators in C++ called the data insertion operator, and the data extraction
operator. Since cout and cin are stream objects, we will need these operators
to send data to the stream, and get data from the stream.
The data
insertion operator, <<, works with cout to send the data you insert to
the cout stream. For example:
cout
<< "Hello World!";
This
takes the string data, "Hello World", and sends it to the cout
stream. This places this text into a buffer which will be sent to the screen
when there is an "enter character" in the stream, or the stream is
manually flushed. Please note that if you are unfamiliar with string data it
will be introduced in the variables and data types section, and then expanded
upon in the arrays section. For now all you need to know is that strings are
characters enclosed within quotes. Now, there are two ways to send an
"enter character", or a newline, to the stream. One is by using the
\n character within your string and the other is to use the data insertion
operator to send endl to the stream. The endl identifier is a predefined
constant within iostream.h and just means end line. Something to note is that
\n is an escape sequence or escape character. When C++ sees this in a string it
really means newline. There are several others including \t which means tab. To
find a complete list of escape characters search google, they'll pop up. That
being said, \ is not treated as a character within a C++ string per say. For
example, if you had the following line,
cout
<< "bla\h";
The
result wouldn't be as expected. C++ would think that the h was a special
character. So, if you want to have a backslash within your string, simply put
two of them. "bla\\h" would actually output "bla\h" to the
screen. So, now that that's all cleared up, an example of using these methods
is as follows.
cout
<< "This is one one line \nThis is on another line";
cout
<< "This is on one line" << "This is on another
line";
Note that
at the end of these instructions, the cursor will be after the e in line. To
bring it down to the next line, use another endl at the end of the line. Notice
that you can string multiple data together when you use this form of io.
cout
<< "This is" << " my test" << " of
the cout function!" << endl;
It is
usually a good idea to have an endl at the end of your cout statement unless
you want it to stay on the same line. If for some reason you need to flush the
buffer manually before a newline is reached, use the flush identifier. This
works the same as endl but you use flush instead. Note that this usually isn't
a problem, but it cropped up for me when I was mixing C code with C++ code. The
procedural code of C would execute before the objects of C++ could execute. Yes
it's odd, don't worry about it.
An
example of flushing the stream:
cout
<< "This text will appear immediately after this instruction is
executed!" << flush;
Now, to
input data from the keyboard for example, we use the cin strem object in
conjuction with the data extraction operator, >>. This involes using
variables so if it isn't clear right away, it will become so in the next
chapter. Lets just jump right into an example snippet of code.
int
x;
cin
>> x;
This will
cause the program to pause and wait for input from the keyboard. Input is ended
when the enter key is pressed. This input goes from the keyboard into the cin
stream and then is extracted from the cin stream into the variable x. Sounds
complicated but it's moderately simple.
Written For: Omicron of http://www.bluesfear.com
Back to Bluesfear
Digital Art
2000, 2004© BlueSfear