Introduction to C++


First C++ program


The following C++ program will print text Hello, World! on computer screen:

#include <iostream>
void main()
{
    std::cout << "Hello, World!";
}

Our first program is constructed from the following parts:

#include <iostream> is a directive to include keyboard and screen input/output functionality to our program. Without this directive, the program would have no means to print "Hello, World!" greeting on the screen.

Second line contains main entry point into our program. That is, when the program is loaded into memory, its execution begins here. The word "main" is the name of C++ function. C++ functions define computer operations. Entire combination "void main()" suggests two things: first, this is a function with no input parameters, because parentheses are empty. And second, keyword "void" declares that main function does not return any results back to the operating system.

Opening brace "{" indicates beginning of our function. Closing brace "}" ends the function. The braces require no special calculation and specify function body.

Statement std::cout << "Hello, World!"; sends quoted string of characters to console output device, namely cout. Because cout is an object that comes from C++ standard library, it is prefixed by the namespace of the standard library, std:: . Our program communicates with cout by using operator << in order to print text on computer screen.

Finally, the statement contains semicolon ";" at the end. This is part of C++ syntax: individual statements must finish with semicolons. Interestingly, semicolon is part of the statement itself, and is not a separator between multiple statements.


Creating first Microsoft VC++ 2005 project


Many free compilers exist today. Among them, Microsoft Visual C++ 2005 Development environment includes an editor with program code colorization, syntax checking, on-line help, and other tools. It enables to find and diagnose programming errors using program debugger with graphical user interface. In addition, Visual C++ allows writing portable, standards-conformant C++ programs that use advanced features of the language, based on concepts of object-oriented design and generic programming.

Combination of these qualities makes Microsoft Visual C++ an attractive choice for many novice programmers and professional developers. For developing highly portable code, it is crucial to compile your programs by as many different compilers as possible, and VC++ is certainly one of them.

Like many other graphical development environments, VC++ comes with its own terminology for configuration parameters associated with programming tasks. To begin using Microsoft Visual C++ in our homework assignments, we need to create a new project in Microsoft development environment. Essentially, project is a combination of configuration files, located in the project directory on the hard drive. Each project is placed inside a solution , which allows loading multiple projects into the VC++ for parallel work. When we create our first VC++ project, the solution will be automatically created for us.


Steps to compile and run a C++ program from the command line


  1. Open Microsoft Visual C++ 2005 development environment. From the top menu, select File -> New -> Project...

    When "New Project" dialog opens, specify the following parameters:

    Name:

    main

    Location:

    C:\CIS60\Projects

    Uncheck "Create directory for solution" check box.

    Click OK.


  2. Project Wizard dialog comes up:



    • Click "Next>".
    • Select Win32 Console application project for the project type
    • uncheck Precompiled header check box option
    • check Empty project check box: option
    • click Finish.




  3. In the "Solution Explorer" sidebar, right-click the "Source Files" and select Add -> New Item...

    The dilaog box opens:

    Click "Visual C++" on the list of Categories on the left, click "C++ File (.cpp)" on the list of templates, type main in the Name box.

    Click "Add" button.


  4. main.cpp opens as an empty source file in the editor window. Type text of the "Hello, World!" program:

    #include <iostream>
    void main()
    {
        std::cout << "Hello, World!";
    }
    



  5. From the top menu, select Build -> Build Solution, or simply hit F7 key to compile the program. You should see the output window:

    If there were any errors, make sure you have created the project of the right type and that you typed the program text exactly as above.


  6. You should now be able to switch back to the command window and execute your program by running command:

    C:\CIS60\Projects\main\Debug\main.exe

    which should print "Hello, World!" greeting on the screen.


Our directory structure now includes a subdirectory for Visual C++ project files and the final directory tree looks like this:

                        C:\
                         |
                         '-CIS60
                              |
                              '-Projects
                                  |
                                  `-main
                                     |
                                     +-Debug
                                     |   |
                                     |   `-main.exe   (compiled executable file)
                                     |
                                     `-main.cpp       (program source file)