
C++, COBOL, Pascal, BASIC, and other programming languages are English-like; a computer is an electro-magnetic machine that doesn't directly understand either English or English-like programming languages. In order for a computer to "execute" or "run" a program, the program must be translated into the electro-magnetic switches that the computer "understands." There are several ways of doing this, including assembling, interpreting, and compiling.
Assembly languages are formed by one English-like code for every instruction of the machine's native language, "machine code." Translation of a program written in an assembly language into machine code is called "assembling" the program. Because assembly language instructions map (via assembling) directly to machine instructions, the resulting machine code is likely to be extremely efficient -- runs fast and conserves memory. These are the primary advantages of assembly- language programming. Disadvantages: it tends to be hard to work with, and, since every model of computer has its own version of assembly language, programs aren't "portable" -- they must be rewritten in order to run on other models of computers. For example, a program written in IBM PC assembly language should run on all IBM PC-compatible computers, but not on Macintoshes.
"High-level languages" such as C++, COBOL, etc., tend to be easier for programmers to work with than assembly languages since an instruction in a high-level language typically is the equivalent of several assembly or machine language instructions. High-level language programs are generally either interpreted or compiled into machine code.
Many beginning students of college-level computer programming know BASIC (not Visual Basic), which is usually interpreted. When an interpreted program runs and control within the program reaches a given instruction (such as a LET statement), the interpreter translates the instruction into machine code and executes the corresponding machine instructions, then goes on to the next instruction of the program WITHOUT REMEMBERING the machine code it has just finished executing. Thus, if an instruction is repeated, it must be re-translated to machine code before it can be re-executed.
By contrast, COBOL and C++ are usually compiled. The compiler translates the program IN ITS ENTIRETY to machine code, and "remembers" all the machine code by saving it in an "executable" file, before the program is run. Thus, when the program is run, translation of source code instructions to machine code has been done in advance; repetition of a source code instruction does not require re-translation of the source code instruction to machine code; hence the program tends to run faster. On some computers where a language can be both compiled and interpreted, the compiled version will run enormously faster -- it's not unusual for the parts of the program not requiring input or output to require 1000 times as much time in the interpreted as in the compiled version.
OK, this clearly indicates that an important advantage of compiling as opposed to interpreting is speed of execution. What could a disadvantage of compiling be? Well, remember we said above that one high-level language instruction is typically equivalent to several machine code instructions. Suppose you are running a program in, say, BASIC, that has 500 BASIC instructions, the average of which is equivalent to 5 machine code instructions and the largest of which is equivalent to 10 machine code instructions. If compiled, this would mean you would need enough memory to hold 5*500 = 2500 machine code instructions in order to run the program; if interpreted, you need enough memory to hold 500 BASIC instructions plus at most 10 machine code instructions (for whatever BASIC instruction is currently executing). Thus, interpreting uses less memory. This was an extremely important factor for the early microcomputers -- machines with small memories -- and probably explains why they typically came equipped with interpreted BASIC.
A compiler typically scans your source code file for "syntax errors," i.e., errors in the use of the programming language's rules concerning formation of statements in the program. Frequently, one actual error will generate several error messages, as the first error may "confuse" the poor compiler. Hence, the first error message is significant; others may or may not be.
If the compiler does not find "serious" syntax errors in your program, it will also typically (the details differ from one compiler to another) produce a file that is either the "executable" version of your program or an almost-executable (it may be an "object" file that must be "linked" to produce an executable file). The executable file is the machine code translation of your source code that the computer uses to execute your program when you give the command to do so. Students should note that the goal of the process is not merely to have your program run. It should run correctly and well. It may be necessary for you to observe your program's behavior, edit its source code, compile again, and run again, many times, before you are satisfied with your program's performance.
On the other hand, if the compiler finds errors in your program, it is necessary for you to edit these errors out of your program (in the editor). You must then re-compile. The cycle of editing and compiling may have to be repeated several times before your program is ready to run. Notice also that you're probably not "done" when your program runs the first time - successful compiling does not guarantee either correct logic (necessary to get correct "answers") or nice input/output. It's the programmer's responsibility to check these matters.
To page index
To Boxer's home page
Once the Borland C++ software is set up on the computer you use, it seems much easier to use this software than Visual C++ (see below for comparison). In the following, I assume you are using the student disk that came with your textbook. This version of the software runs as a console application - that is, in a DOS window.
Note: Since you're now working in a DOS environment, some DOS pathologies come into play. One of these is that DOS, unlike Windows, has an 8-character maximum for file and folder/directory names. Since you may have named your folder in Windows without adhering to such a restriction, if your folder has a name longer than 8 characters, use its first 6 characters followed by "~1". For example, if your folder name is CIS265ProjectA, you can change to that directory via the command
DOS also is unlike Windows in not permitting blanks in file or folder names. Thus, if you wish to change to the directory named CPP Projects, contract the name into one word (removing the blank) and use
To page index
To Boxer's home page
The Visual C++ software on the CD that came with our textbook doesn't seem to be accompanied by documentation (online or otherwise) that explains how one prepares a program for usage. Here's an outline of some steps that prove useful for the program used as Project 0 of CIS 265 during the Fall, 2000, semester. Thanks to David Basamania for showing me some of the following.
Some of the steps described below can be omitted if your program has sufficiently simple structure (e.g., only one source code file, no input file, no output file). However, if you use more than one source code file, or an input file, or an output file, what follows may be the best way to get your program working.
It seems a complex (in the sense described above) program must be organized into a "project." This is done as follows:
The program is compiled as follows:
Once you have successfully built an executable file, you can run or execute your program by
If your program creates an output file (in the current example, bill), you should find it in the project folder (after the program has run).
To page index
To Boxer's home page
When you run your program, you may want a hardcopy of what appears in the i/o window (for example, to prepare your submission for a grade). This may be done by copying and pasting the i/o data to a word processor (such as Word) or a text editor (such as Notepad), then printing. (Notepad does a better job for many purposes, since, like the i/o window, it shows all characters with the same width, hence can demonstrate better than Word that columns of data are properly aligned.)
Windows XP
|
Windows 98
|
(Strange how the "progress" of a transition from Windows 98 to Windows XP results in a more complex process.)
It will often be the case that your i/o more than fills the console window. In this case, you should arrange that your program pauses often enough that you can perform the above several times, for portions of the program's i/o.
When you have completing copying your data, you may print from Notepad or Word.
To page index
To Boxer's home page
The Gnu compilers have been upgraded, and the commands one uses with them are different than in the past. The following information was supplied by Prof. Ann Rensel. In the following, the "%" represents the operating system's prompt message (which may be different than "%"); what's underlined is the command you would enter.
If you have a project with a single source file (say, "main.c"), you would compile it with
Use ".cpp" as the extension of the file containing the "main" function. In the following, we use the example of a source code file containing "main" with the file name "reciprocal.cpp".
For C++ the compiler is called "g++" - so if you had a program "reciprocal.cpp" then you would compile it by the command (this is the option students are less likely to prefer)
The -c option tell g++ to compile the program to an object file only; if you left it out g++ will attempt to link the program to produce an executable, whose name defaults to "a.out" (without quotation marks). To create an executable file from the object file, you would use
When the program compiles successfully (via either of the methods described above), you would run the executable with