(gentle music)
- You're probably wondering
what is this term data type?
And how does it apply to C++?
When we were young,
we first learned about the different types of numbers.
Usually with a venn diagram like this,
where we start with integers,
which are our counting numbers.
And then we start to add the negative numbers as well,
but there's no fractional portion there.
Then we move on to including fractions
which is now rational numbers
which is integers and fractions.
Then we have real, such as the cube root of five.
And then we eventually learn in more advanced classes
about complex numbers.
Because C++ is what we call a strict data typing language.
That means that the data type we choose specifies
the allowable values that can be stored
by our variable name.
In addition to integers and rational numbers.
We also need to be able to represent characters
and sequences of characters which are called strings.
C++ in most other languages has special data types
to represent Boolean values and data types that allow
for multiple variables, such as an array or a vector.
Sometimes it's necessary to know the memory address
or location of a variable in memory.
So C++ also has pointer and reference data types.
An integer is a number that can be written
without a fractional component.
Such as -35, -5, zero, 23, 3,456.
Double is the name in C++ that we attached to the datatype
for rational numbers.
A rational number includes a fractional component.
So if we want to include a number with a decimal point,
we want to use double.
If we don't want the decimal point
we want to use int.
An example of rational numbers include -3.14, 0.0,
3.14, 1.5, 7.0.
Notice the value seven is an integer
but when you use the datatype double,
it automatically appends 0.0 to the end.
There are four common fundamental data types.
These include bool which is named
after the mathematician, George Boole.
And it represents a value that's either true or false.
Remember in machine language, we only have ones and zeros.
So true is a one.
False is a zero.
Next we have char sometimes pronounced char.
This is a single character
and it's always enclosed in single quotes.
So it's one character.
It could also be a number in single quotes
or even a symbol in single quotes.
Followed by int.
Int is short for integer.
And it does not include the decimal point.
Double is a rational number
that does include the decimal portion.
In addition to those four fundamental data types,
we have some variations such as short
which is actually a small integer.
It takes up less space.
So if you know that your numbers
are only going to be from zero to 56,
you might want to use short instead of int.
It'll save space and memory.
We have long, and long, long.
Where long, long is new with C++ 11.
But this allows us to do large calculations
on large integer numbers.
We have float, which is the small version of double
and that is a rational number with few levels of precision.
And then the opposite would be long double,
which is a rational number with extra levels of precision.
We also have this ampersand.
Now the ampersand is attached
to a data type such as int ampersand.
And when it is,
it means that the value is the address of,
or reference value.
The asterisk is also attached to a data type
such as short asterisk or double asterisk
and that identifies a pointer value.
C++ also has compound types.
We mentioned string and we said string
is actually a sequence of characters.
So it's made up of the character data type.
And in order to use string,
we have to have the pound sign include string header
at the top of our program.
Vector and array both allow us to store multiple values
within one variable name.
And these data types must all have the same type.
So they must either all be integers,
all doubles, all characters, all strings, et cetera.
And list is another compound data type.
There are more that are not listed here
but these are the ones that we're going to use in this course.
Each data type allocates a certain amount of memory
to hold the values based on their fundamental data type.
For example, both bool and char are allocated
only one byte of memory.
Int is the next largest, and that has four bytes of memory.
And double, actually has eight bites
because it needs to include that extra precision.
The fundamental data types are available automatically
through the standard library.
But many times we also need special data types
such as strings, vectors and arrays.
To use strings, we already said
we need to include the header, pound sign include string.
An example of a string might be,
string name equals today is Monday.
And since a string is an array of characters,
it actually puts each letter into its own spot.
Here, we can see the array.
All arrays and factors are referenced using an index.
The index starts at zero.
So for example, the T in today is stored at position zero.
Then the O is in position one.
All the way to the Y at the end of Monday in position 14.
Now notice there is an extra symbol added to the end.
This is a no character
and it is used to determine the end of our string.
So all strings that are stored in memory
will have this symbol at the end.
One thing to note
is that when you do create a string literal,
it can not be changed.
So since I used string name
instead of doing a character array,
the phrase today is Monday, cannot be changed.
Remember, C++ is what we call a strict data typed language.
And with C++ 11, it is even more important
that we're careful to declare our variables
with the correct data type.
We have a few ways to change the data type
of a value or variable.
We can use what we call an implicit conversion,
or we can do it explicitly.
An implicit conversion happens
when we have compatible datatypes such as short and int.
We can create a short variable
and assign it to an integer variable.
C++ will automatically change the data type.
The same is true with the float that is stored as a double.
But you must be careful to go
from a smaller type to a larger type to keep data integrity.
Since Boolean variables are either a zero or a one,
we can assign a bool value to an invariable.
It is also possible to save a character
in an integer variable.
Since all characters have a numeric conversion value
according to the ASCII conversion chart.
Another way to change the data type
is to use the static underscore cast command.
So if we have a situation where we only
want the integer portion of a double value,
we can accomplish that using our static cast.
Or maybe we need to save a copy
of a number into a string variable.
C++ has the ability to dynamically change the value
of a variable or piece of data
so that it can be used for a different purpose.
Let's switch over to our IDE
and take a look at a couple examples.
Okay, I know I want to print out some values.
So I'm going to automatically add my using namespace.
Now my main function.
Let's go ahead and declare some variables.
So I'll start by declaring a double variable X
and I'm going to give it a value of 45.765.
Next, I'll declare an integer Y.
And let's set Y equal to X, but let's use our static cast
so we can change the value.
So I'm going to do static underscore cast.
And what I want to do
is I want to change the value of X to be an integer.
So in other words, it's going to truncate the value.
It's not going to round because I'm using the static cast
it will automatically only give me the integer portion
of the original number.
All right, let's also, let's try this.
Let's do, int B equals true.
Remember, Boolean variables or Boolean values
true and false are just zeros and ones.
So I can assign the value true to B
and it'll print out a one.
I can also set an integer value equal to a character.
The lower case a in the ASCII conversion table
is the integer value 97.
So when I print it out, I'll see the value 97.
All right, let's add some see out statements
so we can see what's happening.
So I'm going to say, X colon
and I'll print out X,
and I'll put an endl at the end.
So X is going to print out the original double value.
Next I'll print out the value
after I convert it to an integer, which is Y.
Next we'll print out the value A.
I'm sorry, let's print out the value B.
Go in order.
Remember B is an integer,
although it was given the value true.
And finally, let's print out the value of C.
C should be the value 97.
Okay.
Let's go ahead and run this.
I have an error on line eight.
I forgot, I need to put my variable in parentheses here.
So let's go ahead and do that.
Here we go.
And now I'll compile it again.
And there's our results.
So we have 45.765.
When you make that double an integer, you get 45.
When you assign a Boolean variable true
to an integer variable, I get the value one.
And when I assign a character variable A
to an integer variable C, I get the value 97.
(gentle music)