ebook img

C++ In One Day: The Ultimate Beginners Guide To C++ With 7 Awesome Projects PDF

112 Pages·2016·0.33 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview C++ In One Day: The Ultimate Beginners Guide To C++ With 7 Awesome Projects

C++ In One Day The Ultimate Beginners Guide To C++ With 7 Awesome Projects By W.B Ean Copyright 2016 - All Rights Reserved – W.B Ean ALL RIGHTS RESERVED. No part of this publication may be reproduced or transmitted in any form whatsoever, electronic, or mechanical, including photocopying, recording, or by any informational storage or retrieval system without express written, dated and signed permission from the author. I : NTRODUCTION 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, let’s get started! C 1: B C++ HAPTER ASICS OF 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 let’s 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 <iostream> 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. For executing the program, go to the Build menu and select the option Build and Run project. You will see how a black screen appears. This is the console and this is where we will perform our input and output. Pausing the execution If the console appears and immediately closes, don’t worry, it is normal. But we need to see the output. For this, add: cin.get() Before return 0; Data Types and variables When we work with a program, there are many types of data we can use. For example: numbers, text, and some special types of data. All of these are called Data Types. They are very important in C++ because it is the main point for developing inputs. In C++ we have 3 categories: numeric, alphabetic and binary. In the numeric category we have 3 types of data: int – Represent integer numbers float – Represents numbers with 7 decimal positions after the point double – Represents numbers with 15/16 decimal positions after the point There are more specific numeric data types but for the purposes of this article, these three are enough. For the alphabetic category we have two types of data: char – Store one alphanumeric digit string – Store a sequence of alphanumeric digits And in the binary category, there is only one data type: bool – Store a true or false. Zero or One. The bool data type may sound very useless at first but it has its handy applications when programming some specific routines. But, why do we need all of these types? Simple: In C++ there is a concept named variable. A variable can store information of any of these mentioned types. Imagine a variable like a box that can hold some value for a certain time and you can do any operation with that value. In C++, before using variables, we must declare them. How? Like this: <datatype> <nameOfVariable> Let’s make a simple example. We want to calculate the sum of two numbers. These numbers are integer. So, in order to accomplish this, we must declare three variables, one for each number and another one for the result: int numberA; int numberB; int result; As you can see, for each variable we defined the int type and after the data type, the name for the variable. You can name the variables in the way that you want, but it is recommendable to give them a name similar to the use it will have. Now the question is: How can we assing a value? We use the equal = operator: numberA = 10; numberB = 5; result = numberA + numberB; For this, I assigned 10 to numberA and 5 to numberB. After that, we assigned the value of result to the sum of both variables. The complete program would look like this: #include <iostream> using namespace std; int main() { int numberA; int numberB; int result; numberA = 10; numberB = 5; result = numberA + numberB; cout << result; cin.get(); return 0; } Notice that here, we used cout << result; for our output without the double quotes. When you use double quoutes, ALL the text inside the quotes will go directly to the screen. However, when we want to display the value of a variable, we must write the name of the variable without quotes. C++ then will look for the value inside the variable and show it on your screen. There are some limitations to this code. For example: we have to assign values directly inside the code for the sum. The optimal way would be giving the user the option for input the values into the program. This can be done like this: #include <iostream> using namespace std; int main() { int numberA; int numberB; int result; cout << "Enter first value: "; cin >> numberA; cout << "Enter second value: "; cin >> numberB; result = numberA + numberB; cout << "The sum of both values is: " << result; cin.get(); return 0; } Notice some differences in relation to the other code. First, the declaration of the three variables we need. After this, we tell the user to enter the first value. We can see how cin>>numerA is used. With this line, we tell C++ to capture the input from the keyboard and the value assign it to the variable numberA. The same for numberB. After this, the result of the sum is calculated and is sent to the output. Notice how in the last output, the overloaded operation appears twice. This is very convenient we want to show multiple values in the same line. Now, let’s say we want to know the average of these two values. We can modify the result line in the following way: result = (numberA + numberB) / 2; The parenthesis indicates that the mathematical operations inside the parenthesis will be done firstly. After that, it will continue with the rest of the expression. However, we have a problem with this line. Suppose numberA has the value of 5 and numberB has a value of 6. The result would be 5.5. No problem here until… we notice that result is declared as int. And, according to our data types, the int data type is reserved only for integer. So, what should we use? We must change the datatype of result to either float or double. double result; Besides sum and divition, there are more math operators to perform operations. The sustraction is performed with the “-“ operator, the product is performed with the “*” operator. And a special case is the “%” operator, named Modulus. The modulus will return the left over of a division. More complex mathemathical operations can be performed with C++ using the <complex> library. Por example, power, square root, trigonometrical functions, and more. String I/O The management of string in C++ can be done with the library <string>. It includes the object string that we need to use. For example, let’s suppose we want a program to say goodbye to anyone. It would look like this: #include <iostream> #include <string> using namespace std; int main() { string name; cout << "What's your name? "; cin >> name; cout << "Goodbye " << name; cin.get(); return 0; } The first thing you may notice is that we added the string library. Inside main, just like any variable, we declared name as string type. After that, we requested the user to write their name and then output the sentence “Goodbye” followed by the given name. Let’s see another example: #include <iostream> #include <string> using namespace std; int main() {

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.