Welcome to the second C++ tutorial. If you have not already you should probably check out the previous tutorial because this serious of tutorials, like most other subjects, builds off of the previous material.
Previously, we discussed basic output, printing string of characters to the standard output which is probably more familiar to you as the console or the terminal. In this lesson we will talk about the various types, data types, which exist in the C++ language. Types, or more formally a type system, are the ways in which programmers manage and interact with different types of data. So for example, if you consider how you would count the number friends attending a party you might count off the number of people there on you hand; one, two, three, four, . . . , n friends are at the party. Now consider that you want to know what average number of minuets that it takes for you to go jogging, one day it might take thirty minutes, the next it could take thirty three and then the average would be thirty one point five. You might notice the difference between these two examples is that one answer is fractional and the other is not, or in more mathematical terms one requires a real number, or floating point as we call it in computer science, as an answer but the other number is an integer. Continuing the example of a party with friends, suppose that you want to know which of the friends which you have invited are in attendance. Not the number of friends which attending, but rather which specific ones arrived. In order to record exactly which of your friends are attending you would need to record their names. Names are of course permutation of a certain set of letters which together spell a name. In C++ all three of those problems would have to have their data, answers to each problem, recorded using different data types but fortunately there are only a few.
Probably the most commonly used data type is the integer, or as we call it an
int. This data type can hold values that are mathematical integers, -1, 0, 1, 15, etc, but not numbers like .5, 17/11, sqrt(2), etc. The range of
integers that can be supported by your machine is the number raised to the power of the number of bits that your processor can operate on minus one. So for example a sixty four bit processor can natively manage 18446744073709551615
different numbers, more on positive/negative numbers in a moment, but to complicate matters further, but only slightly, there are variations of the int which include short, long, and others. Short is generally half of the precision of an
int and a long is generally twice the precision, but this can be somewhat dependent upon the hardware, compiler, and version of the standard library that you are working with, for a more accurate picture of the type system you are working
with when you run code for this tutorial, however it is exceedingly unlikely that any variation of implementation is likely to occur through poorly implemented compilers or such like because there we will not be working with numbers which
require very high precision. The other thing to remember is that no matter what the range of numbers your computer operates on, or what variation of the int you might be able to, the basic idea is the same and the fundamental
mathematical concepts at play do not change.
The next data type which you are likely to encounter is the floating point number. In more mathematical vocabulary this data type can hold real numbers, numbers with fractional components like 1 + 1 / 2, up to a certain non variable digit precision. This precision is dependent upon the type of machine which you are operating on, but this is generally up to seventeen digits. Again, this is actually quite a bit of precision and will suffice for whatever code we write in this tutorial. Also, just like the int floating point data types have a variety of implementations with more or less bits for precision or for speed, but the fundamentals don't change. Specifically, you are likely to encounter one of two variations, the float and the double. Both of these types do the same thing but there is sometimes a precision/speed trade off where greater precision, the double, requires more time to operate while the smaller data type, the float, requires less time but has fewer digits of precision. Most of the time the speed actually isn't that important and unless you know that the extra precision is not necessary I would recommend using the double because of the greater precision.
The third data type we saw in the examples is the string. Technically, the string is an array, we'll explain what that is in lesson three, of characters, letters like 'a', 'b', 'c', numbers 0-9 - digits not spelled, '1', 2', '3', and
special characters such as, '%', '#', '^', etc in a particular order. Exactly which characters can be represented in a string depend on some thing called the ASCII, American Standard Code for Information Interchange, or UTF-8, Unicode
Transformation Format, which are seven and eight bit formats respectively. Each of these standards have the entire set of characters described above, as well as some non-Latin characters as well. There are other standards for other
alphabets but we will be working exclusively with the two mentioned above. Strings are made up of some set, or multiset, of these characters in a particular order. For example, the characters, 'c', 'a', 't', can be combined to spell,
cat, but those same characters could also be combined to spell
tac, which is a different string. With the possible exception of some archaic standard for character sets most of this is probably quite intuitive and does not require extensive explanation but it never hurts to see some of the theory.
The final type that we will look at in depth is the bool. C++, but not its predecessor C, supports bool, short for boolean, values which hold only two states, true or false. There isn't very much to say about these but they are useful at times when you only care about two different states that you program might encounter. For example while a program is running, true, do some but then when the user closes the window, the bool flips to false, close. We will see more of these in the third lesson but you do not have to worry about them right now.
It is time to jump into our second project in which we will experiment with some of these data types through a program which takes three numbers as user input and averages them. Once again it will probably be easier for you to read through the code and try to understand what you can and then see the line by line explanation afterword.
I will ignore the parts of the program which we saw in the previous program except to point out that the little message that we printed out previously,
Hello, World!, has now been changed to a somewhat helpful line,
Program ===, which might make it easier to denote the beginning and end of a program in whatever environment you are using. It is completely optional and can be omitted without causing harm to the execution of the rest of the program
but it illustrates that simple thing you learn early on might come in handy later on. Right off of the bat you might also notice that there is a new file which we have to include, <string>, there's nothing fancy about this just
remember that you need to include that file when you want to work with strings.
Jumping into the main function you might notice right away that we are working with some new material. The line of code, string prompt =
Input(Type A Number And Press Enter) a value to be averaged: ; is what we call a variable
variable does not
vary in the sense that it changes in throughout the program, the
runtime of the program, but it is still a variable because it is not a
literal, which would just be the part
of the line with inside of the quotes. Of course we could have put anything inside of the quotes and of course we didn't really need to make
prompt a variable, but whatever we put in there will appear three times throughout the
program, once each when we use
cout, which has the positive effect of reducing the amount of typing that needs to be done by the programmer. Moving on to the numerical data types used in this example, you can see that we have one
integer, which is set to three and will always be equal to three no matter what the user does, and five doubles. Each one of these five doubles requires the user to input data through cin. Cin works to take user data in the same way
that cout works to print data to the terminal. At the end of the program the three values that the user inputted will be summed and divided by three to get their average value. That value will be printed to the terminal and then the
program will print the
=== Ending Program === message and the program will terminate.
Something that might have escaped notice is the naming convention for our variables. In algebra, geometry, calculus, statistics, or some other math course you've probably seen variables, like
x, and used in a formula like,
x^2 -4x + 1, or something like that. It's fine to call variables by short non descriptive names like
y for short programs but even in a program as simple as the one that we just wrote it would probably be hard to
follow along without more descriptive names like
sum. But I need to mention that there are some rules for acceptable variable names. Variable names can end with a digit, like the second double, but not start
with one. They can have any combination of letters and most special characters, but they are case sensitive. I chose three very different styles of naming for my variables to illustrate this but it is generally bad style to mix and
match different styles like that. In the same way that there are style conventions for indentation there are for variable names but I will leave that up to the read to decide for themselves which works the best.