1. What Does “Implement a Program” Mean?
As you know, our goal is to create a program a computer can execute. You should be familiar with the idea that you need to express programs using carefully constructed steps, and may have already tried your hand at creating such sequences. But even if the “programs” you’ve created are structured correctly for a computer to follow, in the end, a computer can’t really follow the steps you’ve written.
Why?
Why can’t a computer “read” your steps and follow them?
Because computers don’t understand English instructions — or instructions expressed in any other human language, for that matter.
The only instructions a computer can follow are those written in machine language.
Every CPU has a limited set of instructions it can use to perform any task we ask of it when we run a program.
In addition, a CPU understands these instructions only if they are written in the binary (1s and 0s) form it expects.
These instructions written in binary form are called machine language.
An executable file (one with an .exe extension in a Windows environment) contains instructions in machine language form which the CPU steps through when we run a program.
We haven’t been using the CPU’s instructions, and our steps are not in the expected binary form either. So now the question is: how can human-language instructions be turned into an executable file of machine language instructions?
Maybe we could write our algorithm’s steps in machine language ourselves? We would need to learn the CPU’s instructions and how they look when written using only 0s and 1s. For example, maybe the CPU has an instruction named “add” which is written “10110”. If we took this approach, it could take a long time to write a program and also to find any mistakes. One 0 or 1 out of place and our program wouldn’t work. It would be nice if we could write the instructions in something a little closer to English. And we can…
We are going to write our programs in a programming language called C++ and then translate our programs into machine language. To accomplish this there are, at a high level, two steps.
Step 1 – Create a Source Code File
All of our algorithm’s instructions need to be written in the C++ language and saved in a text file. The text file we create is called a source code file. It is a file containing the code which will be used as the source for the machine language saved to the executable file (hence its name). C++ source code files end with a .cpp extension. The other sections of this guide discuss many aspects of writing C++ programs.
Programming languages are created to provide programmers with tools for writing programs which are more English-like than machine language. They are also created, however, with conversion to machine language in mind. Like the languages we use in our daily lives, rules must be followed to use C++ correctly. For example, in English we don’t end a sentence with a comma. A language’s rules for structuring statements are called its syntax. By following a programming language’s rules, our programs will be in a form capable of being translated to machine language. If a program we write breaks the rules, translation will not go smoothly. As we learn C++, we will learn its syntax.
Step 2 – Translate our Source Code File to Machine Language
After creating the source code file, we will use a program called a compiler to translate the file’s contents to machine language stored in an executable file. There are actually a few steps to creating an executable file that are performed when we compile a program:
When a compiler translates a program to machine language, it takes the instructions stored in a source code (.cpp) file, converts them to machine language, and stores the results in an object file. Object files contain the machine language version of a source code file’s instructions, but not in a form that the computer can execute. Object files are sometimes called intermediate files because they are the result of an intermediate step in the process of creating an executable file. In a Windows environment, an object file has the same name as the source code file but has an .obj extension. In the example above, the source code file program.cpp is translated by the compiler which produces the program.obj object file.
Many programming languages, including C++, come with tools programmers can use in their programs. Examples include tools for reading data typed by a user and for displaying data on the screen. These tools are available in a language’s library. The C++ library is called the C++ Standard Library.
Our C++ programs will frequently use tools from the C++ Standard Library. As a result, the final executable file will include the machine language version of our source code and items from the library. During the process of creating an executable file, the contents of the object file will be combined, or linked, with those the program uses from the library. Linking is performed by a program called a linker, initiated by the compiler, which creates the actual executable file (program.exe in the example).
To summarize, we will take the steps in a program, write them in C++, and save them to a source code file. We will then use a program called a compiler to translate our C++ code to machine language which will be used to create an executable file. The CPU will then be able to execute the instructions in the executable file to perform the task we wanted performed.
But wait… what DOES “implement a program” mean? The process of creating a source code file and a subsequent executable file is called implementation. When we begin with human-language steps and end with an executable file which contains the steps in a form the CPU can follow, what we have done is “implement a program”.