We start by learning how to write and run a simple example program to compute compound interest. First we will need to install and configure the software required to write C++ programs. Next we will see how to write a simple program.
1.3 Compiling and running the code
Running C++ code isnât as easy as you might like.
The reason for this is that C++ is what is called a âcompiled languageâ.
The chip inside a computer that does most of the work is called the CPU (central processing unit). It does not know the C++ language or indeed any computer language which is pleasant to program in. The CPU speaks a language called assembly language, also known as machine code. In machine code, all instructions are coded up as a sequence of numbers. Each number is a code word for some action the CPU should take. Programming in machine code directly is completely unbearable. What is worse, different CPUs may use different versions of assembly language, so you have to rewrite your code for different computers.
To get around this, one programs in âhigher level languagesâ which are written in ways that humans can understand. At some point, the programâs instructions need to be converted to machine code.
In an âinterpretedâ language, the instructions are converted to machine code every time they are executed. MATLAB and Python are examples of interpreted languages.
In a âcompiledâ language, the instructions are converted to machine code before the program is ever run. This process is called compilation. C++ is a compiled language, so you must compile your code before you can run it.
Historically, the advantage of compiled languages was that they run faster. The reason for this is that converting things to machine code takes time. If you do this every time the code is run, it will necessarily run slower. The big disadvantage was that you have to recompile your code if you change the type of computer you want to run it on.
These days, computers are so fast that this advantage is not really relevant any more. Modern languages can be compiled very fast and even use âjust in time compilersâ that observe how the software is being used by the user and perform optimisations based on this. This is one of the reasons why the claim C++ is faster than languages such as Java and C# is a bit of a myth.
A good development allows you to compile and then run your code at the touch of a button. But before we can do this, we need to get our code into our development environment.
We will now describe the steps you need to go through to compile the example code. Jump to the relevant section for your computer and follow the instructions to the letter. Note that a guide to compiling on Macs can be found on the website accompanying this book.
You will need to get everything exactly right. If you cannot get the code to work, there is a zip file called InterestCalculator on the website for this book. This contains working versions of the code that you can use.
1.3.1 Compiling on Windows
Open Visual Studio.
Select FileâNewâProject . . .
Select EmptyâProject
Enter the Name InterestCalculator and press OK.
Note the name of the folder where your project is being saved.
Notice that to the right of the screen you have an area marked Solution Explorer inside which there is a picture of a folder marked Source Files. Right click on this and select AddâNew item. . . .
Select the option âC++ fileâ and enter the name main.cpp and press Add.
This creates a file called main.cpp which we will use to store our code. On the right-hand side of the screen you will see a text editor window where you can edit the code for main.cpp.
Copy and paste the example code from the website4into the editor window.
Select ProjectâInterest Calculator Properties. . ., then select LinkerâSystem and set the SubSystem to Console (/SUBSYSTEM:CONSOLE) using the drop down.
Press OK
Press CTRL + F5 to compile and run your program
This should have worked if you have managed to follow every instruction exactly. If it fails, close Visual Studio, delete all the files in the directory you noted down and try again! But this time be more careful.
Setting up your first-ever project is probably the most fiddly and tedious task you will have to perform in this book.
Why are there so many steps to creating your project?
Firstly a typical C++ project contains a lot of different files, so in practice you donât normally run through such a complex process very often. Most of the steps above are only needed when you create a new project. The two steps that you would be likely to perform repeatedly are:
creating new C++ source files by right clicking on the Source Files folder;
pressing CTRL and F5 to compile and run your program.
The âprojectâ groups together all of your files and allows you to set in one place the configuration options for all your files. This is why it makes sense to have a âprojectâ as well as just the C++ files.
Secondly, you can write different types of programs on Windows. Most programs have Windows user interfaces, but very old fashioned programs have text input through the âconsoleâ. Console programs are easier to write, but not the default on Windows. So we have to tell Windows that is the kind of program we want. This is why we must set the SubSystem.
Let us examine all the files that have been created.
If you open Windows Explorer (by pressing the windows key and E) you should be able to browse to where your project has been saved and you will see that a lot of different files have been created.
Most of these are used internally by Visual Studio and so are of no interest. However, the following are interesting.