Links

Home
Open Source
World News
About W3 Systems
References
C++ Reference
CSS
MySQL
SEO
Meta-Site
C++ Tutorial
Quirks Mode
Bug Collection
Detecting The World
Free CSharp
Javascript Reference
Meta-Site
ASCII Table
Network Engineering
DHTML DOM
SSL
Staff
Rainbow Custom Logo Mats
Primes
Spellchecking
PDF Conversion
Sabbaticals
XML
Health Sharing
Household Rules for
daughters male visitors

C++ Tutorial


The basics of C and C++ are pretty easy to learn.  C and C++ are compiled languages.  C++ is nearly a superset of C.  I'll concentrate on C++.  Free compilers are available for C++ on all platforms.  C++ is everywhere. 
Linux:
Windows
Jumping in head first, here is a no-op C++ program:


lesson1.cpp


int main()
{
  // I don't do anything...

  /*
    no, not anything, at all.
  */
}

The program comments say it all; this program doesn't do anything, but it does compile! And it compiles without errors or warnings.  It also illustrates the general structure of a C++ program. 

How to compile the thing?  Assuming Linux and g++, (the C++ compiler flavor of gcc from gnu), at the command line you would just type:
g++ lesson1.cpp

at the command line.  The result, in notoriously POSIX fashion, is the cryptically named executable a.out.

If you want to instead name the file something more reasonable, like lesson1, you would type
g++ lesson1.cpp -o lesson1

Getting back to the program code, both types of C++ comments are shown.
As a final comment on lesson 1, I should point out that nothing differentiates the C++ code in lesson 1 from straight C code.  That's about to change with lesson 2. 

You are qualified!  If you have transcribed the lesson 1 code and compiled it, feel free to put down C++ as one of your skills on your resume.  (Just kidding! Theres a little more work to do.)


lesson2.cpp

// lesson2.cpp

#include <iostream>
#include <iomanip>

// very important unless you like to type a lot
using namespace std;

int main( int ac, char ** av)
{
  int i;
  cout << "ASCII TABLE" << endl;
  for( i=0; i<256; i++)
  {
   
    cout << "  int ["   << dec  << ((int) i)   << "]";
    cout  << "  hex ["  << hex 
   << ((int) i) << "]" << " = [";
    if( i != 27 )
    {
      cout << ((char) i);
    }
    else
    {
      cout << "the escape character!";
    }          
    cout << "]" << endl;   
  }
  return 0;
}

Lesson 2 actually does something useful. 
It prints out the ascii character set from 0 to 255.
Lesson 2 introduces the concept of streams.


To compile lesson 2, lets change the command a little to:
g++ -pedantic lesson2.cpp -o lesson2

Can you guess what the -pedantic does?  Pedantic is like -WAll, detailed warnings about stuff in your code.  While your learning, (and even when your not, it's a good idea to include  -pedantic or -Wall in your compile statement.  Now, starting from the top...

At the top of the file is an ANSI C comment line:

// lesson2.cpp

The compiler ignores anything between the // sequence and a newline character.

The next significant lines in the program are:

#include <iostream>
#include <iomanip>

These are interpreted by the C++ pre-processor.  Whats a preprocessor?  C++ is compiled in multiple passes (at least two.)  The preprocessor is called first to fixup the program code in the ways specified by pre-processor directives.  '#include' is one such.  It tells the pre-processor to go find the requested file and add it to the program code at the point of the directive statement. 

iostream and iomanip are two include files that have definitions of the io streams and io manipulators that are nearly vital to C++.  Lesson 2 has examples of the use of these.

Next comes the following:

// very important unless you like to type a lot
using namespace std;

The 'using namespace' directive tells the compiler that you want to reference class and entitiy names directly without having to prefix everything with the entities namespace.  The C++ standard library, including streams and templates is in the namespace 'std' (for 'standard, not sexually transmitted diseases!)  Namespaces were added to the C++ standard in order to prevent the 'name collision' that results when using different libraries that may use the same class names for different classes.

int main( int ac, char ** av)

This is the entry point to the mandatory main function.  As written in lesson one, there are alternative argument lists to main.  This is the fairly typical one.

int i;

Declare an instance of i, of type integer.
One of the native types, ex:( int, short, long long long, float, double, long double, char, wchar).

Now for the magic:

cout << "ASCII TABLE" << endl;

No other language has this symantic concept (maybe 'D'?). 
There are three standard streams for all C++ programs...
These are opened for you automatically.
The << characters sequence is the stream output operator.
It outputs the character string "ASCII TABLE" to standard output. 
The 'endl' thing is a 'stream manipulator'. 
It puts the proper end of line termination into the stream.  On MSDOS, OS/2  and Windows, the proper end of line is 'CRLF'.  On Unix and Linux, it is 'LF'.

Streams make for a lightweight  way of moving stuff to and from 'files'.  In Unix/Linux standard input and output and error are open files to the console (typically).

  for( i=0; i<256; i++)
  {
  
  }

This construct is a 'for loop'.  It iterates through a sequence until a condition is no longer true.
The for statement is really three statements...

for( i=0;  i<256; i++)  The highlighted initializes things. It's executed at the start of the iteration.

for( i=0;  i<256; i++) The highlighted is the test condition.
If false, the program falls out of the loop; after the closing brace. 
The 'test' is executed before statements in the braces.

for( i=0;  i<256; i++) The highlighted increments the variable i by 1. 
It's executed last, after statements in the braces.



More coming later...
Copyright (C) 2008 - W3 Systems Design