A First Application

 

"I saw 'cout' being shifted 'Hello world!' times to the left and stopped right there..."

 
 --Steve Gonedes, 2000

The first program in a new language should always be "Hello world!".

 1 
 2   // Hello World Application
 3   #include <iostream.h>1
 4   
 5   int main()2
 6   {
 7     cout << "Hello world!\n";3
 8     return 0; 4
 9   }
10 

1

The iostream.h header file is required for the cout call. The #include is a C++ preprocessor directive [6] , that causes the C++ preprocessor at compile time to substitute the contents of the specified file into your program at that location. Using the #include directive allows you to import libraries of code into your application as required. The size of your application will increase by the size of each header file included, but the inclusion of iostream.h is necessary to perform any input/output calls, such as printing to the screen in C++ (we will discuss this later).

2

The main() function is the starting point for all command line C and C++ applications. The default return type in C++ is int, meaning that this function should return a whole number.

3

The cout call sends the "Hello World!" string to the standard output stream, usually the screen. The << operator evaluates the parameter that follows it and places it on the output stream. To end a line of output you can use << "\n" or << endl.

4

The return 0 statement tells this function to return a value of 0 as the whole number to the calling code. This statement is not necessary as a value of 0 will be returned from the main() function by default.

The source for this example is in HelloWorld.cpp.

You might think that this is the shortest C++ program that you can have - It is not! The shortest valid C++ program is:

  main()
  {
	
  }

There is no need for libraries, as there is no input/output, there is no need to specify a return type for the main() function as it defaults to a return type of int and an empty method is a valid method. This shows how flexible C++ is in assuming default states and values. This is also one of C++'s greatest weaknesses!

Setting up a compiler

 

"C++ : an octopus made by nailing extra legs onto a dog"

 
 --Unknown

There are several C++ compilers that you can use for this course. Once the compiler is ANSI compliant there should be no issue in using it with this module as we just require a standard non-windowing compiler. A Unix/Linux compiler will also work. The current recommended compilers are Borland C++ compiler 5.5.1 and Bloodshed Dev C++.

Borland C++ Command Line Compiler

The company blurb: The Borland C++ 5.5 Compiler is the high performance foundation and core technology of Inprise/Borland's award-winning Borland C++Builder product line and is the basis for Inprise/Borland's recently announced C++Builder(TM) 5 development system for Windows 95, 98, NT, Windows 2000 and Windows XP. The Borland C++ 5.5 Compiler and associated command line tools, is now available for free download from the Borland Web Site at the address Borland Free Compiler. To make it easier and to avoid filling in the forms I have made the compiler available locally here - BorlandCppCompiler.exe (8.52MB)

The free download includes Borland C++ Command Line Tools:

  • Borland C++ Compiler v5.5 (bcc32)

  • Borland Turbo Incremental Linker (tlink32)

  • Borland Resource Compiler / Binder (brc32, brcc32)

  • C++ Win32 Preprocessor (cpp32)

  • ANSI/OEM character set file conversion utility (fconvert)

  • Import Definitions utility to provide information about DLLs (impdef)

  • Import Library utility to create import libraries from DLLs (implib)

  • Borland Turbo Dump to structurally analyse EXE, OBJ and LIB files (tdump)

  • Librarian for symbol case-conversion, creating extended libraries and modifying page size (tlib)

Included Libraries:

  • Borland C/C++ Runtime Library

  • ANSI/ISO Standard Template Library (STL)

Here are the steps to install the Borland C++ compiler on your PC.

  • Download the installation from here - BorlandCppCompiler.exe (8.52MB). You will receive a warning line in Figure 2.1, “Borland Compiler Download Dialog Warning”. Choose "Save" and save it to a temporary directory on your machine.

    Figure 2.1. Borland Compiler Download Dialog Warning

    Borland Compiler Download Dialog Warning

  • Browse to the temporary file and execute it. It will install the Borland compiler in a suitable directory. It will suggest c:\borland\bccx.x.x. I choose c:\program files\bcc551\ to keep my machine neat. So all points from here assume that you have installed in the same directory as me. If you have not then substitute your path in for mine.

  • Download the file HelloWorld.cpp to a directory such as c:\Temp. You can do this by right-clicking the link and choosing "Save Target As..." and then entering c:\Temp\HelloWorld.cpp

  • Change your path environment variable to include the compiler executables directory. In my case this is C:\Program Files\bcc551\Bin. To do this go to the "Control Panel" under your start menu and open up "System". Under "System" there is an "Advanced" tab. Choose this tab and press "Environment Variables". Double-click Path as in Figure 2.2, “Setting the Path environment variable.” and add in the C:\Program Files\bcc551\Bin with a ";" before to separate the path.

    Figure 2.2. Setting the Path environment variable.

    Setting the Path environment variable.

  • Next you must create a file in your bin directory (in my case c:\program files\bcc551\bin) called bcc32.cfg that only contains:

        -I"C:\Program Files\bcc551\include"
        -L"C:\Program Files\bcc551\lib;C:\Program Files\bcc551\lib\PSDK"
    	

    and another one called ilink32.cfg that only contains:

        -L"C:\Program Files\bcc551\lib;C:\Program Files\bcc551\lib\PSDK"
    	

    This sets up your compiler environment to always find the include files and library files. Please be careful if you are using notepad to edit these files that you do not allow notepad to append a .txt file extension to these files. You should either put " " around the filename, e.g. "bcc32.cfg" or under "save as" you should set the "save as type" to "all files".

  • Finally check that your compiler works by typing Bcc32 HelloWorld.cpp in the C:\Temp directory as in Figure 2.3, “The Hello World Application in Action.”

    Figure 2.3. The Hello World Application in Action.

    The Hello World Application in Action.

    and then execute it by typing HelloWorld in the same directory.

Bloodshed Dev C++

The Bloodshed Dev C++ compiler is available at: http://www.bloodshed.net/devcpp.html. It is is a full-featured Integrated Development Environment (IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU Compiler Collection) as its compiler. Dev-C++ can also be used in combination with Cygwin or any other GCC based compiler. Install version 5 from the: http://www.bloodshed.net/dev/devcpp.html page through Sourceforge. This compiler requires Windows 95 or higher.

Figure 2.4. The Dev C++ Integrated Development Environment (IDE)

The Dev C++ Integrated Development Environment (IDE)

Codeblocks (Use in 2012/13)

The Codeblocks IDE is available at: http://www.codeblocks.org/. It is is a full-featured Integrated Development Environment (IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU Compiler Collection) as its compiler. There is a version of Codeblocks for Linux and Mac as well as for Windows.

Figure 2.5. The Codeblocks Integrated Development Environment (IDE)

The Codeblocks Integrated Development Environment (IDE)



[6] Preprocessor directives are orders for the preprocessor, not for the program itself. They must be specified in a single line of code and should not end with a semicolon.