|
Greetings all. I've agreed to be taken on to write a few C++ tutorials for those inquisitive folks out there. This month we'll cover the most basic of all programs (and, coincidentally, the standard "first program", in any language), as well as the very basics on how to get started.
Now just a couple of warnings before we get started. First off, I am primarily a C++ programmer. I can read C, and I can program in C, just not very well. So everything I write will be from an object-oriented standpoint. This means that I'll be doing my best to stick things in classes where applicable, and I'll also later on be taking a stab at object-oriented design patterns. If you're coming from a procedural background (e.g. COBOL/PASCAL/FORTRAN/Whatever), this may be a bit hard to swallow, but fear not! I'm always available via e-mail to answer questions.
Next, I am a bit coddled when it comes to development environments. I've always had access to free copies of things like Visual Studio, which can be rather on the expensive side if you're looking to purchase it on your own. The practical upshot of all this is that I am a windows developer. I have experience with UNIX and Linux, mostly due to some stubborn classmates I was on a project with who claimed to be fighting a vaguely-defined battle against the evils of Micro$oft and monopolies, but it is by no means my primary environment. If you're working on a linux machine, I'm sorry - I won't be able to help you much. I'd suggest you track through the man files that came with your compiler (most likely gcc/g++) for any answers you may seek.
Finally, most of my development will be in Visual Studio.NET 2003. This probably won't be an issue, but if we ever get into anything that's a little on the intensive side programming-wise, strange compatibility errors may show up. However, this definitely deserves mention because it presents a crucial problem in the first segment of code I will feed you (I'll explain it when we get there).
So with that said, let's hit the basics.
Getting a Compiler
Note that this section was ripped directly from my first article on game development. Some text has been changed to protect the innocent.
The only things a programmer needs are a text editor and a compiler. Whether it is a Pico-like editor and GCC or a full-blown IDE, you can’t do much of anything without something to translate the code into an executable. There are plenty of decent compilers out there, but most of them aren’t cheap. If you aren’t a lucky student (like me) who can get Visual Studio.NET for free from school (Don’t worry MVPs – it’s the MSDNAA, not piracy), you’ll probably want to opt for a freeware version. Some of the more prominent ones, and where they can be found, are:
- MinGW Studio for Windows – http://www.parinya.ca/ - A free IDE based off of MinGW (Minimalist GNU for Windows) – has much of the look and feel of Visual Studio, without all the costs
- DJGPP - http://www.delorie.com/djgpp/ - for those of you out there who firmly believe that GUIs ruin productivity, this freeware compiler is for you.
- Cygwin – http://www.cygwin.com/ - Not really a compiler, but it comes with several compiler packages. This program more or less emulates a Unix terminal on a Windows machine, giving you all the joys of VI and GCC without the pain of reformatting and partitioning
Personally, having worked with MinGW Studio for Windows, I think it represents
the best free IDE for the budding developer. Of course, none of this is necessary
- given a default compiler you can do all your work in Notepad (and some
people die by that approach), but it's still nice to have a program that
arranges everything for you.
With a compiler and editor procured, we're now ready to move on to the main course.
Hello, World!
The following example represents the quintessential beginner's program. Any programming book on the planet is going to start you off with a variation on this program. Indeed there's a site out there (I don't have the address at the moment) that has a variation of this program for every language. It's called the "Hello, World!" program, and it goes a little something like this:
file - main.cpp
#include <iostream>
void main(int argc, char* argv)
{
cout<<"Hello, World!"<<endl;
}
Let's talk about this a bit. The first comment is just something that I'll be
doing - a way of denoting filenames (as in, this code will be in the file main.cpp).
It is a standard convention to name your code files *.cpp, and your header files
*.h (will come to these later). Consequently, you should not include that line
in your program
The next line - #include <iostream> - is what as known as an include
statement. What that basically means is that we have some pre-defined helpful
functions in a file somewhere else, and we want to use them in our program.
The file that contains the functions that we'll be using is called iostream.h.
This is where that first critical difference comes in. In almost all compilers
except for the latest version of Visual Studio.NET, you will get an error
at this point. If you'll notice, I cleverly neglected to include the .h in
my #include statement. The reason for this is that Visual Studio .NET doesn't
like to accept the "old" style of includes. If you're using a different compiler,
or using an earlier version of Visual Studio (I hear version 6 is still popular),
you'll have to change the line to:
#include <iostream.h>
This should solve any problems. Note that you'll probably have to do this throughout my articles, if you don't have the latest version of Visual Studio.
The subject of include files bears some mentioning. Basically, what this does
is go out into your compiler's program directory, and look for a file called "include.h".
Any compiler worth the bits it's written in will have this file, in addition
to a host of other files, included with its install package. If you do not
have this file (and it'll become evident fairly quickly - you'll get errors
like "Unresolved External Symbol", which is a shorthand way of saying "What
you talkin' bout, foo?"), then you will have a bit more trouble compiling the
program than I am prepared to give. The long and short of it is if you don't
have this file, or if the compiler tells you it can't find the file, it's
time to get a new compiler. We'll cover the basics of the #include statement
next month (including things like locating custom include files), but for now
just know that a certain number of files come standard with any compiler, and
iostream.h is one of them.
Next, we come to the statement "void main(int argc, char* argv)". This is a function declaration. What we are doing here is designating the next block of code to follow with a name. Whenever a function somewhere else tries to call "main", the program will know that you mean this code. Let's tear this statement apart. Note that this is a special function, as will be defined in the following explanation:
- void - this tells the compiler that this segment of code doesn't return a value. We'll cover this more in-depth in a future tutorial.
- main - this is the name of the function. When another function wants to
call this function, it can just use the statement "main();". If we were to
change the word "main" to "steve", the statement would similarly change to "steve();".
We need to call this function "main" because it is what is known as an "entry
point". Basically, every computer program needs a place to start running.
If we were to feed this to the compiler as a series of ones and zeros, on
its own it wouldn't be able to figure out where to start. So the gurus responsible
for creating programming languages decided to give the compiler a little
help. They set a standard to be that all programs have a function that is the
same - namely the "main" function - so that the compiler knows where to start.
Without this statement, you'll get errors such as "No entry point defined".
Just take my word for it for now that all programs have to have a "main" function.
- (int argc, char* argv) - this is what is known as a parameter list. This defines a series of values that can be passed to the function when it is called. We won't cover parameters this month - just know that as a result of the choice of the main method, these parameters are required. We'll touch on them in a later tutorial.
The curly braces "{}" are required to define the extents of the function. Everything that falls within those braces is hereafter known as a part of the "main" function. These will become more critical as we move along the C++ ladder. Anything you put outside of these braces in the best case won't execute, and in the worst case will cause a compiler error (with some exceptions, to be covered at a later date).
Finally, the workhorse of this program. The line of code embraced by the curly
braces - "cout" Hello, World!"endl;" does one thing - it writes the words "Hello,
World!" to the screen. That's it. The statement itself is fairly simple:
- cout - this is essentially a call to a function described in "iostream.h". It takes the information passed to it, and writes it to the screen, protecting us from the horrors of manipulating the monitor ourselves.
- << - This is a "Stream" operator which tells
the operator on the left (the "cout") to output everything on the right
(the "Hello,
World!"). This can be stacked to form a "stream" of characters to be output
to the monitor (hence the "Stream" in "iostream.h" - it stands for Input-Output
Stream). You can have as many of these as your little heart desires,
and any successive calls to cout will tack things onto the end of prior
streams. It's just more nice to other people, who might have to read
your code, to use multiple "cout" statements.
- "Hello, World!" - This tells the program that you want to write "Hello, World!" to the screen. Anything between the quotes will end up on the screen.
- endl - This is a constant, defined in iostream.h, that tells the compiler to output a new line. This is more just a courtesy, and to be completely honest it takes just as much effort to write this as it does to write the character string it is replacing ("\n"), but this looks nicer.
And just one more thing - every statement in a function in C++ must end with a semicolon (;) - that way, the compiler not only knows that the current statement being read is complete, but it also knows that a statement isn't finished if you happen to go to the next line on a particularly long statement. Some languages (Visual Basic, for one) have opted for a different approach - no "terminator" character, as they're known - but they then have to require you to do things like place an underscore (_) at the end of the line if you haven't finished the statement yet.
And that's it. Your very own "Hello, world!". Tune in next month when we cover fun stuff such as variables, and writing your own include files.
|