Introduction:
Many times we wonder how computers can work from the inside. For the end user, there is practically no interaction with something deeper. They do not need to know many details about how the computer works or something about programming. The end user is only attracted by the functionality and the program to have a simple interface that is easy to handle.
However, the software does not create by itself. Somebody has to do the dirty work. Or, like other like to call it, the art of programming. The main part of computers is their microprocessor, the brain that executes every instruction needed for every task the user needs. These processor only understands binary code (zeros and ones) and it does not know nothing about English or numbers. So, how can we interact with the processor? We use programming languages.
Through history, many languages have been developed for the interaction between the programmers and the machines. In recent times, programming has become a major field of study with a wide variety of programming language to choose depending on the purpose. One of the most preferred languages is C++. Why? Mainly because of its speed, its standards and the wide range of applications. C++ was developed as an improvement for the C programming language. This change added many features, like enabling object oriented programming, templates, memory handling, error handling and more tools for the programmers.
The C++ in one day Course is targeted for people who want to learn the basics of the C++ programming language. We will cover from the most basic concepts and stuff, perfect for beginners, and as the course goes, we will be adding more features from the language and also start to develop some real life projects. This course is divided in four sections: 1. The basics of C++: In this chapter, we will cover the basics of C++. This is the main core of the course for beginners. Here we will cover the basic structure for every C++ program.
A brief explanation of the library system. The standard system of input and output from C++. Datatypes are another fundamental topic to cover and will be discussed, including examples of declaration, using and storing values. String processing will be covered. In this chapter, we will also explain the basic flow structures that are used in C++ for designing software. The if structure, the switch structure, the while structure and the for structure.
Finally, a very brief introduction to the basics of object oriented programming. 2. Things to do with C++: In this chapter, we will discuss what kinds of software can be developed using the C++ programming language. 3. Implementing projects with C++: Here we will write some code! 7 projects have been selected and programmed in order to show you how you can do useful things with C++. 4.
What to do next: Finally, we finish the course with some recommendations on topics that, while are not very necessary for the basics, they are a good place to follow after learning the basics. These features can enable you to design more optimal and complex software. So, lets get started!
Chapter 1: Basics of C++
C++ is a multipurpose programming language. What is this? That it can be used for writing every type of software that the programmer wants. It can be from a simple program like computing the sum of two numbers up to a full operating system (for example, Windows) and videogames (PS3, PS4, XBOX). Some advantages that C++ has over its competitors (like C# or Java) are, in first place, its speed.
When you write a C++ program, it is compiled to a binary file that runs along with the operating system. Second, it is standard. C++ has a very well defined standard from beginning to end and this means that, if the programmer writes code in standard C++, it can be ported to other compilers with minimum or small changes. However, not everything is as simple as its sounds. Although C++ is very fast, it is very difficult to master due to the libraries that it has and the Standard Template Library (STL) that will be discussed in later chapters. But lets get started with the basics of the language.
Instructions and compiler Through the article we will be using the GNU C++ compiler, G++, along with the IDE CodeLite. All of the code will be written in standard C++. CodeLite and the compiler can be downloaded from http://downloads.codelite.org/ Libraries The full extension of C++ has libraries for many purposes: input and output, math, memory management, time, file processing, string processing and many more features. However, when we start to code, we must define which of these libraries we want to use in our program. Since 2003, C++ also implemented a feature named namespaces , which are another way to organize all of the stuff inside the language. The main function In C++, all programs must contain a function (or a piece of code) named main .
The purpose of this function is to indicate the compiler where will the program start its execution. Input and Output (I/O) Almost every program in C++ needs an input and an output. There are some exceptions to the rule, but normally, there is always I/O. The input is all the data that enters the computer, like numbers, words. The output are the data that is showed to the screen (also named console). The library used in C++ for this operations is named iostream .
Inside iostream, there are two objects that we will be using to read data from the keyboard and showing data to the console: cout and cin . Before programming anything, in CodeLite we must create a new project. Steps for creating a new project in CodeLite:
- In the main screen you will see a button with the text NEW. Click it.
- When the new window appears, select on the left side the option Console and select Simple executable (g++) from the list.
- On the right panel choose a name for your project, for example Example1.
- In project path, select a folder in your computer where you will save your code.
- In the compiler section, be sure to have gnu g++ selected.
- Click OK
After doing this, go to the left side of your screen and will find a folder with the name you selected for your project. Open it and you will see another folder named src (abbreviation for source ). Inside src, there will be a file named main.cpp .
This is the file where you will write your C++ code. The example code for a simple input and output in C++ is as following: #include using namespace std; int main() { cout << "This is my first C++ program"; return 0; } As you can appreciate, the first line we included the library for I/O. On the second line, using namespace std we say to C++ that we want to use all of the features inside the selected libraries (like cout ). After this, we find our main function. Here is where all the execution begins. All functions in C++ hace an opening and closing brace that indicates where the function starts and ends.
Inside main, there is the line cout << This is my first C++ program; This line will output everything inside the double quotes to the console. Notice that this line ends with a semicolon. In C++, all the instructions inside a block of code end with a semicolon (there are some exceptions that will be covered). You can see that between the cout object and our text, there are two < signs. They are named overloaded operators and they represent the direction where the information is traveling, in this case the information is going outside the computer. As you may think, when we want to make an input to the program, we will be using the >> operators.