Programming Tutorial - Part 1

Programming for Jibber Jabber Fools

Well, one Jibber Jabber Fool in particular, but if you’re interested in learning how to program and you aren’t Reece, feel free to read along too!

Introduction to C++

 C++ is a programming language that’s been around for quite a while now.  It’s a straight superset of C (so all C programs and techniques work in C++ too), and like C it is only ever *compiled* (see Terms).

There are loads of languages out there – some easy to learn and some pretty difficult.  They’ve all been designed for different purposes, so why should you learn C++ rather than one of the others?  I can think of a few reasons:

* Speed.  C and C++ are both equally speedy.  They’ve been around for ages and their compilers are great at making them very, very efficient.  The language has stayed very close to the instructions that the computer uses internally (its machine code), so any compiler doesn’t have to do much work to produce something fast and efficient.

* Universally useful.  C++ compilers are available for just about every computer or processor made in the last 10 years or so.  Everything can be programmed using it, and everyone wants a good C/C++ programmer.

* Structure.  A nice balance of low-level commands and some high-level structure.  You can make things that are easy to read and maintain without losing the speed.

* Versatility.  Many languages have limitations.  They can only run in a window, or they can’t run quickly enough to make a game, or they can’t connect to the internet, or they can’t use certain formats of graphics.  C and C++ can be used to write *anything* that runs directly on a computer (rather than in a web browser).  Operating systems, applications, music players, games - *anything* - and anything you’ve seen your PC do really quickly can be done with C/C++ at exactly that speed (if you know how).  No limitations.

 Technical Terms for Starters

 C – The original.  All C commands were kept for C++, and C++ programs can use C whenever they want to.

 C++ - The New Batch.  The main addition for C++ is the idea of Object Orientation.  This is far too scary to discuss here, so we’ll leave it for now…

 Source file – a text file containing part of your program.  In C++ they have to have a .cpp extension.  “.c” is a straight C file (with none of the extra C++ bells’n’whistles).  Source files contain the actual Work that your program does in the form of Functions and Global Data, but they can't be run on a computer - they need to be compiled first.

 Function - A block of a program with a name that Does something.  A function can be used once or many times within a program.  All instructions have to be within functions.

 Global Data - Data that's accessible by all of a program's Source Files. Could be a game's map, its graphic, the level number the player is on etc.

 Compiler – A program that turns a source file into an "object" file.  It does this by going over the source file a number of times, finally producing the object file.  A compiled file has to be *linked* before you can run it (see Linker)

 Object file – You can’t actually run a Source file on your computer.  First it has to get turned into an Object file.  This is much closer to a format the computer can run (but still not quite ready…)

 Linker – The linker is a program that runs after the compiler.  It takes all the Object files that the compiler has made and sticks them together by linking together all the loose ends - joining together all the function calls with the functions that they call.  It spits out an Executable ready to run.

 Header file – a file that tells Source files what other Source files are up to.  It doesn’t have any actual code in it – just information about what’s what.  You write this yourself in a text editor, just like source files, and describe the functions and global data that you'd like to use in one source file from another.  As Reece says : "It's just like an index!"

 Visual Studio – A program that lets you write and organise C++ programs.  Microsoft has kindly made a very good version available for free and it includes a wonderful debugger.

 Project file – A collection of Source files and Header files.  Visual Studio runs through all the Source files and puts them through the compiler.  Then it pops the whole lot through the Linker.

Some of these things seem a bit advanced or obscure, but you can come across them all very early on in your C++ career...  For example, your very first error messages will come from either the Compiler or the Linker, and if you don't know what these things do you won't know why they're sad. 

 Making a C++ program - A summary

There’s no way to just Run some C++ without a whole lot of faff – there’s no “Immediate mode” or anything like that.  All your code goes into Source files, each source file is compiled, and then they are all linked together into an executable.  Code in one source file can call functions in any of the others – it’s the Linker’s job to sort it all out at the end.  When the program is first launched, a function called “main” is always the first to run - after that it's up to you! 

 Over in Part 2 we'll actually make a Real Program!  Wohoo!


Digg! Reddit! Del.icio.us! Mixx! Google! Live! Facebook! StumbleUpon! TwitThis Joomla Free PHP
Add this page to your social bookmarking websites
Last Updated on Thursday, 21 August 2008 21:11