PEG:C++ Lesson 1 for Pascal Users

From PEGWiki
Revision as of 00:04, 15 November 2009 by Brian (Talk | contribs) (Created page with 'This is the first lesson in a series to help Pascal users make the transition to C++. We will be using the [http://www.microsoft.com/express/vc/ Microsoft Visual C++ IDE]. Note t…')

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This is the first lesson in a series to help Pascal users make the transition to C++. We will be using the Microsoft Visual C++ IDE. Note that whenever some menu option or key combo is mentioned, it refers to Visual C++, but anything about the C++ language itself will work on any reasonable compiler (the other major compiler being g++, which is used on the Judge). You will be addressed, when necessary, as though you use the Visual C++ IDE on Windows. Download and install Visual C++, and let's get started!

The first thing you'll notice is that setting up Visual C++ to compile and run programs is considerably more complex than using the Turbo Pascal or Free Pascal IDEs. Follow the given instructions carefully, though, and everything should work fine; after doing this a number of times you should become accustomed to it.

Visual C++ is designed to make it easy to write large projects. You will not need to write large projects for the purposes of PEG; most of the programs you write will fit into a single file. But the fact that it is well-suited for large projects provides a rationale for the complexity. You do not compile individual files in Visual C++; you create a project (each project goes in its own folder, so a new folder is created whose filename is the same as that of the project) and all files associated with that project should be stored in the project's folder, including that single C++ source file. You build the project to produce an executable. The IDE needs to be told that some file inside the project's folder is actually a C++ source file (and not something else, such as a test case file) and that when you build the project you want that source file to be compiled.

When you start the IDE, close any of the annoying dialogue boxen that pop up at the start, and go to File -> New -> Project. Select Empty Project, type a name for the project in the corresponding box, and save your project in some nice, empty directory that you'll use for your C++ programs. For example, you might create a "C++ Programs" subdirectory of My Documents, then the path to your project might be "My Documents/C++ Programs/My First Visual C++ Project", and the path to your source file might be "My Documents/C++ Programs/My First Visual C++ Project/hello.cpp". Now, go to Project -> Add New Item, and select C++ File (under the Code category). You should finally be able to type text in the large pane to the right; this is where you type your C++ code. Save this file as "hello.cpp", making sure that is in the directory of your project. Remember, for every program you write, you need to create a separate project that goes into a different subdirectory of "C++ Programs".

There are two annoying points that are easy to forget. First, if you want to go back and work on a project that you previously started, you must open the .sln file, not the .cpp file. This is because the .cpp file contains only your C++ code itself, whereas the .sln file contains metadata that tell Visual C++ the exact environment of your project. Second, if you have a program open, and you want to open a different one, rather than just closing the .cpp file you need to close the entire "Solution" (project). This can be done by going to File -> Close Solution. Otherwise, the IDE will have no idea what to do with the new file you create.

Here is an example program which can be copied directly into the IDE. Note that on a given line, everything to the right of "//" is a comment (which will be ignored by the compiler):

#include <iostream>
using namespace std;                   // these top 2 lines are kind of like "uses crt",
                                       //  we'll describe them more later
int main()                             // this is the main program
{                                      // in C++, an opening brace is the same as "begin"
    cout << "Hello Fox" << endl;       // this is the main way to output in C++, we'll
                                       //  describe this more later
    return(0);                         // this stops the program, you should have it as
                                       //  your last line of code (though the program will run without it)
}                                      // the same as "end"

To compile this program, use the Build functionality (F7). After building, pressing Ctrl-F5 runs your program. A console window should pop up, your program's output should appear in the window, and the message "Press any key to continue..." should appear at the end (it is not part of the output). Press (almost) any key and the window will close, letting you go back to editing your program.