This is a continuation in our C++ tutorial series started last week! This episode covers understanding different data types in C++ especially strings, integers, floats, doubles (decimals), Booleans and the character data types!
We then give examples of each data type as variable and show basic string manipulation operations as well as some bath C++ math operations!
Is there something specific you'd like to see in a future video? Let me know about it in the comments below! Don't forget to subscribe to the channel and leave a like on the video!
If you'd like to support the channel in a huge way be sure to check out my Patreon to help me grow my content and reach a wider audience! https://www.patreon.com/LeMasterTech
Thanks for watching and good luck with your programs!
what's up you guys welcome back to the
master tech and in today's video i'm
going to give you a basic introduction
to data types and variable types in c
plus as well as some basic string and
numerical functions you can use
[Music]
all right so this video is going to be
kind of episode 2 in our c plus plus
tutorial series a continuation on the
previous video where we talked about
creating your first program and some
basic console out
methods and some basic ways of
structuring your program so if you need
that check that out i will leave a card
to it somewhere linked
otherwise let's get right into the
content for this video so first thing
i'm going to talk about is actually just
comment styles because if you're
familiar with other programming
languages you know there's always some
way of leaving text in your program
that does not actually have any
functional code so like in python you
can do it with a hashtag or a pound sign
or you can do it with like three single
apostrophes um
but in c
uh c based languages it's different in c
plus in particular you can do the double
hashtag or you can do
kind of a block of text by doing a
forwards
i think it said double hashtag double
forward slash or you can create like a
block of text by starting
one of these chains which is forward
slash and then a star and then star and
then forward slash and then anything in
here
will be a comment
so that's just a quick introduction to
comments i'm gonna try to use more
comments that's something i frequently
guess feedback on the channel is i just
get writing code i don't leave enough
comments so i will try to do that and
i'll mostly use these double forward
slashes because most of my comments can
stay on one line
but it's nice to know about the other
style as well alright so let's talk
about data types and variables
and so
we talked a little bit about this
previously because the first program we
made had a string example and a number
example in it
but let's just really quickly go through
the most commonly used types of
variables
there are always going to be a few
one-off situations where you'll learn
about a weird a unique variable you can
even create like a user-defined data
type but we're going to keep this one
basic because you'll be able to write a
ton of programs just with what we cover
here so the first is probably the most
obvious one is
string and
we talked about this as well to define a
variable
as a certain data type it's different
than in python where you would like just
type in sample string and then equals
and as long as the stuff you put over
here is like a string it knows it's a
string you actually have to tell it what
data type it's going to be so you need
to have the word string and then the
variable name and then if you want you
can set it equal to an initial value
right on this line or you can just leave
it at that and then later in your
program you can come through and give it
actual text so let's say we want to make
this
sample string equals the master tech
these two lines of code could definitely
be combined you just put the equals
after sample string
i'm gonna do the rest of the examples on
one line i just wanted to show that this
is legit too you don't have to have an
initial value for it just to name and
define your sample
so the second one i want to talk about
is definitely less common than a string
it's basically just a single character
so i'm going to make sample
char as my variable name and when i say
single character that means you use
these single quotations instead of the
doubles that are used for strings and in
here it can only take up to one
character so it can be a number or a
letter because it's sort of like one
snippet of a string
so you can put one character in here but
if you try to put a second character in
as a char variable it's going to fail so
it's only going to take the first
character you put in anyways so it's
good to know about those but you're not
going to use them that often especially
because there's no reason not to make a
string that just has one character in it
so there might be a situation where you
want a grade book or something that can
only ever have one letter but even in
those sorts of applications you're
probably going to end up using a string
more often than not
but it's useful to know about this if
you come across
single character variables in your
programming adventures it's good to know
where that's coming from and what it
does
now let's talk about some of the
numerical types of variable you can have
so let's start with int and this is
going to be just an integer value so it
can be positive or negative you know it
can be negative
any whole number or it can be positive
any whole number
it just needs to be a whole number
and then
the next i want to talk about is one of
the two types of decimal values that you
might use sometimes in general you're
actually not going to use a float
because the next one we talk about is a
little bit more robust and a little bit
more accurate but a float is a valid
form of decimal number
it's just basically like a floating
decimal point numerical value
but the one you'll find more often and
you'll probably want to just
lean towards as your default variable is
a double and the reason for that is it
has more decimal places and it is a more
precise number and a more precise form
of doing math in general um
but so these are all of your
kind of basic primary variable types and
there's one thing missing that's maybe
not intuitive you might not think of it
as a variable type but if you're
familiar with programming at all you
know computers basically boil down to
ones and zeros
they essentially boil down to making
very simple binary decisions as a true
or a false or zero or a one there is a
data type for that called a boolean
boolean is just a special word that
basically is computer lingo for true
false one byte binary
and so to
create a boolean it's bool b-o-o-l and
then variable name just like any other
variable and then you give it either
true
or false those are the only two states
it can have and so um this can be really
useful in making like binary logic
decisions like if this thing is true do
this otherwise do this other thing
there's no reason it has to be binary
you don't always need a boolean you can
pretty much always find another way to
write the program if you want but it's
very useful to know about this
so those really these basic variable
types and this is true across most
programming languages these variable
types are going to enable you to write
almost any program by combining them
with lists and functions and operations
manipulating them and your advanced
programs that you'll create these are
going to always be kind of the primary
data types and variable types that
you'll be using
all right so the two that probably come
up the most and have the most that you
can do with are strings and numbers so
let's go ahead and take a look at some
ways that you can do
string operations and that'll be the
next section we talk about here
so
let's say that we want to get a specific
piece of a string so we have our sample
string up here from before the master
tech
let's say we want to get one character
out of it we just want to know what it
starts with
well let's say character sample
indexer so in general what um spot in a
string a single character has is
referred to as its index you'll hear it
called some other things but index is by
far the most common one
and in programming lingo you always
start counting from zero so uh like to
look at le master tech here
the capital l is actually what character
you have in index zero lower case e is
in one the m is in two a s t e r so you
count zero one two three four five six
seven eight is just a space and then 9
is the capital t and to show you this i
will say that the sample indexer
is equal to
and the way you address a single
character of a string is with these
square brackets so
let's say we want the capital t at tech
as long as i counted correctly
just putting a 9 in these square
brackets should give you
just a capital t and so we can check
that by using what we covered before how
we're going to display a line of text
onto the console
is c out and then less than less than
sign and then the thing we want to be
shown on screen and in this case we'll
say the sample indexer
and then less than less than and then
end l with a semicolon and so now if i
run this it should just give us
character nine from the sample string in
our console window here so let's go
ahead and run it
and there you can see we get the capital
t right there so
that's how you address a specific
character in a string um and the second
way to check
what actual like substring you want so
let's say next we have a new string
variable that i will call my sample
sub string this is checking
for a whole row of
of information a whole not row a whole
series of characters from a string to
make a substring of so let's say we
wanted to go back to our le master text
sample string so we have sample
underscore string just like that
and instead of the square brackets now
we're going to use this function that's
dot
s-u-b-s-t-r and that stands for
sub-string
uh and you give this two arguments you
give it the um you give it the starting
character and then you give it the
number of characters you want to count
so let's say we want to start at nine
which is the capital t right and we want
to be four characters
um
this should give us the word tech as a
as a sub string of the main string
that's above us so let's go ahead and
run that just so we can see and make
sure i make any mistakes there you go
tech
and now you may say well what if i don't
always know the end of the string it's
actually not going to throw an error
even if you make this longer than the
string is so like obviously the most we
have is tech and then the string is over
but if i say i want to be eight
characters long
it's still just going to give us tech
that's fine and if i say i only want
this to be two characters long it'll
just give us the te that we asked for so
that's how you pull a sub string out of
a primary string this is how you address
a specific character
you can just like any variable
you can overwrite a specific character
so you could say
you could say here
sample
string
9
equals and you could say
let's make it a capital
l instead of uh capital t that was
probably stupid but now if you see what
happens to the
um
thing we press print out instead of t e
it's l e because you can change a
specific character in your string by
using this same format as you can just
to look up what it is
so
there's a couple basic string operations
and let's use
let's do one more that i think is pretty
useful we're going to find if a
substring is in a main string of text
and if it is we're going to print out
where it starts
um
so to do this we're going to want a
integer value because it's actually
going to if it's true it's going to
return the number at which the sample
string starts
so we're going to make a new variable
that i will call find substring and
we're going to set it equal to a sample
string
dot and this is another function that
we're going to use that comes kind of
built in with c plus plus strings
dot find and then we give this two
arguments we give it the sub string it's
looking for so let's say tech
and then we give it um if we want this
is actually an optional argument but you
give it what index to look after so uh
let's say for at first we just want to
start at zero
now let's print find substring and
see what this gives us now we know
because we have used i forgot a
semicolon
we know because we already looked it up
so that we could use the 9 before that
it starts at 9. so this is good to know
but let's say that instead of tech
because we've been using that one we
actually want to do er space tech okay
so we know that this substring should
exist
here which counting back a little bit
that would be 9876
so if we run this now it should give us
the index at which
yep the index at which our sub string
starts
and then what's neat is if we make this
8 or something that's after that
substring appears and we run it we'll
get minus one which is essentially the
find substring um the find functions way
of saying i didn't find it because
obviously this is an integer not a
boolean so most of the time when it
finds the substring it's going to tell
you where it was but when it doesn't
it's going to return a minus 1 meaning i
didn't find it
okay so that's the find substring
function and that is pretty useful and
for now i'm going to leave it with that
because there's obviously a ton of ways
you can manipulate strings
but this is an introduction video so if
you have any specific questions feel
free to ask them in the comments or
we'll probably cover them in a future
video as our programs get more complex
but for now let's cover some basic
math operations
okay
and so for math we talked a little bit
about the difference between an integer
a double um and a float but let's just
start looking at what you can actually
do with those so i'll make a new c
outline
just to talk a little bit about some of
the ways that math works in c plus
so if i do five plus seven
um you can do basic math just like that
just typing the operations um and if i
run this you'll see we should get 12.
there we go we got 12 right there um and
this works for four operators so it
works for plus it works for minus it
works for division which is a forward
slash and it works for multiplication
which is the star key
um so i'm just keep track of what we
talk about here plus minus division
starkey these four are your basic
operations but some additional stuff you
can do right here uh this is a character
that most programming languages have
it's even often represented as the
percentage key this is called the modulo
operator
and this is essentially saying remainder
so if i were to say
11 and this would be read as mod 5 an
hour to run it
you're going to see we get a 1
because 11 divided by 5 is obviously 2
but
it's 2 and then 1 5th right so
is 11 divided by 5 is checking how many
whole times the divisor fits into the
number and then what's left over so if i
had like
um 22
mod 5 we should get a 2 because 5 fits
into 20 four times and then there's two
left over
so it's essentially there you go there's
two so it's essentially a remainder
function it won't come up as often as
the basic math operations but you will
find yourself using it possibly more
times than you would expect
all right now a second thing about how
numbers work if i were to try to combine
an integer with a decimal
in math well you'll see when i run it is
it's going to automatically update it to
a double or a decimal or a float you
know it's going to have the
decimal points because it knows all
right 22 plus 5
i have one
more accurate more precise number i have
a double and i have one integer by
default it's going to increase the uh i
guess smartness if you will of the math
and say well an integer plus a double my
answer should be a double so i make sure
not to chop off the decimal place so
it's smart enough to know to do that for
you
but if you were to do 10 divided by
6 which obviously does not evenly divide
so we should get essentially one and two
thirds right one point six six six six
six well if i run this it's just gonna
give me one because it's not smart
enough to know oh well i was given two
integers and an operation but i should
convert to double because they're gonna
want a smarter operation
it doesn't know that by default but if
you make either of these numbers just a
point zero so you still kind of have an
integer because it's a whole number but
you've added a decimal place to it well
if i run it now
i get 1.667 so there you go it gets more
precise so it's just important to know
even if you have integers it's not a bad
idea to make one of them a double or
make all your numbers doubles even if
you're using them like integers
okay so that's a little basic on how
that works and then probably the next
thing i want to talk about is the
incrementer tool so let's make an
integer and let's just say it's a
counter okay
and initially it equals zero
well uh there's a built-in function to
add one to a uh to a number and it's
plus plus
so i'm going to go ahead and show you if
i just say counter plus plus and then
print out what the counter is even
though i said it was 0 initially
it's going to give us a 1 because that
plus plus automatically adds one to a
value this is really useful for like for
loops or while loops or anything where
you want like an indexer to loop through
something
this also works with minus minus it just
will subtract one and this can go
negative so uh that will give you a
negative one you can see there
and then with that so now we've talked
about those we've talked about plus plus
minus minus
and with that there is uh also the
ability to do minus equals and then
instead of um always subtracting one you
can tell it how much to subtract so you
can say actually i want to subtract
three from what i already was so that's
saying zero minus three
um and then obviously the counter to
that is plus equals you can affect um
how much you want to add to it so that's
plus equals and minus equals those are
some pretty basic
useful tools as well while you're
looping through
numbers
and then i want to talk about importing
tools so we kind of included the i o
stream for the first project and for
this one so that we could print stuff
out onto the console but we didn't
really talk about what this is this is
essentially an import statement it's
saying
i want a package of functionality that's
not by default available in my program
but c plus is able to use it
and to do that we just use the pound
sign
and then include and then the name of
our module
okay and so for this one
the name of the module is going to be c
math because it uses a lot of built-in
native to c programming languages math
functions
and some of the things you can do with c
math are really cool and uh really
useful and so to do this let's just make
a double real quick that i will call
my
decimal
all right so it's a decimal number that
i'm just going to print out onto the
screen so i can show you what's
happening with all the functions we're
about to use let's start by talking
about raising something to a power all
right so
to do this it's just pow
and then you give it two arguments you
give it the number and then you give it
the power you're raising it to so this
would be 3 9 27
it should be 81
when we show this on the screen
there you go we get 81. so the pow
function is just looking for the number
and then the power you want to raise it
to
the next function we'll talk about is
going to be
using the square root so it's sqrt and
then you just give it the number you're
looking for the square root of and
so let's do one that we know will work
first obviously the square root of 16 is
four
but let's give it a number that we know
won't be a whole number so let's say
what's the square root of 13.5
uh
3.67423 which is neat i didn't know that
um
but so these things will get real
accurate they don't have to be whole
numbers they don't have to be integers
um and now let's talk a little bit about
the round functionality so i'll leave
13.5 in there
round is going to follow default
rounding
rounding commands which are essentially
anything below 0.5 you round down
anything above 0.5 you round up
there are two ways to change this and
one is seal
so seal is always going to round up
which seal stands for ceiling ceil
it's going to round your value to the
ceiling so 13.2 even though it's
obviously closer to 13 by standard
rounding rules it's going to round up
because we're using ceiling
now the opposite of this is floor
rounding which is going to take a number
like 13.7 which obviously should round
up it's closer to 14.
and we're going to round down to 13. so
now we have pow we have a
square root
we have round we have seal and we have
floor and actually this would probably
be a good time to change this uh comment
to the
let's do it this way let's change it to
the multi-line comment
because that is going to be something we
want to break onto multiple lines it's
getting hard to read there we go
so there we go we covered something in
the beginning i didn't think we'd need
we needed it look at that
um all right and now there's a few other
things we'll touch on really quickly um
one is going to be the f max function so
this is where you give it uh two
arguments uh and you wanna return what
the larger of it is so let's say we were
given 13.7 and 4.5 it'll tell us the
larger is 13.7
the complement to this is f min it's
going to tell us what the smaller number
is and you may say this is stupid i'm
going to type in two numbers and it's
going to tell me the larger the smaller
well it's not actually stupid because a
lot of the times these would be
variables like this would be the result
of the score of the game you just played
and then the second one would be the
high score you already had and they're
stored in variables and what you'll want
to do is you'll want to move whichever
one of those two is larger into the new
high score variable so there are reasons
that you need to know these even though
seeing them typed in directly like that
might not make a ton of sense right now
so that's the max and min functionality
and i think those are all the ones i'm
actually going to give a
example for because this is kind of a
long video and i know you guys want to
get writing programs
so one thing i will say is all the
trigonometric stuff in here is also
available so that's sine cosine tangent
you can use logarithmic functions so log
is in there log base 10 is in there
exponentials where it's not just
raising it to a power it's actually the
e to the whatever those are all
available within the math function as
well
so be sure to go in play around we're
going to be using some of this stuff uh
in the future some of the first pro
projects we're going to build are like
calculator type functions
so it's really useful to know about the
math functionality but that is a basic
introduction to strings a basic
introduction to numbers we covered data
types and variable types so there's a
ton of information in here i hope you're
enjoying the c plus series be sure to
let me know in the comments what you'd
like to see more of
what kind of stuff you want us to
eventually get to whether that's games
and game specific programming or more
data science applications so thank you
so much for watching
i really appreciate the support don't
forget to subscribe to the channel leave
a like on the video and as always good
luck with your code and thanks for
watching thanks bye