Start coding

Now that you've got your compiler installed, you're ready to begin programming!  Here's how: make a folder called mycode in the root of your c: drive. To do this, double-click My Computer, then double-click your c: drive, and choose File, New, folder.  This will be where you keep your source code and program files.  Now, go inside that folder via My Computer, and choose File, New, Text Document.  Name it hello.cpp -- this will be your first program.  Hold the Shift key, then right-click on it, choose "Open with...", and open the file using Notepad.

Hello.cpp is about the simplest program there is, and it's the same program that almost every c++ programmer first learned.  I'll type it out here, and you type it into your hello.cpp file:

Now, click File, Save and close the window.  Get to your DOS Prompt and switch to your mycode directory.  Once there, type the following line at the command prompt:

gxx  -o  hello.exe  hello.cpp

For reference, you can also use this complier to compile C source files, using this syntax:

gcc cSource.c -o cProgram.exe -lm

For the purposes of this tutorial, you won't need to make use of this.

This compiles your source code file, hello.cpp, into the program hello.exe.  If the compiler gave you errors, go back and make sure you typed hello.cpp exactly as I did above.  If you have to make any changes, be sure to save the source file again and then recompile.  To run your program, just type hello at the command prompt.

That's it!  You just wrote, compiled and executed your first C++ program.  You're on your way to becoming a C++ master.  Ok, I'm getting carried away here.

But you are on your way now.  One final note: the source code file hello.cpp and the executeable program hello.exe are two independent files.  True, you need the first to get to the second, but once you've compiled it, you don't need the source code to run the program.  That means that you can email your program to all your friends and they can run it and they don't need the source code or the compiler or anything.  But, you should always save your source code files, because they will help you when you need to remember how to do a certain thing, or if you ever want to change a program.

Analysis:

Let's look at the different parts of this program and see what each part does.

Functions: main() and cout are functions.  A very general definition of a function is a piece of code that does something.  Whenever a function is started, it is because some other function called that function.  Here, Windows calls main(), and main later calls cout.  Some functions, like main(), return a value to the function that called it.  Here, main() returns a zero to Windows, which is what all programs do when they exit successfully.

Data types: int is a data type.  It stands for integer, meaning whole number.  Here, int is used to say that the value returned by main will be an integer (here, a zero... if you type return 0.1, or return 19.00387, when you've already said that the function will return an int, it won't compile).  There are other data types, including float and double (numbers with decimal parts), char (characters), and you can even create your own data types, called classes.  Classes are what make C++ so powerful.

Preprocessor directives: #include <filename.h> is a preprocessor directive.  These are used to provide definitions for functions.  For example, the definition of main consists of the statements inside the curly braces ( { } ) that follow it.  Thus, main is a user-defined function.  But we didn't define cout, so the computer doesn't know what we mean when we type cout... unless we #include the file iostream.h.  That's where the definition of cout is typed, in the file iostream.h.  Such a function, that is defined in a library like iostream.h, is called a library function.  ...  Anyway, cout stands for "console out", and it simply outputs things to the screen.  Here, we've outputted a string of text to the screen.

 


more code on the way...