Programming in C++
Dontae M. Grose II
Publishing Date: August 20 th , 2017
Table of Contents
Chapter 1: Input, Output, and Information Page 4
1.1) The Programming Environment Page 4
1.2) Program Structure and Layout .. Page 8
1.3) Output and Inpu t ...... Page 11
1.4) Variables ..................................... Page 15
1.5) Mathematics and C++ .... Page 20
1.6) Arrays ...... Page 24
Chapter 2: Data and Structure ................. Page 30
2.1) Function Declaration ................... Page 30
2.2) Variable Scope and Prototyping .. Page 36
2.3) Memory Addresses andPointer s ..... Page 42
2.4) Classes I ...................................... Page 44
2.5) Classes II (Classes with Functions) . Page 46
Chapter 3: Algorithms and Logic ........ Page 49
3.1) The If and If-Else statements .. Page 49
3.2) Switches ...................................... Page 53
3.3) Loops I (The While Loop and For Loop) Page 56
Chapter 4: Files, Namespaces, and Libraries ......... Page 62
4.1) Namespaces and Libraries ......... Page 62
4.2) File Reading and Manipulation ... Page 64
Answer Key ......... Page 67
Chapter 1
Chapter 1.1
The Programming Environment
To begin programming in c++ we need to first download the graphical user interface (GUI) that we will be using to write our code in. The best programming environment for c++ is called Codeblocks and can be downloaded for free. While there are other programming environments you may also use, Codeblocks will be the easiest to learn at first and create for an easier transition to other GUI and software. One of the largest environments used to program is Microsoft Visual Studio which is for experienced users and once you learn the foundation of programming you should feel free to download this software and use that instead of CodeBlocks.
Graphical User interfaces serve as the main component in writing any computer code. Because GUI is the way data is represented to users, giving programmers a platform to code on as well as visually see projects. CodeBlocks is a free graphical user interface that we will be using throughout this book because everyone can download this software and because it is in my opinion the best free graphical user interface. Ensuring that you know how to use this software because when issues arrive this knowledge will become primary in problem solving. As mentioned earlier you do not have to use Codeblocks but I would high recommend it to anyone who is completely new to programming. When downloading CodeBlocks we need to make sure that we are downloading the correct version for the correct operating system that you are currently running. This is huge as Codeblocks can be a very touchy piece of software, again, just take some time to familiarize with the software before entering the code of chapter 1.
Another thing to note when downloading CodeBlocks is that we need to run over code and test it. We also need something that will do this for us and that is called the compiler. The compiler that CodeBlocks uses its built into the software which is a huge plus, but we should make sure that we click on the correct download link that includes this compiler. The download link will have the words mingw in the link somewhere and will include the GNU GCC Compiler that we need to run our code. Going through the installation of CodeBlocks make sure to read instructions carefully and you will be asked again what compiler you want to use during the installation process; In which you will of course choose the GNU GCC Compiler. Once you are done installing the program you will get the home screen of CodeBlocks where we can now begin to write our code.
From the home screen of CodeBlocks click on the create a new project sub-link, next you will be presented with a dialog box that says New from template as the title of the window. From here click Projects on the left side of the dialog box and then console application on the windows to the right. You will then be asked if you are programming in C or C++ and while you know we are using C++, C isnt that much different and if you want to learn that as well the transition from C++ to C will be very easy to do. Next, we simply need to name our project and pick that place for our project folder to be saved. Lastly, we choose our GNU GCC Compiler, make sure the Create Dialog Configuration box is checked and that the Create release configuration, is unchecked.
Once you click finish you are finally done setting up CodeBlocks and now have an environment to program and code as we wish. But we still should go over all the different buttons that are everywhere, which I will be glad to tell you that most of the features, most of which will not be used, but will later become apparent of their use. Later if you decide to continue to use CodeBlocks you will see that there are tons of plugins for different types of programming that can be done. For now, all we will use are the management tab on the left side of the software, if you do not see the management tab on the left side of the screen press Shift+F2 and that will open that management tab, we will also need to be familiar with the file and build tabs on the top of the Codeblocks menu. File is where we will go to save, open, and close CodeBlocks and build is where will tell Codeblocks to compile and run our code. For example, Going to File > new > Project, will bring us back to the original screen of CodeBlocks where we can create a different project. You can have several projects open at once in CodeBlocks which is the reason for the management tab on the left side of the screen.
Navigating to the build tab you will see the dropdown list that allows us to run and build or project using the compiler that we have previously installed. When we are running our programs, we will be using the build and run button (Shift F9) to build our program (resend it to the compiler) and run the program, which outs the program result. If you look down onto the code editor you will see a bit of code already inserted into it. Navigate to build and run, and run this program to see its result, and make sure you have everything installed correctly. You will get the console window that most of our code will appear in (until defined differently) saying Hello World by default. This is the first program that we have ran and the first program most programmers run when they are learning any programming language.
After you have navigated through CodeBlocks and are generally familiar with everything, navigate to the folder where your program was saved previously. If you cannot find this folder, save it again to a parent folder that will hold all your programs of somewhere that you can easily navigate. Open the folder that contains the program that you saved and you will notice that there are two folders within the program folder along with 3 other files. You should have a folder called bin and a folder called obj these folders are for the compiler and will be used later. You will also have the Project file used which is what you will click on when you are opening the project in CodeBlocks. You may see the CodeBlocks icon next to the project file and you can use this as a reference when you want to open a program that you have in a folder. Lastly there is the main source .cpp file which is a file that contains the c++ code. The reason of this file is to store the code if you want to use it somewhere else, are sending it to someone, and if you use a different software such as visual studio, down the road, you can easily transfer code back and forth from each platform. This brings us back into c++ being an object orientated programming language which allows for rearrangement and the reuse of functions that are previously created. Unlike some different languages which are proprietary to the program they are made for and must be highly modified to use in other programming environments.
Next page