Mastering C++
A Step by Step Guide for the Beginner and Advanced User, Including 26 Quizzes and 120+ Questions
Copyright 2019 by Michael B. White - All rights reserved.
This book is geared towards providing precise and reliable information about the topic. This publication is sold with the idea that the publisher is not required to render any accounting, officially or otherwise, or any other qualified services. If further advice is necessary, contacting a legal and/or financial professional is recommended.
- From a Declaration of Principles that was accepted and approved equally by a Committee of the American Bar Association and a Committee of Publishers Associations .
In no way is it legal to reproduce, duplicate, or transmit any part of this document, either by electronic means, or in printed format. Recording this publication is strictly prohibited, and any storage of this document is not allowed unless with written permission from the publisher. All rights reserved.
The information provided herein is stated to be truthful and consistent, in that any liability, in terms of inattention or otherwise, by any usage or abuse of any policies, processes, or directions contained within, is the solitary and utter responsibility of the recipient reader. Under no circumstances will any legal responsibility or blame be held against the publisher for any reparation, damages, or monetary loss due to the information herein, either directly or indirectly.
Respective authors own all copyrights not held by the publisher.
The information herein is offered for informational purposes solely and is universally presented as such. The information herein is also presented without contract or any type of guarantee assurance.
The trademarks presented are done so without any consent, and this publication of the trademarks is without permission or backing by the trademark owners. All trademarks and brands within this book are thus for clarifying purposes only and are owned by the owners themselves, and not affiliated otherwise with this document.
Table of Contents
Introduction
Are you ready to start learning how to program? C++ is one of the most powerful programming languages in the world, a language that will give you a decent grounding in the techniques for modern programming. Many of the C++ concepts are shared with other languages s, when you learn one youve got a great head start for another one. And it's fair to say that you will need to learn more than one very few programmers work with single languages.
It has for a long time, and continues to be, the language of choice for applications that are high-performance, programs that run fast and continually being updated with new features on a regular basis. Dating back to 1979, C++ was developed by Bjarne Stroustrup and came about as a result of a Ph.D. thesis. From there, it has grown in leaps and bounds, becoming the strong object-oriented language it is today.
I have split this book into several parts. In the first part, you will get to grips with the language, how to get it on your computer and start writing programs with it. You won't be a C++ master but you will have a decent understanding of it and you will have the groundwork to learn some of the more advanced concepts. From there, we go on to working with large data amounts, file input, efficient data processing and you will learn some shortcuts too. Ive also included quizzes along the way, with the answers provided at the end of the book.
By the time you reach the end, you should have no problem reading C++ code and writing programs that are both interesting and useful. So, lets dive in and learn C++.
Chapter 1: Setting Up Your Environment
If you want to control your computer, you have to be able to speak to it and, to do this, you need a programming language. All this language is, is a piece of text, much like a book, that has its own definitive structure. That structure is stricter than a standard language and it has a much smaller, more defined vocabulary. C++ is one of the most popular of these languages.
Whats The Difference Between C and C++?
C programing language was originally developed for the Unix operating system. It a powerful language but it doesnt have any of the more useful or modern constructs. C++ is newer and is based on the C language, adding in modern features which all make it far easier to use than C, allowing for more sophisticated programs to be written.
With C++, memory management is easier. There are more features for object-oriented programming and for generic programming youll see what this means later. For now, all you need to know is that C++ gives programmers an easier ride than then C does.
Because C++ is a superset of the C language, you dont need to have prior knowledge or experience of C; if you do then you will just find C++ that much easier to grasp. And, contrary to what many people will tell you, no, you do not need to know math to become a programmer!
For the most part, computer programming is all about logical reasoning and design, not about complex arithmetic, calculus and algebra. Where math and programming overlap is in the area of precision thinking and logical reasoning; the only time you would need to have skills in math is if you intend to go into advanced 3D graphics engine processing statistical analysis or some other numerical programming that is specialized.
Terminology
Throughout this guide, you will see a lot of new terms, all of which will become clear to you. To start off with, Ill give you two of the more basic concepts that you really need to know before you start:
- Programming the act of writing a set of instructions that a computer can read, understand, and execute. These instructions are known as source code and that is what you will be writing if you follow this guide.
- this is the result of your programming a file that can be executed. This means that your computer can run it. You already see these if you have a Windows system, they are known as EXE files. In order to create an executable file, you must have a compiler. This is a program that converts your source code into executable code. If you dont have one of these, all you can do with your source code is look at it.
So, thats what we do next. Set up our environment so that we can write source code and execute it.
Setting up Your Environment
What we will be doing is setting up a compiler and a text editor, the latter of which is where you create the source code in the correct format. You must write your code in plain text format; these files will have nothing more than the text you write. It won't have any information about fonts, or text size, or anything like that.
Along with that, an editor will also provide some extra features which are helpful auto-indentation and syntax highlighting. With syntax highlighting, you get the benefit of colored coding that makes it easy for you to tell the different parts of your program apart. With auto-indentation your code becomes easier to read.
For those on Mac or Windows systems, well be using something called an IDE Integrated Development Editor. This is a combination of an editor and a compiler.
Lets get set up so we can get down to work.
Windows
For this, well use Code::Blocks. This is a free C++ environment that is easy to download and to use:
Your first step is to download Code::Blocks and, to do this, go to http://www.codeblocks.org/downloads . Click on the link that reads Download Binary Release and then go to the section for Windows.
Next, look through the files until you find one with mingw in the name something like codeblocks-10.05mingw-setup.exe (the number might be different). Click it and save it onto your Windows desktop.
Next page