A First Application


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 which is attached to the bottom of this page.

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 Eclipse CDT and Codeblocks. 

- If you have not programmed in C++ before I would recommend Codeblocks

- If you have programmed in C++ before I would recommend Eclipse CDT


Codeblocks

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. If you wish you can use the 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.

Download the version with the name something like "codeblocks-12.11mingw-setup.exe" where you also install the MINGW compiler. If you have previously installed the compiler there is no need to do that.

Figure 1. The Codeblocks Integrated Development Environment (IDE)



Eclipse CDT

"The CDT Project provides a fully functional C and C++ Integrated Development Environment based on the Eclipse platform. Features include: support for project creation and managed build for various toolchains, standard make build, source navigation, various source knowledge tools, such as type hierarchy, call graph, include browser, macro definition browser, code editor with syntax highlighting, folding and hyperlink navigation, source code refactoring and code generation, visual debugging tools, including memory, registers, and disassembly viewers." - http://www.eclipse.org/cdt/


Figure 2. The Eclipse CDT 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.


Subpage Listing



These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way.
ċ
HelloWorld.cpp
(0k)
Derek Molloy,
29 Sep 2013, 07:12
Comments