C++ Tutorial From Basic to Advance

C++ Tutorial From Basic to Advance


what is going on everybody my name is Bucky
Roberts and welcome to your very first C++ tutorial now my guess is that if you’re watching
this video you probably already have a basic understanding of what C++ is but for those
of you guys who don’t C++ is a computer programming language it’s how you make computer programs
computer games so if you’re interested in making any of those then congratulations you’re
in the right spot so before we just start opening any program and trying to write C++
code I want to let you guys know that you need to download and install a few programs
first now good news all these programs are entirely free but before we just go ahead
and start installing them let me tell you guys what they are and what they do the first
program that you’re going to need is called an i d– II this is pretty much the program
that allows us to write the code and run it aside from that we need something called a
compiler now ago compiler is basically the thing that translates the code that something
that computer program is going to understand to something computers can understand so we’re
going to be typing code and it’s going to be easy for us to read and understand what’s
going on but in order to transfer it to or excuse me translate it to a bunch of ones
and zeros that the computer can understand we need that compiler so that being said a
lot of people download these separately they download IDE and then they download a compiler
but what I’m going to be doing is I’m going to be showing you guys a program that is going
to allow you to download and install it all in one big shebang so let’s go ahead and open
Google and like I said don’t worry about paying for anything because this program is entirely
free so anyways like I said go ahead and open google and type in code blocks and go ahead
and press Enter now the first link that’s going to pop up is probably going to be four
code blocks org so go ahead and click that and this is the program that we’re going to
be downloading so of course we need to find the download link you can either click on
this one on the left or this one on top doesn’t matter go ahead and click on one of those
and we see a couple options here download the binary release download the source code
or retrieve source code from SVN we’re going to want to download the binary release it’s
the easiest and most simple way to download this program code blocks so go ahead and click
download binary release and check it out it gives us all these options now depending on
what platform you’re running most of you guys will all be running Windows you’re going to
want to choose one of these options but for those of you running Linux and they also have
a Mac version down here you might want to choose these but for most of us we’re going
to be running this on a Windows machine sorry saying all right now we have it narrowed down
to two choices code blocks 1005 setup or code blocks 10:05 a.m. ing W setup what we want
to do is we want to download this second one the one that says mi n GW now this version
1005 may be different depending on when you’re watching this tutorial and if they updated
code blocks but make sure you get the one that says mi n GW the first one is the IDE
the second one M mi ng W that mi ng W means the compiler to so when we download this it’s
going to give us the IDE and remember that’s where we write the code at the compiler as
well remember the compiler is the program we need so the computer can understand what
we’re doing so go ahead and you can either download it from barely OS or so excuse me
SourceForge I already have it downloaded so I’m not going to click this but go excuse
me I guess I’m phlegm in my throat I just drink a fruity stuff freeze from Taco Bell
and I ate a taco and that is not a good idea because they should call these things brain
freezes brain freezes because that’s all that does to you just freezes your brain in your
throw and and it puts a bunch of phlegm in your throat but I can’t stop drinking it it’s
a dick thing so anyways go ahead and click this link from sourceforge.net and go ahead
and save the file and whenever you save it and open it a wizard is going to pop up and
you just have to click Next like 80 different times and then you have code blocks installed
so I already installed it so I’m not going to walk you guys through how to do that because
it’s really simple and I’m not going to act like you guys are dumb and you don’t know
how to install things on your computer so anyways once you have it installed you have
this program called code blocks right here now you probably have it on your all programs
you might have it under code blocks what I like to do is right-click it and just send
it to desktop and what that’s going to do is going to give you a nice little icon on
your desktop but anyways once you have it installed just go ahead and double click it
and we’re going to run a really simple program just to make sure it’s working correctly so
go ahead it gives you these tips close them out what we’re going to want to do is create
a new project and you can either do that by creating this new project right here but what
I like to do is I like to go up to the file menu and click a new project now it’s going
to give you a list of a bunch of different projects you’re going to want to create what
we’re going to want to choose is console application this is what you’re going to be choosing most
of the time this pretty much means a basic program for the computer so just go ahead
and select this and press GO and make sure you’re working in C++ sees a totally different
language we don’t want that so C++ go ahead and click Next and now name your project anything
you want I’m going to name mine YouTube why not and you can save it wherever you want
I might as well just save mine right on desktop it looks good but it really doesn’t matter
where you save it or even what your name your project just name is something that’s easy
to remember so go ahead and give your project site we’ll go ahead and click Next and make
sure you’re using the GNU GCC compiler basically everything should be set up by default make
sure your settings look the same as mine as long as you didn’t hit anything by accident
you should be good to go so go ahead and click finish and you see that nothing pops up by
default you’re saying alright I thought we just created something so how do we see what
we just created actually if you click this little plus sign next to sources it shows
you that all the files it created for you automatically so go ahead and double click
this main and CPP and it shows you or in other words it brings up the file that it created
automatically so this is actually the core basics of a very simple C++ program we don’t
understand what anything means right now but trust me it’s a very simple computer program
that the only thing it does is it prints out hello world on the screen so to make sure
that everything is set up and running smoothly let’s go ahead and we need to do something
called build and run this now I’m going to be talking about this later but for now I
just want to make sure that your computer is set up you installed it and downloaded
it properly so go ahead and click this build and run icon right here it’s with the little
gear and the little green triangle so go ahead and click that and it should you know maybe
flash a couple boxes but basically a box should pop up that says hello world and this is all
that that program is basically it prints out hello world on your screen so if you build
and run this and you get hello world it means that you downloaded it installed it and set
up your project successfully so we are good to go so again I know you guys don’t know
what any of this means or even know how to code one line of code yet I’m going to be
going over that in the future tutorials but for now that’s all I have for you guys so
thank you very much for watching don’t forget to subscribe and I’ll see you next tutorial what is going on guys it’s Bucky and welcome
to your second C++ tutorial and in this tutorial what I’m going to be doing is I’m going to
be breaking apart this computer program that I ran in less tutorial so if you remember
whenever we first created a project in code blocks colorbox already made a very simple
computer program for us for us to run and test out just to make sure everything was
working and all it did was it printed out hello world on the screen so if you go ahead
and build and run this we see that it just says hello world and some other computer stuff
that isn’t really relevant anyways all this computer program does is print out hello world
and before I get into breaking this apart and telling you what each piece does I want
to say one thing you would think since computer programs are just made up of a bunch of lines
of text that we can learn each line one at a time but computer programs are weird and
that this is pretty much the most simple program you can write and check it out there are already
seven different lines included here so in order to even write a most basic program you
need to code at least seven lines of code so it seems kind of confusing and I just want
to give you guys a warning as long as you can get through this video all the rest of
the videos are going to be incredibly easy this is the only one where I have to throw
a bunch of information at you but still I’m going to break it down as easy as possible
and the reason for that is because you need so many things in order to run such a simple
program but once you understand the basics everything else is just smooth sailing I promise
so let me go ahead and we’re going to be getting more in depth in each of these lines but for
now let me just give you guys a brief introduction to what each line does let’s go ahead and
start at the top and work our way down this include iostream line right here this is technically
called a preprocessor directive so if you’re taking a test if you’re watching these stories
because you know maybe you’re in C++ class and your teacher is kind of confusing you
answer on the test this is a preprocessor directive now what that means in read a terms
is it pretty much says all right we’re going to be including a file in this program that
we’re going to be using later on so it’s going to be including a file i/o stream that we
didn’t make we didn’t make this file but it needs it to run this program later on so that’s
why it needed to include it again we’re going to be talking more about header files later
on but for the basics of the story all you need is that to know that this line includes
the file that our program needs to use later on now the next line you can see is just a
blank line and you may say alright do you need this blank line do you not need it blank
lines and spaces you can have a million of them if you want or you can have none of them
just like that all blank lines do in programming is they make the program easier to read just
with a book or a magazine or newspaper sometimes you want blank lines between your code sometimes
you just want your code right under each other it’s personal preference um people you know
whatever you want blank lines just make it easier to read so whatever is easier for you
add as many lines or as less lines as you want and that brings me to another point these
tabs to see this is kind of tabbed out or tabbed in it’s the same thing some people
tap things and make it easier to read this program is just going to run just fine if
it’s just like that but you know people just like the line certain things up so you’ll
see later on once we get coding that tabs and spaces and blank lines they don’t really
meet anything significant they just make the program easier to read so that being said
under that blank line is this using namespace STD and believe it or not this does not stand
for sexually transmitted disease I like to think that but trust me it doesn’t it stands
for the standard library and that’s another thing that is built in to C++ that is pretty
much saying this we’re going to be using all the standard things with C++ nothing fancy
again we’re going to be going over libraries and stuff in the future but basically these
two lines include things that are going to need later on this includes a file and this
includes something called a library what you know is just a bunch of C++ stuff so with
that being said we know that these two lines include things fair enough so now let’s get
to the meat and on meat and potatoes I guess of the computer programming and that’s this
this entire thing is called a function now all computer programs are made up of functions
and all functions are are things you want the computer to do for example whenever we’re
making programs later on we’re going to make a function to play sounds and we’re going
to make a function to draw graphics on the screen and as you can see functions are just
things that you want the computer to do so every computer program starts with a function
called main this is how your computer knows where to start so for example in a couple
tutorials we’re going to have a function called main and like I said we’re going to have another
function called play a sound and we’re going to have another function called make a ball
bounce around on the screen and you’re going to have all these functions and your computer’s
going to say alright you gave me all this stuff to do but how do I know what order to
do them in well your computer automatically knows any time it runs a program to start
with the main function so that’s why it’s so crucial to name this function main so in
order to make a function you do this you first write what type of data you’re going to be
working with and what functions typically typically do is they do some sort of calculations
for example find the body mass index or calculate the velocity of something well whenever it
makes this calculation it usually wants a number back in answer in other words well
since we’re going to be working with just integers main always works with integers so
that’s why we need that but again we’ll be getting into that later on after that is the
name of the function and this one is named main don’t worry about these parentheses right
now but what we do need to worry about is these little they’re not semicolons squiggly
brackets I can’t I guess they’re called curly braces I don’t know whatever the heck they
are this has an opening and a closing curly brace and in between them you have something
called statements now all functions are made up of statements and all statements are basically
instructions and each instruction needs to end with a semicolon that’s why you see every
line inside a function ends with a semicolon for example if you had a function that says
make a peanut butter and jelly sandwich one statement would be get out the bread the next
statement would be put butter on peace bread the next statement would be put jelly on peace
bread the next put them together the last put them in your mouth BAM you now have a
function and a series of statements well all this function did the main one said print
out something on the screen and return zero simple enough so now you can see the basics
of a function how they’re made and how every function needs to include statements or instructions
so that being said let’s break this down even more the first statement is saying this first
of all this C out thing right here that’s called an output stream object and it’s basically
used to write characters on the computer screen that’s how we are able to type hello world
on the computer screen so this is always used in conjunction with this this is called the
stream insertion operator this pretty much takes all this stuff to the right of it and
prints it out on the screen I mean this is just uh you know it’s weird symbols but that’s
what whoever made C++ they decide to use these weird symbols to print out something on the
screen why they did I don’t know I have to take it up with them but anyways this is basically
how you print out stuff in the screen so this prints out hello world and then this pretty
much just means end line or go to the next line so this line right here is pretty much
the same print something out on the screen now this last thing I want to talk to you
guys about is called the return statement now usually like I said functions will calculate
something like body mass index or velocity and when they calculate it it’s going to want
an answer back so if I had a function that calculated in my weight it would return 1/5
five or 160 depending on if I ate tacos or not today well the main function should always
have returned zero and return zero means whenever you return zero it tells your program that
it ran fine without any errors for example if you had a bunch of extra text right here
your computer program will give you an error and it would never get to this line right
here so it will never return zero so whenever it does get to this line it means everything
above it ran fine in return zero means you’re good to go main ran fine so I know that was
a lot of information to give to you guys for just your first tutorial but again like I
said every computer program starts with a bunch of stuff that you need to know and we’re
going to be covering each more of the topics in depth but for now I just want to give you
guys a basic understanding of how a computer program is made so if you don’t understand
everything completely then don’t worry you’re not alone you will in future I promise but
for now thank you for sticking with me and thank you for watching this tutorial and I
can’t wait to see you next tutorial and if you have any questions don’t forget to check
out my website the new Boston comm and yeah I’ll see you in the next video what’s going on guys is Bucky and welcome
to your third C++ tutorial and in this tutorial I want to go over a little more about printing
text out on the screen now in the last tutorial you remember that we could use that special
thing called C out which was technically called the ALPA object and the less than less than
sign which was called the stream insertion operator to pronounce X on the screen in the
way we did this is we added double quotes after and whatever we print it out in this
double quotes like boy I love bacon whatever we put inside those double quotes would just
print out on the screen so if we go ahead and we built and run this we saw boy I love
bacon right on the screen simple enough but now what I want to go over is just a little
more about C out and I just want to make sure you guys understand how it works so each function
is made up of these things called statements and typically people put one statement per
line of code and remember all statements and with semicolons so say we wanted to print
out something else out on the screen will you to write C out less and less than and
write something else like and ham just like that so you might be thinking alright I got
a feeling I know this is going to do on the first line it’s going to print out boy I love
bacon and on the second line it’s going to print out an ham because you know that’s how
it appears in our code so that’s what it’s going to do but if we go ahead and build and
run this it says boy I love bacon and ham all on the same line of code every scratching
your head saying all right why exactly does it do that the reason it does that B is because
when you’re using this thing called C out it’s called printing it out into a stream
and it pretty much takes all your characters and prints them out in the same stream so
whenever this stream is done you’re not telling it to move to a new line you’re saying print
out these characters and then stop right at the end of the sign so then this stream starts
printing out and again you’re not telling it anywhere to move to a new line computer
programs are very picky and they need to be told exactly what to do so in order move to
a new line we need to add a special symbol I guess you would say and the symbol is this
first of all you need to add another stream insertion operator and you can go ahead and
write E and DL what this means is end line and now when we go ahead and build and run
it check what happens it runs boy I love bacon and then it runs in line and before your stream
insertion operator would be right here but now since you set end line and go ahead and
end this line and it moves it to a next line so basically all you have to remember is anytime
you want to move to a new line use the character n L which is code word for end line now if
we’re saying alright that’s easy enough to remember so let me go ahead and show you something
that might be even easier for you inside your quotation marks if you go ahead and add backslash
n this is basically the same as adding a newline backslash n is the code I guess that all is
code for go to a new line so now it’s going to say boy I love bacon and then it’s going
to come across this thing called backslash and it’s going to say hold on what is this
backslash n oh I know what it is it’s the symbol to move to a new line so this is basically
just an alternative way to move to new lines the only difference is when n L you needed
to add another insertion operator and then right end L right there with a newline you
write it inside the quotation marks because it gets interpreted in different kind of ways
so let’s go ahead and build and run this and just verify that yep boy I love bacon newline
and ham so again like I said whenever you want to print text out on a new line you have
to be very specific with C++ and tell it exactly what to do and you can do this either using
the NL or the new line that goes inside the quotation marks so aside from this you might
think in alright so I print this out and I got this on next line but what if I wanted
a blank line in between these two well all you would have to do is write new line new
line and then when you go ahead and you build and run this oak say alright boy I love bacon
new line new line and then it printed out and ham simple enough pretty cool huh and
I guess one other thing I can show you is if you go ahead and do see out insertion operator
arm you can do things like this boy new line i new line love new line bacon new line so
you know you’re not only limited to you know one new line per statement you can have multiple
new lines in a single statement so now it says boy I love bacon pretty cool huh so that’s
all the basics I wanted you guys for this tutorial again I wanted to stress how to print
out text on the screen and also the stream insertion operator it’s basically this each
stream insertion resumes it’s printing out wherever the previous ones stopped so if you
want to move it to a new line you need to use one of these new line symbols either NL
or new line so that’s all I have for you guys in the soil now you should be a pro at printing
texts out on the screen so you know we’re not ready to bundle up this program and sell
it to Microsoft yet but hey let’s start so thank you guys for watching if you have any
questions go to my website the new boston comm and ask me on my forum so thank you guys
for watching and I uh I guess I’ll see you next video what is going on guys welcome back to your
fourth C++ tutorial and in this tutorial I’m going to be going over something called variables
because variables are such a huge part of C++ I can’t stress this enough so let’s go
ahead and tap into our memory banks and try to remember back in like seventh grade when
you first start learning about variables and if you can’t remember let me give you a brief
history lesson variables are basically just placeholders that’s all they are so let’s
go ahead and say we had a variable named X and we set it equal to bacon well then whenever
we would have a sentence like Bucky loves X we knew that x equals bacon so this man
Bucky loves bacon simple enough so let me give you guys another quick example say X
was equal to 7 so then when we had a statement like 5 plus X we knew that this meant 5 plus
7 simple enough so as you can see a variable is basically just a placeholder now let me
go ahead and show you guys how to make variables in C++ and C++ in order to make variables
you go ahead and first write the type of variable you’re going to be working with now I’m going
to be going over datatypes later but for now I want to give you guys one data type and
that’s I and T this stands for integer and an integer is pretty much like a whole number
like 683 540 it’s a number without a decimal place so now that you gave C++ the type of
data you’re going to be working with you need to go ahead and name your variable go ahead
and name it anything you want I’m going to go ahead and name mine tuna so now it has
a type and a name so now all we have to do is use the equal sign and set it equal to
a value so since this e has to be equal to a whole number let’s set equal to 6 it doesn’t
matter what whole number you set equal to but I just like 6 it’s easy to type so now
we have the variable tuna and C++ knows that it’s an integer and it’s equal to the value
of six so now what can we do with variables well just like we can print out text on the
screen we can also print out variables just as easy so if you went ahead and said C out
and just go ahead and print it out tuna we can go ahead and build and run this and it
would print out not the word tuna but it would print out six just like that so that’s pretty
cool and that’s the basics of variables and how we can use variables in C++ and actually
and this is sort of what I was planning on doing is building a simple calculator um let
me go ahead and I guess I have time for this let’s go ahead and make two variables and
I’ll show you guys another cool thing that you can do the C++ let’s go ahead and make
int we’ll call make a variable called a and we’ll set it equal to four let’s go ahead
and make another variable called B which is of course an integer because that’s the only
type we know right now and we’ll set this equal to you know 21 or something doesn’t
matter as long as they’re both numbers now let’s go ahead and make another variable called
a sum and this sum is going to be equal to a plus B so now you can see aside from let’s
go ahead and yeah that’s good enough so now you can see that aside from just assigning
variables numbers just like that you can also use basic arithmetic to set variables equal
to another value as well so now the value of 25 a plus B is going to be equal to sum
so now if you go ahead and print out C out some and we go ahead and we build and run
this check it out it prints out 25 on the screen pretty cool huh so again in this story
I want to show you guys that in order to make variables go ahead and write the datatype
what do you want the name of the variable or excuse me what you want the name of the
variable to be and what you want the value of variable to be and then aside from just
assigning variables basic values you can also use basic arithmetic like a plus B on B minus
a you could done that let’s see if you had B minus a let’s go ahead and see what the
value of that is 17 so again you can go ahead and set variables equal to basic arithmetic
using other variables and you can even do this sum equals 48 minus 7 again this isn’t
really the sum anymore it’s more of a proud buzz and not the product not the quotient
the difference there we go that’s what I and there we go I got it so again you understand
the basics of variables that’s what I’m trying to stress so in the next tutorial what I’m
going to be doing is I’m going to be showing you guys how to make a very basic calculator
and this is the first program we’re going to be building they actually interacts with
the user and it’s going to be awesome and you needed to understand variables before
I could show that to you guys so that’s why I had to go over this story but for now thank
you guys for watching don’t forget to subscribe and don’t forget to check out my website the
new Boston comm it’s awesome so anyways thank you guys for watching and I’ll see you next
video alright guys welcome back to your fifth C++
tutorial and in this tutorial since we now have the knowledge of variables within C++
let’s put that knowledge to use so let’s go ahead and make three variables right now but
I’m going to be making them a little bit different than before so in the last tutorial we had
a variable like a and you said equal to the value like 54 simple enough but I want to
tell you guys that whenever you first create a variable you have the option of assigning
the value then or assigning the value later on such as a equals 54 this is perfectly fine
and you can do it this way so whenever you first create a variable and you don’t assign
the value this is called declaring the variable declaring is just another name for creating
the variable so let’s go ahead and create three variables and by the way I’m going to
be making a basic computer program where the user enters two numbers such as like 2 & 3
and the computer is going to add them together and it’s going to give them the answer which
would be 5 so int a I’m going to have a variable called a and I’m going to have another variable
called B and what we’re going to be doing is we’re going to be calculating the sum or
in other words when you add the two numbers together the answer of that and storing it
in a variable called sum so we don’t have any of the values yet so that’s why we can’t
fill them in the users going to be entering the values for us so the first thing we want
to make in this program is just a little prompt on the screen the tell the user to enter a
number and we do that using C out and we just write enter a number pause and then let’s
go ahead and write newline and now we need a way for the user actually to input information
from their keyboard so here’s a neat little thing I can tell you see out took information
from your computer and output it to the screen but anytime you want to take information from
the computer or excuse me from the user and give it to the computer you need to use C
I n so C out basically takes information from the computer and gives it to the user cien
takes information from the user and gives it to the computer so if you’re right CIN
and by the way if you’re taking the test this is technically called the input stream object
and the little greater than greater than sign is called the stream exaction extraction operator
so you know make sure to fill that out on your multiple choice if you’re watching this
in the C++ class or something so anyways you can see that this is different from the C
out for two reasons first of all it’s C in instead of C out and also your arrows face
the opposite way that’s because you’re giving information to the computer instead of the
computer giving information to you so now what we can do is we’re going to enter a number
and whatever number we’re enter we need to give it a place to store that number well
we already have a variable right here so let’s go ahead and use that and use the variable
a so check this out it’s going to allow us to enter enter a number and whatever number
we enter it’s going to be stored in the variable a so for example if we enter the number three
a is going to be equal to three simple enough so now that we have that variable taken care
of we basically need to do the same thing again with the other variable B so C out will
say enter another number and just go ahead and we’ll just make a new line to make the
program easier to read and use that C n again but now when they enter a number we want that
value to be equal to the variable B so they’re going to enter two numbers one’s going to
be equal to a the other is going to be equal to B so now for the sum we don’t want them
to you know enter value for the sum we want to calculate that ourselves so in order to
do that just go ahead and write sum equals a plus B and now what your computer program
is going to do is it’s going to say RA I’m going to grab the very excuse me I’m going
to grab the value you put in for a and I’m going to grab the value you put in for B and
going to calculate that and I’m going to store that in the variable sum so for example if
you wrote ten and fifteen it’s going to say alright grab ten grab fifteen calculate the
sum of it which is 25 and store it in a variable called sum so now the sum is equal to the
answer and all we have to do now is print it out on the screen so see out will just
write the sum of those numbers is is it the sum of those numbers are or some of those
numbers is I think it is because it’s one song and again anytime you want to add more
than one line of text you could do it like this you could write a new line and then print
this out on another line but I want to add on the same line so I’m going to go ahead
and add another insertion operator just like that and just go ahead and write sum and then
add another one and just go ahead and write end line just so our program looks nice and
neat so basically well let me go ahead and run this and I’ll talk you guys through it
after I run it our computer program says enter a number Huss I’m going to enter 50 and then
go ahead and press Enter once I press enter 50 is going to be stored in the variable a
it says enter another number I’m going to enter 32 then I’m going to press ENTER and
that 32 is going to be stored in the variable B then it’s going to make our calculation
and print it out on the screen the sum of those numbers is 82 50 plus 32 is indeed 82
so remember what we did is basically this we made three variables that we’re going to
be using later on the first thing it said is just basically a prompt enter a number
then we entered 50 it stored 50 in the variable a using CN again CN is a way that the user
can give information to the computer program instead of the other way around so now a is
equal to 50 it gave us another prompt and we typed in 32 now B is equal to 32 so in
order to calculate the sum we took the sum variable and set it equal to the sum of 50
plus 32 so now some holds the value of 82 or 83 whatever I typed in I can’t remember
and then all we did is we printed it out on the screen and again we could have wrote three
different statements for this one C out for this one C out for this one C out for this
but in order to save us some time we can actually use this operator right here and print everything
out on one line so that is why our program ran the way that again let’s go ahead and
run it one more time enter a number 44 and 76 hit enter that some of those numbers is
120 pretty cool huh so that’s how you build a basic calculator in C++ using variables
and C out so now that you got that done on we can begin moving on to the next topic which
is I don’t know but trust me it’s going to be amazing so um you study this and once you
understand the basics of variables and CN you’re ready to move on to next video so for
now thank you guys for watching don’t forget to subscribe and I’ll see you in the next
tutorial what’s going on guys it’s Bucky and welcome
to your sixth C++ tutorial and in this tutorial I want to go over some concepts with variables
that are definitely going to help you later on so the first thing I want to go over is
this you remember in the last story where I told you guys that there are basically two
different ways to create variables the first way is to create a variable called tuner or
something and set it equal to value like 43 all on one line simple enough easy to understand
well what I also told you guys you can do is you can declare aka create it on one line
and set it equal to a value on another line such as that they’re both perfectly acceptable
ways to do things the one thing I want to stress is that whenever you do this way declare
on one line and set equal to value on another line you don’t need to write what type of
variable it is again once you give C++ the information of what type of data you’re working
with you don’t need to list it every time you’re working with the variable C++ will
remember that information and you know it’ll keep it so whenever you’re working with tuna
later on you don’t need to explicitly write int and actually you can’t or L so to give
you an error message so just make sure that you know just remember that don’t write int
twice on same variable so aside from that let me show you guys this in tuna equals 99
so later on and I didn’t tell you guys this but what you can do is if you set tuna equal
to something else like 76 whenever your computer creates a variable it sets aside a piece of
memory to store that value in this case 99 well whenever you create or excuse me whenever
you use that variable again and set it equal to a new value where your computer is going
to do is it’s going to wipe out the old value in order to make room for the new value so
now you said all right we have a variable called tuna and that’s equal to 99 well now
we have a variable called tuna and we want to set this equal to 76 well all right so
we’re going to try to print out tuna and our computer is going to say alright should I
print out 99 or 76 well since we created one originally that was 99 and then we overrode
it to make it 76 it’s going to print out 76 so let’s go ahead and build and run this and
see what we get 76 so basically whenever you have a value it’s placed in a memory location
and then whenever you give that variable a new value it’s going to overwrite the old
value and put it in the same location so I just want to tell you guys that this is how
you can overwrite variables so if you’re expecting 99 and you got 76 this is why so that’s actually
all I have for you guys for this story again the two things I want to stress is whenever
you’re creating a variable you only write the type once you don’t need to write it every
single time and then whenever you need to reassign a value you just go ahead and write
the variable name and not the type again and also whenever you’re overwriting a variable
you use the variable and it’s going to be equal to the latest value so whenever you
you know set this equal to 76 it’s going to wipe out the 99 and use the 76 so I just wanted
to stress those you know a couple memory concepts so then whenever we’re building programs later
on you might have a better understanding of how it works so for now that’s all I have
for you guys in the next tutorial you know how I said that we’re going to be going over
arithmetic like how to add two numbers together well there’s actually a bunch of different
arithmetic operators that we need to learn and that’s where we’re going to be covering
in the next tutorial but for now thank you guys for watching don’t forget to subscribe
and I’ll see you next video all righty guys welcome back to your seventh
C++ tutorial and in this tutorial as promised I’m going to be going over basic C++ arithmetic
now I don’t know I think it was like tutorials go I gave you guys a variable and I showed
you guys that instead of just setting it equal to a plain old value like this you can use
basic arithmetic like 4 plus 6 and then when you go ahead and you print it out X or anything
like that it actually calculated the value and printed out the answer so go ahead and
run this and you see that X now equals 10 4 plus 6 so aside from addition I want to
show you guys all the arithmetic operators that you can use and they’re a little bit
different than you might expect so let’s start with the easy ones first the first one is
subtraction and of course like 8 minus 4 this would be valid if we go ahead and build this
we saw that the answer would be 4 so addition subtraction easy enough but also aside from
addition subtraction you can use multiplication but make sure not to use X like that because
that will mess up in order to multiply in computer programming use the asterisks and
that’s the symbol above the eight on your keyboard and what this is going to do is multiply
these two numbers eight times four so now if you go ahead and run this we should get
32 and check it out 32 just like that so the well let’s go ahead and before I go over this
let’s go ahead and go over division in order to use the vision in computer programming
let’s go ahead and take 81 divided by 3 division the symbol for this is the forward slash so
let’s go ahead and take 81 divided by 3 and we get 27 just like that simple enough but
check this out what if we have something like 81 divided by 2 well this I mean it should
be 40 but it has a remainder of 1 so let’s go ahead and try to build and run this and
see what we get well it just has 40 and that’s not entirely right because it should have
a remainder of 1 with it so we’re saying all right we’re working with integers right here
and we any other data types so how can we get that remainder well whenever we want to
use the remainder we need to use something called the modulus operator and that is the
percent sign like this what this is going to do is it’s going to give you the remainder
of the answer it’s not going to say 40 remainder one it’s just going to give you that one so
let’s go ahead and build and run this and you can see that whenever we do 81 modulus
2 it’s going to give us the remainder which is 1 so let me show you guys a better example
on maybe 24 modulo 7 well we know that 21 or excuse me 24/7 is 3 with a reminder where
the remainder of 3 so let’s go ahead and build this in again it doesn’t say 3 remainder 3
it just says the remainder which is 3 simple enough so now that we know all the arithmetic
operators I can begin teaching guys about order of precedence now aside from just you
know one number of rithmetic operator another number you can have complex calculations like
6 times 4 plus 8 times 4 plus 9 divided by 20 and if we go ahead and build and run this
let’s see what we get 56 birthday in our a so what order is just doing this in is it
doing 6 times 4 and then plus 8 or is it doing you know 4 plus 8 and then times that by 6
because depending on how you do it it’s going to come out with a different result so the
basics is this if you remember way back in like seventh grade math you know something
called your associative property and that’s pretty much this anything that’s in parentheses
gets done first if you don’t have any parentheses it goes like this it does all the multiplication
in the vision which would be this this and this it’s going to do that first and then
it’s going to do you’re dissin addition and subtraction if you don’t have if you know
if you just have like four plus eight plus seven um you’re saying all right then is it
going to do a plus seven first or four plus eight first it really doesn’t matter it comes
out the same either way the only time you have varying answers is when you have like
four plus three times seven for example if you did four plus three it would be 7 times
7 would be 49 but whenever we run this whoa easy box we get 25 why is that because it
uses the associative property it actually multiplies first which is 7 times 3 and that’s
21 plus 4 which is 25 so again if you want to explicitly say 4 plus 3 first you need
to surround that in parentheses so now whenever we build and run this it’s going to run your
parentheses first and say our a 4 plus 3 equals 7 times 7 equals 49 so remember the rule is
this parentheses first then your multiplication in the vision and then your addition and subtraction
so remember that and if you get confused it’s called the associative property you should
have learned it in like eighth grade math or ninth grade math or maybe 7th grade I don’t
know I can’t remember it was a long time ago for now that’s all I want to talk to you guys
about basic arithmetic and how it works in C++ so for now that’s all you guys get so
thank you guys for watching this tutorial if you have any questions go to my website
the New Boston calm and click on forum and you can ask me there so one last time thank
you guys for watching don’t forget subscribe and I’ll see you next video
what is up guys is bucking welcome to your eighth C++ tutorial and in this story I want
to talk to you guys about something called the if statement now the if statement is a
way where we can have our program make very basic decisions what we’re going to be doing
is we’re going to be giving our computer program a test and it’s going to run that test if
the test is true then it’s going to run a certain bit of code if the test is false then
it’s not going to run any bit of code so depending on that test depends whether we run coda or
not so it sounds confusing but it’s actually incredibly simple so let me go ahead and I’ll
show you the syntax for if statement right now the syntax to make an if statement is
I F you just go ahead and type the word F and then go ahead and add parenthesis now
inside our parentheses is where we’re going to be making our test now after those parentheses
go ahead and add curly braces now this is called the body of the if statement and now
here is where we write the code to run if the test is true so let’s go ahead and make
a simple bit of code let’s go ahead and just write like C out armed lucky is awesome so
if our test is true it’s going to print out Bucky is awesome on the screen if the test
is false it’s not going to print out anything so let’s go ahead and let me talk to you guys
about the different types of tests the first test I want to show you guys is the greater
than test and this simply tests if one value is greater than another value so if v is greater
than three go ahead and run this code so 5 is indeed greater than 3 so this is true so
whenever we build and run this it’s going to say Bucky is awesome pretty cool huh so
now let’s go ahead and make something like umm is 1 greater than 3 so it’s going to test
this is 1 greater than 3 well this is actually false 1 is less than 3 so it’s not going to
run this bit of code so go ahead build and run this and as you can see it did not print
out Bucky is awesome on the screen how sad so of course we have the greater than test
we obviously have the less than test one lesson three well now this is going to be true so
it’s going to print out Bucky is awesome on the screen so let’s go ahead and run this
and indeed it prints out Bucky is awesome so aside from greater than and less than we
also have these two tests greater than or equals two so whenever we tested whether three
is greater than three this test is going to be false because three is not greater than
three three is actually equal to three well if we want to test if three is greater than
or equal to three we use greater than with the equal sign after it so now whenever you
run it is three greater than or equal to three yes three is equal to three so print it out
Bucky is awesome so aside from that we also have the less than or equal to so if you know
is one less than or equal to three yes one is less than three so it’s going to print
out Bucky is awesome so again what I want to show you guys is that you have greater
than less than greater than or equals two and less than or equals two so now those are
called the relational operators we also have two more tests that we can do and these are
called the Equality operators this is a really simple test it tests whether one value is
equal to another value or if one value is not equal to another value so in order to
do this go ahead and write is three equal to equal to three and you might be thinking
all right why don’t I just use one equal sign why didn’t they make it that way well whenever
we were creating variables such as like int x equals eight the one equal sign the single
equal sign is assigned for creating variables and assigning values so that’s why I decided
that two equal signs was sufficient for testing so remember a common mistake is to put one
equal sign in there and you’re going to get an error or something terrible is going to
happen so whenever you’re testing you need two equal signs so let’s go ahead and run
this test is three equal to three well yes it is so it’s going to print out Bucky is
awesome I’ll give it another test like is seven equal to three well no it’s not so it’s
not going to print out buck is awesome so that is one test is called the equal sign
equal sign or equal to test and there’s one more test I want to go over and that’s explanation
point equal sign and this is the key word for not equals to so is 8 not equal to or
in other words is 8 different than 8 well no it’s the same so it’s not going to print
out everything is 10 different than 8 well yes it is so that’s going to be true and it’s
going to print out Bucky is awesome so that is all the tests that I have for you guys
and that’s all the tests in C++ again to recap one last time greater than less than greater
than or equal to less than or equal to equal and not equal so now that we know all those
tests I probably should tell you guys this people typically don’t in by people I mean
computer programmers computer programmers typically don’t just test two numbers against
each other they usually have variables that they test against each other like int a equals
98 int B equals 76 and then they test something like is a arm greater than B well if we go
ahead and run this 98 is indeed greater than 76 so of course that’s true it’s going to
print out Bucky is awesome and aside from that you either have two variables to test
or you have a variable and a value like is 98 greater than 5 something like that we can
actually mix variables with values and that would be true as well so again you can either
have two values in here two variables or variable on one side and value on another side pretty
cool huh so that’s the basics of again relational operators are the greater than and less than
one and equality operators are the equals equals in explanation point equals so now
that we know how to make a basic if statement again to recap one last time if your test
and then the body and it’s going to run the test and if that code is true is going to
run the body that’s the basics of a if statement now that we understand that we can begin enhancing
our program a little bit more so in our next tutorial what I’m going to be doing is I’m
going to be having the user enter two numbers and we are going to be comparing those numbers
and depending on what numbers they entered run a certain bit of code so now as you can
see we’re just building and building to our knowledge of C++ to make better and better
programs so don’t skip over if statements because it’s a huge part of C++ so for now
that’s all I have for you guys so thank you guys for watching in the next tutorial we’re
going to be building that calculator thingy so it’s going to be awesome so thank you guys
for watching don’t forget to subscribe and don’t forget to check out my website what’s
going on guys it’s Bucky and welcome to your ninth C++ tutorial and in this tutorial I’m
going to be talking more about functions now I talked about this a little bit in like I
don’t know a couple tutorials ago but I want to talk about it again because I really want
to stress what functions are and why they are useful in C++ programming so if you remember
a function is basically a computer program is made up of a bunch of functions right now
we have one function right here and basically the job of a function is to do something so
inside function we can give our program a bunch of separate instructions all followed
by a semicolon so our main function that we already learned is the starting point for
every computer program every program starts with the function main so this is one function
right here but let’s go ahead and create another function and in order to do that go ahead
and separate from your main so before we’re coding everything inside the main function
but now we want to create another function so go outside main right here now the first
thing that we need is a return type and this is whenever people build functions they usually
make a calculation and give you back something for example if we had a function to calculate
the speed of my car it would give me back a number if we had a function to UM calculate
my weight it would give me back a number well sometimes functions make calculations and
sometimes when they don’t make calculations just as like printing something out on the
screen it doesn’t really need to calculate anything so sometimes functions are going
to return a number to you in that case you would use int and sometimes they just do stuff
like print something out on the screen and don’t return any answer to you whenever a
function doesn’t return anything to you just go ahead and put void and this means you know
function just go ahead and do your thing and I don’t want anything in return so the very
first thing you write is the return type of the function so this one you see it return
a number but this one is just going print something out on the screen so it’s going
to return void so after this you go ahead and you name your function this one is named
mein and you can actually name it anything you want this function is just going to print
something out on screen like I said so I’m just going to name it print something pretty
dumb I know but hey it’s my function I can do what I want so after this you see that
I added two little curly braces and those are actually called parameters um whenever
your function needs additional information you add parameters but we’re going to be covering
this in next tutorial for now just remember to add them and add nothing in between them
trust me I’m going to be covering this in next tutorial and I’m going to make real easy
to understand but after these parentheses go ahead and add two curly brackets and just
like I said before this is the body of your function for example if you had a function
called make peanut butter and jelly inside your body you would have a set of instructions
like get out the bread next instruction put peanut butter on one slice next instruction
put jelly on one slice lastly eat it well in the body of our function we give it each
instruction about what we want this function to do so in our function called print something
the only thing that we want this function to do is print something out on the screen
easy enough we already know how to do that so let’s go ahead and see out oops there we
go and what are we going to print out on the screen just right like ooh I am text on the
screen it’s very feminine function we have here but anyways then just go ahead and end
the line simple enough so now whenever we use this function we’re going to whenever
you use a function it’s called calling the function why they did that I don’t know but
um just remember whenever I say call the function I basically just mean use the function so
whenever we call this function all it’s going to do is print out ooh I am text on the screen
simple enough so let’s go ahead and remember since our computer program starts with main
we need to tell it in main that this is the function that we want to use so in order to
use or call a function all you do is this just go ahead and write print something empty
parameters and a semicolon so again anytime you want to use a function just go ahead and
type the name of the function empty parameters and a semicolon but check this out whenever
we try and build and run this we’re going to come and get an error this with this little
red rectangle means you can’t read my error message because it’s down here but says printing
something was not declared in the scope and you’re saying all right what did we do wrong
because Bucky told me that this function is built perfectly which it is but whenever it
tried to call it or use it right here it didn’t understand what was going on so why is our
compiler kind of confused well the reason is this computer programs work from the top
down it starts at the very top line and it runs downward until it gets to the very end
and then it quits so whenever our computer program was running it said this all right
include this file done use this done okay now start with main print something Oh what
the heck I never seen that before is this a function or trying to make a variable or
what the heck are we doing I don’t know what this means well that’s because whenever we
created our function we created it after we use that we need to tell C++ before we use
our function that is actually a function so we have a couple ways of doing this probably
the easiest way is just to do this go ahead and cut it and then above your main go ahead
and paste it and now C++ is going to think like this all right I’m going to include this
simple enough I’m going to use this easy enough and oh you made a function called print something
now okay I’ll remember that so now whenever I call my main and I come across this thing
called print something I know that it’s a function that you created so that’s why whenever
I build and run it now it’s going to run perfectly without any errors so that is basically how
a computer program works first you need to tell it that you are actually creating a function
before you can use that function so if you just try and use it it’s not going to know
what it is if you didn’t create it first sever saying all right I just remember to create
all my functions before I use them okay you can do that but let me give you an alternative
method of doing this if for some reason you like your main at the top and you like to
create your functions after well we already know that whenever you try to run this we’re
going to get an error but there is one thing that you can do and this is called function
prototyping and all this is is this trust me it’s the easiest thing ever go ahead and
select your a turn type your function name and the parentheses and go ahead and copy
that now above your main and under your using go ahead and paste this this is called the
function prototype and it’s pretty much a copy of your functions header and it basically
is saying this alright whenever you run this program now go ahead and include this include
this and then we’re going to this is called prototyping a function this is pretty much
telling our C++ program this alright later on you’re going to come across something called
print something now whenever you come across it we don’t want you to freak out like you
have before something us a whole bunch of error messages what we’re telling you right
now is we created a function called print something but you have to go find it and C++
is going to say oh all right now whenever I try to run my main and I come across this
function instead of giving you error message I’m going to know that this is a function
that you have somewhere in your program so I’m not going to freak out I know this is
a function I know to handle it accordingly so check it out whenever you try and build
and run this it works perfectly so that is why you need to do one of two things before
you use your function in main one you can prototype it or two you can just go ahead
and make your function before main either way whenever you run this function in main
it needs to know beforehand some way that this is a function so those are your two different
ways of doing that and again in order to create a function go ahead and type the return type
the function name and the parameters and again I’m going to be talking about these parameters
in the next tutorial so as long as you understand this and the basis of how to create a function
and how to use them in main you’re ready to move on to the next tutorial so thank you
guys for watching don’t forget to subscribe and I’ll see you guys in next tutorial what
is going on guys is bulky and welcome to your tenth C++ tutorial and in this tutorial I’m
going to be teaching guys how to create a function that uses parameters now a parameter
is quite simply additional information that that function needs in order to work so let
me give you guys a quick example of this say you made a function to calculate the age of
someone how old they were well some additional information you would need before you can
calculate that is what year were they born and once you have this information you can
calculate their age so let’s go ahead and now that we understand why a function would
need additional information let’s go ahead and create a simple and excuse me a simple
function to use additional information so let’s go ahead and create a function that
just prints out Bucky’s favorite number is blank um we might see Bucky’s favorite number
is 20 Bucky’s favorite number is 68 Bucky’s favorite number is 99 well since we don’t
know what my favorite number is yet we’re just going to go ahead and put a variable
in place for that number so since this isn’t going to be making any calculations really
we can just go ahead and use a void because that means return nothing so name your function
anything you want I’m just going to name my print crap since that’s all it’s going to
do print crap out on the screen and before we just added these empty parentheses when
nothing in between them and then we coded the body right here well whenever we use a
parameter you actually put that information right in your parentheses and check this out
you know how I said we didn’t know what that number is yet and that’s the additional information
that we need well since we don’t know what it is yet we just go ahead and we set it equal
to a variable and since this is an integer we go ahead and sequel to the variable int
X you can name your variable anything you want but I’m going to name mine X because
it’s really simple easy to work with so now we can go ahead and we can add the body of
this function and all we’re going to do in this function is pronounce screen Bucky’s
favorite number is that I spell everything wrong yup yep good to go Bucky’s favorite
number is X and then just go ahead and that line and now when our function runs is going
to run something like this if we say that our favorite number is 20 it’s going to say
Bucky’s favorite number is 20 if we say that our favorite number is 10 Bucky’s favorite
number is 10 so we can run this function a bunch of times and give it a different piece
of information each time and it’s going to work so let’s go ahead and remember in order
to use a function in your main you just go ahead and you type the function name and then
you add your parentheses and your semicolon at the end but whenever your function needs
an additional piece of information you need to give it that information inside the parentheses
so we know for making our function that it needs one integer in order to work so let’s
go ahead and in inside these parentheses give it one integer 20 so now what it’s going to
do is well let me go ahead and run this and show it to you guys Bucky’s favorite number
is 20 all right so what happened is this we said all right C++ program we want to use
this print crap method well it said all right in order to use this method I’m going to need
one integer so we gave it the integer 20 so then whenever in this function it came across
the variable X it just substituted 20 for that so whenever you’re making parameters
you first need to list what type of data and also a variable name so then whenever we give
it that variable its substitute that variable for X later on in the program so for example
let’s give it one more example like 854 well now we run it and it says Lucky’s favorite
number is 854 and that’s the beauty of parameters and functions it doesn’t need to be the same
every time so that’s why you can use variables so again what we did is we created a function
and it needed an additional piece of information which was an integer and since it didn’t know
the value yet it just went ahead and set it equal to a variable then later on in the body
whenever it came across that variable and knew whatever value you told it to substitute
for X that’s what value is substituted for X so we can run this 100 times with a hundred
different values if we want it’s going to substitute it for X each time and use that
information to plug it in so that’s the basics of how you use functions with parameter again
whenever you’re creating a parameter use the datatype and a variable name and whenever
you’re using the function don’t forget to pass in or give it whatever information it
needs so that’s pretty cool was simple enough we understand that but in the next story what
I’m going to be teaching you guys is how to use function that use multiple parameters
what if a function needs more than one piece of information in order to work well I’m going
to show you guys how to do that too and it’s a little different than this so you definitely
want to watch that so once you understand this and understand the basics behind functions
and parameters you’re ready to move on to the next tutorial so thank you guys for watching
don’t forget to subscribe and I’ll see you guys in the next video
oh hey guys what’s up welcome to your eleventh tutorial and in this tutorial I’m just going
I’m not going to do this whole tutorial in song maybe maybe another tutorial but I’m
not really in the mood this time so anyways and this is Toro what I’m going to be teaching
you guys is how to use functions that require multiple parameters so let me give you guys
a quick example of function that might require multiple parameters if you were making a calculator
and you wanted the user to enter two numbers well it did it doesn’t exactly know what those
numbers are yet so we go ahead and we use these numbers as parameters and functions
so as you can see instead of just a single piece of information it might need two numbers
or eight numbers or 20 numbers so in order to use multiple parameters in a single function
let me show you guys how to do that so let’s go ahead and make that program right now that
just goes in adds two numbers together well we can’t use void because right now it is
making calculation it’s going to calculate two numbers find the sum of them and return
to the return to us the answer so let’s go ahead and say all right since what you’re
going to be returning to us is an integer go ahead and type int and now what am I going
to name my function add numbers simple easy pretty clear so now we go ahead and make our
parameter list and body list here’s what I do before I add in my parameters I don’t know
why but you know I just do it so what we want to do is we already know that we can use one
parameter by going setting the datatype and giving it a variable name cinches int X well
we know that this function is going to need two numbers we already did one so how do we
say we want to use another number as well well anytime you use more than one parameter
you have to separate it with a comma and then you give it another piece of information that’s
going to need so it’s going to need two numbers one is going to be called X and is going to
be called why so let’s go ahead and now we have a function that takes two extra pieces
of information so we’re going to be getting it to integers x and y so now let’s go ahead
and use those numbers somehow so what we’re going to be doing is let’s go ahead and make
another variable called answer and this is just equal to the values of X plus y so say
that the user passed in two numbers on five and six well what we’re going to do now is
say all right five equals x six equals y so answer equals eleven so once we have that
answer calculated all we need to do now is we need to return it and this pretty much
means give it back so in order to return anything you just go ahead and write return and then
what you want to return well just return the answer so a return value is basically once
you make your calculation the final calculation that’s what they get back so in main we return
zero because computer programs know whenever you get zero back this means that your computer
program ran successfully well for this one we want to return the answer because that
means that we calculate the answer so that’s where we’re going to return back so now when
we’re using our function we can just go ahead and print out that value using C out and then
go ahead and write add numbers and then go ahead and write whatever numbers you want
to add and just like before whenever we were creating our function we had to separate these
variables with a comma whenever we’re using on this function later on in our main program
we separate the values we’re passing it in with a comma as well so 43 is going to be
equal to X and 86 is going to be equal to Y so let’s go ahead and go and run this and
check it out 129 pretty cool huh so what happened is this we said all right we want to print
out the return value of this right here so we can’t just go ahead and do this because
this is going to go ahead and calculate it but it’s not going to print anything out on
the screen so actually made that calculation but nothing’s printing out so in order to
print that out that’s why we need that see out to print out the return value of this
so anyways what we did is we said this all right we want to use this add numbers function
right here but it needs two integers in order to work and needs a x and a y so we said all
right you want two integers you got it you got a forty-three and an 86 let’s make some
magic happen baby so it took that 43 plug the end for X and 86 plug the end for Y and
it’s stored that value or excuse me to store the sum of the calculation in a variable called
answer and the answer was like I don’t know what it was but it took the answer and it
returned it back to you so says all right this is basically equal to 129 so this right
here is pretty much just a different way of saying 129 so I’m going to print out 129 and
that is how you use functions with multiple parameters for example if you wanted to add
four numbers together it would be the same thing in X&Y and it’s a and B and then you
just go ahead and use those values just like before so plus a plus B and then later on
when you needed more information 32:43 again why I’m doing this I just want to show you
guys that you’re not only limited to two parameters you can have as many parameters as you want
you just have to remember to separate them all with a comma so whether it’s 2 3 or 100
remember to separate them all with the comma in here and also when you’re actually using
the function out here so now you understand the basics of what a function is and how to
use multiple parameters single parameters you’re pretty much a pro with functions now
so once you understand this you’re ready to move on to my next so thank you guys for watching
don’t forget subscribe and I’ll see you guys later what is going on guys and gals welcome
to your 12th C++ tutorial and in this story I’m going to be giving guys an introduction
to classes and objects now classes are basically an easy way to group all your functions and
variables and the group a bunch of stuff together for example people like to group similar functions
together in the same class for example if you had a bunch of functions you know maybe
working with temperature or you had a bunch of other related functions you might want
to group them all in the same class and makes them easier to work with later on so then
you know if you have like a thousand functions you say all right where are all my temperature
functions because I need them now oh they’re in my temperature class so that is kind of
the usefulness of classes and the basics behind that so let’s go ahead and in order to create
a class we go ahead and make sure you’re outside your main first of all I want to say that
so I’m going to create mine before my main and in order to do this go ahead and type
the word class and now after this go ahead and add a space and name your class now people
typically name their class with uppercase letters and I’m going to name mine Bucky’s
class just like that and now go ahead and add curly braces in a semicolon at the end
now everything all your functions all your variables all your crap goes inside the body
of your class right here so basically all your crap that you want to go in the class
goes in the class body just like that and I believe whenever you create a class is called
declaring a class but you know if your teacher ever asks you to make sure you say that they’ll
probably be impressed so before we start adding a bunch of functions itself I want to talk
to you guys about something called an access specifier now sometimes you want to make functions
that you can only use in your class and sometimes you want to make functions that anybody can
use outside your class so if you want for example the main to be able to use your functions
inside your class you need to make your functions public if you don’t want main to be able to
use the functions inside your class then you do something called private but we’re going
to be going over that later for now just remember whenever use the public access specifier it
says that you can use it outside of the class so in order to use that public access specifier
just go ahead and type public with a colon not semi colon just a regular colon and this
means that anything you put after this you’re able to use it outside of this class so if
you didn’t have that then you would only be able to use your stuff right here and what’s
the use of that if you need to put it in main so anyways make sure you add public it’s called
an access specifier if you’re taking you know C++ test or something so now let’s go ahead
and we create a function just like before I’m just go ahead and print out something
on the screen so it’s not going to make any calculation so it’s just going to print something
out so void I’m going to name it cool saying and it’s not going to take any parameters
a really simple function here and all it’s going to do is print something out on the
screen now I got to think of a cool saying um preaching Wow totally messed that up preaching
to the choir I think that’s how you spell choir not sure hope I don’t look like a fool
in front on my subscriber so now just go ahead and end line and so all this function is going
to do is print out preaching to the choir on the screen simple enough so now you’re
saying alright this class is it looks good go we have the class declaration class body
we have public and we even have one function in our class this is a pretty sweet-looking
class so let’s go ahead and use this right now well now I want to talk to you guys about
the other part of this tutorial I told you guys I’m going to be talking about classes
and objects I already talked to you guys about classes now I want to talk to you guys about
objects an object is how you access the stuff inside of your class for example why do you
need to make objects at all why can’t you just write cool sayings just like this and
use it just like that well whenever you’re making big computer programs and you have
a bunch of different classes and a bunch of different functions and them may have this
cool sayings you may have a function that says cool sayings inside Bucky class you may
have another function cool sings inside tuna class you may have another function cool sayings
inside a class temperature class well how does C++ know what function you want to use
well it needs an object and the object basically tells it what class you’re working with so
in order to create an object it’s basically the same as creating a variable you go ahead
and you do this you type the name of your class which is Bucky’s class and then after
this you give your object a name and I’m just going to name my Bucky’s object just like
that and go ahead and add a semicolon and now this object Bucky’s object you use this
kind of as the key whenever you want to access the crap inside your class so now instead
of just writing on the function name we have to write the object name first like Bucky’s
object and then in order to access anything inside the class we need to use a dot separator
and a dot separator is basically our way of accessing the variables the functions all
that stuff so as you can see we’ll put the dot separator up and it already knows that
we’re what we want to do we want to access cool saying just like before just type the
name of the function and empty parameters and bam wham thank you ma’am we now have a
full working program so let me run this for you real quick to show you guys that it’s
working make sure I don’t have any errors and then I’ll talk you guys through it one
last time preaching to the choir works perfectly pretty cool huh so anyways people make classes
in order to group a similar function so if I had you know a program that just had a bunch
of cool sayings I’d have cool sayings cool sayings one cool things too I want to put
them all in the same class for now we only have one function because this is just an
example so we have a function that we put inside the class and make sure to make it
public that means that main can use it as well so we have a class and we made that by
making public and through a function inside it pretty cool so now we wanted to actually
use that function that’s inside the class well we can’t just go ahead and write cool
saying because if we had a bunch of different classes it wouldn’t know which function named
cool saying to use so it wouldn’t know what class we wanted to use so in order to use
that function we needed to make an object of that class so we’re saying all right we’re
making an object called bucky’s object and it is from the buckey’s class class so now
whenever we use buckey’s object it knows that we’re going to be working with bucky’s class
so we use buckey’s object cool saying and that knows and that’s basically our way of
telling c++ that we want to use the cool saying function and we want it to come from buckey’s
class so we use buckey’s object so that is why you need objects classes and yadda yadda
out of all that stuff so that is your basics of classes and objects and why you need to
make objects before you can use the stuff inside your class so I know this is kind of
confusing um but this is the basics behind object oriented programming so if you understand
this then if you understand this tutorial then the rest of your programming life is
going to be a whole lot more simple so watch this tutorial watch it again watch it again
until you understand exactly how to make classes and exactly why objects are useful and once
you do you’re ready to move on to next tutorial so for now thank you guys for watching you
guys have no idea how important the information you just learned was so anyways thank you
guys for watching don’t forget to subscribe if you have any questions about any of this
feel free to ask me on the new boston.com slash forum so yeah enough of me talking I’ll
see you next tutorial what is going on guys it’s Bucky and welcome to your 13th C++ tutorial
and in this story I’m going to be teaching guys how to use variables in classes but it’s
a little different than you guys might expect so just stick with me and I’m going to show
you guys the wrong way and then we’re going to show you guys the proper way to use variables
in your classes so before I even get started this tutorial I want to include the string
class as one of our files because we’re going to be working with strings in this tutorial
and if you can’t remember a string is just basically another word for text so whenever
we were outputting things like hey now brown cow this text is just another word for a string
so that’s what a string is to give you guys a quick little background on that so go ahead
and include the string class right under your iostream right above your namespace and now
like I said before I show you guys the proper way to use variables I want to show you guys
the wrong way because this is a common mistake that a lot of beginners do so you go ahead
and you added your access modifier public and say you wanted to make a name variable
low name is of course a string so go ahead and make a variable called name and set equal
to the type string this is good to go I mean so now we have a variable in our Bucky class
called name so again if you remember from les Sorel in order to use the things in that
class you first need to create an object so in order to create an object you type the
class name and then the name of your object I’m going to name mine Bo which does not stand
for body odor stands for Bucky’s object so then we have an object named Bo and with that
Bo object we can access variables by using the dot separator and just the name of your
variable so then once we have this variable name from the Bucky’s class we can go ahead
and see it equal to any name we want like Bucky roberts and then this variable right
here is equal to Bucky Roberts so then whenever we try to print it out Bo dot name it just
goes ahead and it prints out Bucky Roberts on the screen so you’re saying all right Bucky
that seemed to work pretty darn good to me so why did you say that that was an proper
way to do stuff well I don’t want to get into too much detail but basically I want to stress
this making class variables public is generally not good programming practice I don’t want
to get into like software engineering but basically whenever you make your variables
public it makes your programs really easy to mess up so typically what people like to
do is make their variables private and again there are some exceptions but typically you
want to make all your variables in your class private so you’re saying all right I remember
from less storia you told me that whenever I make variables private I can’t access in
them in main so whenever you try to run this program right here we get an error saying
all right this is just going to cause me a bunch of headaches what am I supposed to do
well if we remember what I said I said nothing outside your class can access these variables
so we need to build a public function inside your class to have access to those variables
so that is what we need to do so let’s go ahead and delete all this and we already made
our variable private so now let’s go ahead and make public functions to access them because
remember if we make a public function to access that variable we can use those public functions
outside our class in our main so in order to do that just go ahead and write public
and now everything you type under here is going to be public so we want to add two functions
the first one is just going to be first of all we’re going to want to do two things to
this variable name we want to be able to change it and then that means set it like set equal
to Bucky set equal to Tom set equal to Jessica so we want to be able to set the name and
our second function is just going to get the name we want to return the value Bucky we
want to return the value Tom just go whatever so we have two functions one to set the variable
and want to get the variable so in order to set the variable just go ahead and set equal
to void because it’s not returning anything we’re just changing the variable and what
people typically do is write set and then your variable name after that and after this
we’re going to go ahead and use this string and we’re going to call X because we’re going
to be passing in a string such as Bucky Roberts Jessica Thompson Tommy and that variable is
going to be equal to the value X so whenever we set a name we want to change it all we
need to do is set equal to the variable X and then it sets equal to whatever value we
pass in for example if we say set named Bucky it’s going to treat Bucky is X and now our
named variable is going to be equal Bucky simple enough so that is called our setter
method or excuse me setter function a method is actually if I say method and I actually
mean to say function just remember that I uh I’ve been teaching like 80 different computer
programming languages and in some languages they call method a function and vice versa
so I might get it mixed up but trust me a method and function is just the same thing
so anyways with that little background I now want to say this we already built our setter
function how to change that name variable well now we want Bo to a function to access
it and remember since we want that name in return we go ahead and type string because
the name is of course a string a bunch of text so what people typically do here is instead
of set they write get because they just want to get the name so go ahead and write get
name it’s not going to take any parameters and in the body this is probably the easiest
thing you’re ever going to do all you do is return the variable so check it out we don’t
have direct access to this variable so we have to go through functions in order to change
this variable we go ahead and we use this function in order just to get the value of
the variable we go ahead and we use this function so now instead of directly accessing this
variable we can do it through public functions so again like I said the only thing that have
the only thing that has access to these private variables are other things inside this class
so if we make those functions public then we can access that variable kind of like cheating
you know so in order to do this again in order to use those functions we again need an object
so Bucky’s class make another Bo object and now under this if I scroll up we can go ahead
and Bo we can use the functions from that class by using the object name and then the
dot separator and then we’ll just go ahead and the first thing we want to do is set a
value to that so go ahead and let me just copy this paste it right here and we’ll set
the name equal to something stupid like arm sir Bucky Wallis if you guys know where the
name Sir William Wallace comes from then leave me a comment below congratulations so now
this name variable is equal to the name sir Bucky Wallis so instead of accessing it directly
you can see that we went through a function that was public to do so so now what we can
do is we can just you know print it out or something stupid so see out and in order to
print it out we just can’t go ahead and write Bo dot name because we don’t have access to
that variable what we need to do is we need to go through the function get name just like
that and now what this function does is it returns the name it’s basically a way to make
a private a public simple as that so now when we go ahead and print it out building around
this as long as I don’t have any errors and I don’t check it out sir Bucky Wallis so let
me guys let me talk you through this one more time what we did is we wanted to access this
private name variable well we couldn’t access it directly or else it will give us an error
so what we need to do is we needed to build public functions one to change the name and
one to get the name so then later on in our program whenever we wanted to use those function
we went ahead and we created an object and then remember whenever you want to use anything
inside your class right the object dot separator function object separator function and then
we ran our program and it worked pretty darn good so that is the bad way to use variables
and also the proper way to use variables again you can put these variables is public but
it’s just bad programming practice and bad software engineering you’re never going to
get a job as computer engineer if you make your variables public so thank you guys for
watching this tutorial if you have any questions check out my website the new boston it’s pretty
sweet website I definitely want to check it out so I am going to go get a drink because
I’ve cottonmouth now and you guys study this story and I’ll see in the next video well
is up guys welcome to your 14th C++ tutorial and in this story I’m going to be going over
constructors what they are and why they are useful so first of all I want to tell you
guys what a constructor is a constructor is a function that gets called automatically
as soon as you create an object so before what we could do is we could create an object
and use that object to call functions from our class well now as soon as we create this
object it’s going to call a funk funk ssin code constructor we don’t need to explicitly
use that object to call a function it’s going to do automatically so you’re saying alright
Bucky you talk to talk time to walk the walk show me an example well creating a constructor
is incredibly simple to do so it’s basically the same as creating a function with a couple
weird rules first of all I need to tell you guys this constructors never have a return
type so you don’t need to type int or void or string or anything like that the only thing
you need to do is type the constructor name and the constructor name is always the same
as the class name the exact same so let’s go ahead and copy this and paste it right
here and that’s how your C++ program knows that it’s a constructor because once it comes
across a function with the same name it knows that that is the constructor so let’s go ahead
and we’ll add no parameters and in the body what do we want to happen as soon as we create
this object we didn’t have just you know maybe it will print something out on the screen
like this will get printed automatically pretty cool huh so now we have constructor that’s
going to get called automatically as soon as you create an object interesting all right
you keep saying that but what exactly does that mean well say we have an object from
Bucky class named Bo well before we would have to write Bo yadda yadda yadda whatever
we want to use from it but now as soon as we create this object this constructor is
going to get called so let’s go ahead build and run this and see this will get printed
on Mac app will blah blah blah automatically so you could see that before we needed to
use that object to call functions but this is a special function called a constructor
that gets called automatically as soon as you create that object so you’re saying all
right that’s pretty cool and interesting but why what didn’t I just you know print this
out on the screen or something else what’s the use of a constructor why would you even
want things to happen automatically well typically people don’t print things out in their constructors
actually if you see anyone printing something out in their constructor then they’re idiots
and you should slap them inside ahead because constructors the main reason that people make
constructors are to give variables an initial value so for example this class has a variable
called name but it might have another variable called age height weight favorite color favorite
sports theme have all these variables so as soon as you create an object from that class
you might want to assign a bunch of values to these variables right now so in order to
do that here is what you do whenever you want to take a value and assign it to a variable
you need to add a parameter in your constructor so let’s go ahead and assign a value to this
name right here and in order to do that we need to pass in string and just name it Z
or something so now what we can do is we’re going to be passing it in a string to be setting
it equal to name well we already have a function that does that called set name so in order
to do that just go ahead and call set name with the parameter of Z so now later on whenever
we want to use our constructor our constructor can take a parameter as well and for our parameter
let’s just go ahead and write something like lucky bucky roberts and now what’s going to
happen is this as soon as we create this object it’s going to call a constructor with a parameter
lucky bucky roberts so it’s going to go up see if it has a constructor oh I do and as
my parameter I need a string and for a string we give it lucky bucky roberts so anytime
has saw z it plugged in lucky bucky Reppert so it says all right your constructor you
told me to set the name equal to lucky lucky Roberts so it basically set the name equal
to lucky bug you’re Robert so pretty much lucky Bucky Roberts got passed into Z which
got passed in X which got passed in the name so that’s why our name was lucky Bucky Robert
so now that our name is lucky bug you’re Everts that gets kind of repetitive saying that but
anyways we can just go ahead and you know print it out on the screen or something so
of course to access anything in our class use the object and of course the get name
just like that function so let’s go ahead and build and around this and we have int
main we got a problem here hold on one second we find it oh I see what the heck how in the
world did I do that for some reason I must actually hit paste or something but I copy
the word name outside my class I just wrote the word name randomly in the middle of my
program so remembering not to put words randomly in am over your program or else it won’t run
so now let’s go ahead and build and run this and check it out lucky lucky Roberts so that
is the basics of a constructor how they work and also how you can add a parameter to your
constructor and basically the only times you wouldn’t want to add parameters to your instructors
is whenever you’re taking those variables that are inside your class and you want to
set them equal to values initially so that is basically why a constructor is useful whenever
you have a bunch of variables in your class and you wanted to set them equal to values
initially on that is how you can do that so here’s that example and you just printed it
our name so simple enough so aside from this I guess I have time to show you this one thing
you might be thinking all right so I have an object from this Bucky class named Bo and
its name is equal to lucky Bucky Robert so does that mean later on if I were to create
another object from the Bucky class name like Bo too and I set this name equal to armed
what’s even dumber than lucky Bucky Roberts um Sally Mick salad that’s that’s what’s on
my mind right now so does that mean that I’m going to overwrite this one well let’s go
ahead and print out armed bo2 dot get name just like this so let’s go ahead and print
it out and see what we get lucky Bucky Robertson Sally Mix Aled so why wanna stress is this
whenever you create an object from a class each object has its own set of variables so
this object Bo name is lucky Bucky Roberts this object is named Sally Mix Aled even though
they’re from the same class it’s not like changing the same variable so even though
we set this BL to equal to Sally mixed salad it didn’t overwrite this they’re just from
the same template pretty much so anyways what I wanted to stress is that you can create
multiple objects from the same class and they don’t overwrite each other they’re just a
sign they’re they’re assigned they’re each set variables am I saying that right they’re
each assigned a set of variables so yeah I just wanted to stress that before you get
in to objects in classes more and you get confused so anyways that’s just a little side
note stick with constructors and then once you’re good with constructors you’re ready
to move on to my next tutorial but for now thank you guys for watching and remember don’t
stick words into randomly middle of your program and yeah I guess I’ll see you next video what
is going on guys is Bucky and welcome to your 15th C++ tutorial and in this tutorial I’m
going to teach you guys how to separate your classes and actually add them in different
files so what programmers usually like to do is they like to separate their classes
into separate files because it just makes it easier to work with and manage and edit
later on so once we have like a huge program with 100 classes we don’t want all of those
classes in the same text file or l so just get way too huge so what they like to do is
separate it and it makes it easier to work on especially if you’re working with a couple
people maybe one person could work on each file at a time so anyways enough of me talking
let me show you guys how to do it so we have a basic program right here and we decided
that it’s time to add a new class well in order to do that go ahead and press file new
class and then we have a new pop-up box that pops up the first thing we have to do is name
our class and I’m just going to name mine burrito or something like that and by default
a couple things are checked and I don’t like to have them checked there’s something called
a destructor and we’re not going to deal with any of that yet so uncheck virtual destructor
and also has the structure and another thing I like to do is I like to keep all my class
files in the same directory as my main file so by default I think this is unchecked and
what you want to do is check it in that way all of your files are going to be in the same
directory or in the same folder as your main CPP file so once all your settings look the
same as mine go ahead and press create and what it’s going to do I’m just go ahead and
press yes yes to that stuff and what it’s going to do is it’s going to create a couple
files for you and you’re saying RA Bucky what the heck is all this I thought I was just
going to give me a blank file I was going to put my class in it and I was going to be
good to go well what we need to do is we create our class a little bit different than we were
creating in the last couple tutorials we’re going to be separating it in the two files
so by default codeblocks automatically created pretty much a template of the two files that
we need one is a dot H and one is a dot CPP the dot H is called the header file and the
dot CPP is called the C++ source file and this is we’re going to be writing the code
so first of all let me explain you guys what the header file is the header file is where
we’re going to be putting all of our classes function prototypes and variable declarations
so remember before I said that whenever you made a function you could go ahead and prototype
it above it main well we’re going to be putting all those prototypes in the header you’ll
see later on and aside from that this dot CPP file is where we’re going to be actually
building the functions themselves so this is pretty much where all the titles go and
this is where all the bodies go basically it’s a little more in-depth than that but
that’s a real basic summary so the first thing that I like to do is get rid of this private
and protected because we don’t have any of those yet and another thing that I like to
do first off is see this dot CPP file or CIN all right how come you needed this using namespace
and include in this CPP but you don’t end this CPP well actually you do so go ahead
and copy that and paste it right in there so again both of these dot CPP should have
the same things include iostream and using main page namespace STD so once you have this
paste it in your new burrito CPP you’re going to be good to go another thing that you’re
going to see is this class has already built our constructor for us remember in the last
tutorial I thought you guys had boat constructors what they are pretty much functions that get
called automatically well as you can see this already builds the function prototype and
starts building the function body for us so um by default it just has a comment in it
and I don’t even know if I told you guys what comments are but they’re pretty much just
little notes that you could write to yourself I’m have no effect on the program so let’s
go ahead and delete this and before I start writing something in this function I want
to talk to you guys about this the last thing that might confuse guys is the colon : what
this double colon is is it’s called the binary scope resolution operator so just remember
that for your test so that’s the technical term but what the point of it is is it’s basically
saying that this function which is called burrito because it’s a constructor excuse
me because it’s a constructor is a member of the class burrito so usually you’re going
to have this second one be a different name in this first one state burrito but anyways
it’s basically saying that this function is a member of this class and you use that the
two colons right there if you didn’t include those colons then it wouldn’t know what class
this burrito was supposed to belong to so it will give you a bunch of errors but now
whenever you’re making functions make sure you have the class before it separated by
two colons again it’s kind of weird seeing the first but you’ll get used to it so aside
from that let’s just go ahead and add something to the constructor so remember a constructor
gets called automatically as soon as we create the object so if we go ahead and just go ahead
and type like I am a banana or something like that something incredibly stupid so now let’s
go ahead and write end line and now whenever we create an object from this class it’s going
to print out I am a banana spelt it wrong but hey who cares so before I go ahead and
run this program I want to talk to you guys about one more thing you sent all right Bucky
I understand how to do this in it’s not like I made C++ or anything but just out of curiosity
why do you have to separate the dot H in dot CPP file why didn’t the users who ever made
C++ just let you make it all in one file well the reason behind that is this whenever you’re
going to you know give these functions to your friends to use or maybe you’re working
on it with a team of programmers or maybe you’re just distributing this for sale on
the internet well you’re going to want to compile this right here and change this all
in ones and zeros and where you’re going to be giving the other programmers is this and
raising you only give them basically the prototypes or the titles of the function is saying alright
I already built all these functions they work perfectly you don’t need to change it all
you need to do is to use the functions themselves so again whenever you’re distributing this
this is going to get compiled and the programmers are going to have access to your functions
but they aren’t going to be able to change the function bodies themselves so anyways
that’s a nice little background you didn’t really need to know that for this story of
just a nice you know a little tidbit of information so that’s why you split them up so the last
thing that you need to do is this in order to use objects from different classes in our
main CPP we need to include the headers so go ahead and just like your burrito dot CPP
included the header we need to include the header right here as well so now that we included
the header we can use objects from that class so let’s get rid of this see out hello world
because we don’t need it so now arm now that we have the constructor doing something printing
out I and banana we can go ahead and build an object in order to build object we just
go ahead and type the class name and some object like Bo and it should be good to go
so let’s go ahead and build and run this and see what happens I am a banana simple enough
so to recap one last time in order to make a new class you go to file new class you name
your class and make sure you check to have the header which is this and the implementation
file in the same directory as this so now what it did is it created all these files
for us automatically the only thing we needed to do is make sure that all of these includes
and all of these includes were the same and basically whenever we wanted to use this class
and our main CPP we needed to include the header and what this header did is it basically
included the whole class and then we were able to use all the functions variables and
all the stuff from that class so I know this is a little weird at first because we’re just
looking at real these small examples and they aren’t really useful don’t really tie together
to make a full computer program but later on once we are building a full computer program
you’ll begin to see why classes are useful why a header is super beneficial and why you
actually do want your function bodies in a separate file so again these really small
examples might be hard to see but I just want to break it down easy for you guys so you
understand the basics before we dive into something a lot bigger so for now thank you
guys for watching don’t forget to subscribe and I’ll see you guys in the next video what
is going on guys welcome back to your sixteenth C++ tutorial and in this lesson I want to
talk to you guys about something called the if statement now the if statement is basically
a simple way to have your computer program make a very simple decision so before when
we were programming we built simple programs and it pretty much just ran line after line
after line of code without question so that’s nice and all but sometimes you want to have
your computer program make a simple choice for example you might have some code that
you only want to run sometimes and you’re saying all right why would I even put it in
there if I only want to run it sometimes well for example if you’re making a website and
you had a message on there that said hello Bucky well you would only want to show that
message whenever I logged in you wouldn’t want to show it whenever a guy named Tommy
loggin or a girl named Jessica logged in so you would say if the user is Bucky then say
hello Bucky so you can see that unlike before when you just built a program in your granite
from top to bottom now we have different routes different choices that our computer program
can make and the basic basic decision that each computer can make is made using the if
statement so the syntax for the if statement is incredibly simple you go ahead and type
the keyword if and in between parentheses right after you’re going to give it a test
now go ahead and add curly brackets and if that test is true then it’s going to run the
code whatever you put in here if it’s not true then it’s not going to run the code so
you’re going to say all right um the test is is the user Bucky and the code would be
say hello Bucky on the screen or something like that so that’s basic example but of course
that isn’t real code so let’s go ahead and make a real program that actually works so
the basic what people typically do is they have a variable first of all so let’s go ahead
make int X and set it equal to 10 so now we have a variable equal to 10 they might want
to test this to see um is this variable equal to 10 so go ahead and say if X and now in
order to make these simple tests we use something called comparison operators and when you’re
testing numbers there are six different ways you can compare numbers I know you think is
equal or not equal but there’s a lot more first of all you can test is X equal to 10
is X not equal to 10 is X less than 10 greater than 10 less than or equal to or greater than
or equal to let’s go ahead and test it besides something other than 10 so we know X is equal
to 10 if we want to test if it’s the same as 10 we go ahead and write equal equal 10
and you’re saying all right first of all slow down this doesn’t make sense why would you
not just write if x equals 10 because that makes sense well the single equal sign is
they already reserved that for setting a value equal to the variable so if you did this right
here it would think that you’re trying to set X equal to 10 and your program is not
going to work right so that’s why whenever you’re comparing a variable and value you
need that double equal sign so this means all right test if X is equal to 10 and if
it is let’s just go ahead and give something stupid C out um OMG IM pritty that means pregnant
and ghetto talk so let’s go ahead and NL and let’s go ahead and run this and see what happens
build and run and on the screen that says OMG I am Bragi and why did it do this because
we had a variable X and it was equal to 10 and then we said all right if X is equal to
10 then go ahead and print this out on the screen and it was so print it out but if you
had something like if X is equal to you know 87 now we try and build and run it you see
that our computer program didn’t print it out because 10 is not equal to 87 so you’re
saying all right I know how to test whether a value is equal to a variable but what if
I want to test if they’re not equal I want to make sure that these values are not equal
well the sign for that is a explanation point equal sign this means all right if X is not
equal to 87 print this out and it’s going to print it out because it’s 10 equal to 87
no that doesn’t even make sense so it’s going to go ahead and print that out so that’s why
we get oMG I am per Iggy so aside from that let me go over the basics less-than and greater-than
if X is less than 87 is 10 less than 87 yes and by the way whenever we have a test and
that test is true we say true whenever the test is wrong we say false so it’s 10 less
than 87 yes 10 is less than 87 so we call this test true and it’s since it’s true it
goes ahead and prints it out oMG I am praying now let’s go ahead and be like is 10 greater
than 87 No 10 is not greater than 87 so that’s why it didn’t print it out right there so
aside from that there are two other simple ones I want to go over with you guys there
is less than or equal to so if you add something like is 10 less than 10 and you just had a
basic less time then you try to run it and it wouldn’t work because 10 is not less than
10 10 is the same as 10 well what if you want to test the value is less than or equal to
another variable well we go ahead and we have our variable 10 is it less than 10 nope or
is it equal to 10 yes so this is indeed going to print out oMG I am braggy and of course
there is greater than or equal to but I’m not even going to show you example because
it’s pretty self-explanatory what we have so aside from testing a variable against a
value I just want to tell you guys that you can go ahead and test two variables such as
43 so now we’re going to say all right is X arm greater than or equal to Y aka is 10
greater than or equal to 43 let’s go ahead and go and run that and we see that 10 is
not greater and it’s not equal to 43 so we can test two variables and we can even test
two numbers like is five greater or equal to three and we go ahead and run that and
we print out oMG I am pretty but this testing two values is the least common thing people
I don’t want to say always but I’ve never seen it where people had two values in here
it’s either one variable or two variables if we go ahead and we just had this code right
here then it wouldn’t make sense just to forget this whole if statement just like that unless
you don’t know how to compare numbers then you would have a problem then you shouldn’t
be watching this story but for now I just wanted to talk to you guys about the if statement
and how we can use it to make a basic decision again technical terms whenever your test is
positive we call that a true test whenever your test is negative and it doesn’t make
sense we call that false so the code inside the if block is only going to run when your
test is true as you just saw and again we have two equal signs for equals explanation
point equals means not equals greater than less than greater than equal to less than
or equal to so now you are pro with the if statement so thank you guys for watching don’t
forget to subscribe and yeah see you next story what is going on guys welcome back to
your 17th C++ tutorial and in this tutorial I’m going to be covering something called
the if-else statement now let’s go ahead and first I want to build a basic if statement
because an FL statement is pretty much like an enhanced if statement so let’s go ahead
and use the knowledge or the information that we learned in the last tutorial and build
on it so let’s go ahead and make a simple program we have a variable called age and
go ahead and set it equal to whatever your ages now go ahead and make a basic if statement
now if you remember from the left story all an if statement does is it runs a simple test
if that test is true it runs a bit of code if that test is false it doesn’t do anything
so let’s go ahead and test age say if age is greater than 60 let’s go ahead and say
um C out Wow you are old so if the user is over 60 let’s go ahead and say you are old
and now let’s go ahead and end that line run this program and see what we get we get nothing
and that is because I’m 24 so this program does nothing but you build this program you
show it to your boss and you’re like hey where’s my raise in there like wow this program is
impressive in all however wouldn’t it be nice if we could give a message to the user no
matter how old they are what if instead of skipping this if the test is false let’s give
them all an alternative message like if anyone’s over 60 we’re going to tell them they’re old
if anyone is under 60 we’re going to tell them hey get a job so saying alright um I
suppose I could put another if statement and then Ray age is less than 60 but you know
what that isn’t the best way to do things I’m going to show you guys something called
the if-else statement so the if statement basically ran a simple test and if that test
is true it ran this bit of code and if it was false it did nothing it skipped it but
now we want to say all right else if the test is false then we want to run this bit of code
so either way it’s to run a bit of code either way if it’s true it’s going to run this bit
of code let me just go ahead and copy that if it’s false it’s going to run this bit of
code but let’s not put while you’re old for either one if they’re under 60 let’s go ahead
and write you are young get a job so now when we go ahead and run our program unlike before
when it says our right since this test isn’t true I’m just going to skip it it says all
right this tests is it true so what am I supposed to do it’s false well I’m supposed to do the
else so let’s go ahead and nothing be talking run it and it says you are young get a job
and of course if the user is like 87 we run the program and they say wow you are old so
now you can see instead of just having a basic if statement where it was either run a bit
of code or do nothing we now have the option of something happens for true and something
happens for false so now either way your computer program is going to do something so that is
the basics of an if-else statement so now that you guys know that let me go even deeper
I said that you can go ahead and run a line of code inside the if statement well you can
also run multiple lines of code just by putting them under each other so if we go ahead and
we copy this we can go ahead and paste it paste it paste it it doesn’t matter you can
go ahead and run it four times as long as it’s in between those braces right there server
saying all right what if I wanted to do something like this what if not only I wanted to check
if they were over 60 but if they were over 60 I wanted to check if they were over a hundred
so let’s go ahead and do that so what I want to want to teach you guys now is that inside
if statements instead of just running simple lines of code you can also put additional
if statements this is called nesting of statements so let’s go ahead and say if and remember
this code is only going to run if the user is over 60 so now we want to test if the user
is over 100 and then we’re going to write C out armed why are you still alive I know
this is harsh but hey just trying to get the point across so now let’s go ahead and go
around this bit of code right now so our program is going to say this alright if the age is
over 60 which it is I’m going to run this bit of code right here and in this bit of
code I’m going to test if the user is over 100 well they’re not over 100 but they are
over 60 so nothing is going to happen so we’re going to run this nothing is going to happen
but say our user was 178 pretty old huh well we’re going to say all right is your age over
60 yes so now go here is your aive is your age over 100 yes it is so now run this bit
of code right here and we’re going to go ahead and run it and it’s going to say why are you
still a lot because come on guys let’s face it if you’re 178 years old you should be dead
just saying so now we get begin to see how we can make complex tests and very complex
programs and I know that I it’s called nesting and if statement inside another if statement
but you can also nest an if-else in here and you can also nest another if in here and you
what you can do is you can nest if inside of if inside of if as many times as you want
but say you wanted to test if the user was over 10 then over 20 then over 30 then no
to 40 and the 50 well there’s even a more simple way to do this than nesting a thousand
different ifs inside of each other and I’ll probably be teaching you that in the upcoming
tutorials but for now what I want you guys to take away from this tutorial is instead
of a basic if statement where if the test is false then it just skips the code you can
now add an else and what this does is it gives it an alternative choice and also a side from
that you can also nest if inside other ifs so that way you can basically test two things
and if two things are true run a bit of code so as long as you understand those concepts
you should be good go so for now thank you guys for watching don’t forget to subscribe
and yeah if you have any questions send me a message on my website the new Boston calm
and I’ll be glad to answer them for you so thank you for watching don’t forget sub and
I will see you next story what is going on guys welcome to your 18th C++ tutorial and
in this is rural I’m going to talk to you guys about a while loop and every saying alright
Bucky take a step back I don’t even know what a loop is and now you’re trying to talk to
me about special kind of loop alright let me step back a loop is basically in computer
programming if you want to take a certain bit of code and run it over and over and over
again you can go ahead and you can just type it on like a hundred different lines and no
keep running or you can throw it in something called a loop and what a loop does it says
alright write this code only one time and I’m going to run it as many times as you want
me to so for example instead of say we wanted to print hello on the screen 100 times instead
of writing C out hello on a hundred different lines we can only write it on one line and
loop it a hundred times so it’s a much easier way to make pieces of code around multiple
times so let me show you guys an example and by the way now I can talk to you guys about
a while loop there are many different types of loops a while loop is pretty much the most
basic type of loop in my opinion it’s the easiest so that’s why I’m showing this to
you guys first so let’s go ahead and I’ll show you guys an example go ahead and make
a variable called bacon and set it equal to zero this syntax for a while loop is this
we’re going to say while and then in the parentheses right after we’re going to give it a test
just like the if statement we’re going to test this bacon is less than or equal to five
go ahead and run this bit of code over and over again so let’s go ahead and just run
C out armed bacon is and then now say the value of bacon and then end line so now if
you were go ahead and run this you would say alright run it bacon to zero bacon to zero
bacon to zero and you can see that it keeps printing it out over and over by now it probably
printed it out like 20,000 times so you’re saying all right it’s kind of useful I guess
but would it be more useful if this loop would end for example if we only wanted to run it
20 times how would we go about doing that well sometimes people do want loops that never
end but typically they don’t typically you want to only have this code run like five
times or ten times or 15 times so how do we control exactly how many times it runs well
in order to do that we usually add a line of code like this again what this test is
going to do is test is bacon less than or equal to 5 and since the value is 0 right
now this test is always going to be true so in order to make this test false in this loop
to stop running we need to change the value of bacon at some point so it is equal to 5
or greater than 5 and in order to do that go ahead and make bacon equals bacon plus
1 now each time we run gloop is going to go ahead and do its thing right here and then
when it’s done it’s going to add 1 to the value of bacon so the first time it runs it’s
going to be 0 and then it’s going to add 1 to it so then it’s going to be 1 then it’s
going to be 2 3 4 5 and by the time it gets to 5 it’s going to change it to 6 and not
run so let me go ahead and show you guys making 0 1 2 3 4 5 so we already know why bacon rain
in this case when bacon was equal to 0 it was less than or equal to 5 1 yup – yup 3
4 5 is 5 less than or equal to 5 yep so it printed that out but then whenever it said
all right the value of bacon is 5 so I’m going to print out bacon is 5 and then I’m going
to add 1 to bacon so now bacon equals 6 all right let’s try this loop again is 6 less
than or equal to 5 no finally the test is false so I can quit running my dumb loop so
that is how you add um again like I said and when okay take a take a breath Bucky you’re
jumping too fast sometimes people want loops to run forever and when they do you do something
like the first example I told you but typically and I want to say like 95% of the time people
build loops that they only want to run a certain amount of times for example five times if
you only wanted to run at five times or actually it was six in this case you would add something
called this line right here and this pretty much just changes the value of bacon so at
some point that test has to be false if you wanted to run it 20 times you would go ahead
and just change that to 20 and check it out bacon is 1 2 3 4 5 all the way to 20 pretty
cool huh so you can change how often your loop runs by a couple things it’s starting
point it’s ending point or how much you increment that value by again you can add 3 to each
time if you want and then you get something like this one at 3 2 4 7 10 13 16 19 and when
you added 3 to that and it was 22 well 22 is indeed not less than or equal to 20 so
that’s when it stopped the loop so that is the basics of a while loop again all the while
loop does is take a certain bit of code and runs it over and over and over again until
the test is false and again like I said at the very beginning of this tutorial there
are many different loops that allow you to take a bit of code and run it over and over
and over again a while loop is pretty much just the most basic kind so make sure whenever
building your while loops not to forget to change that value or loops going to run forever
and who wants that so for now thank you guys for watching I’m actually going to go make
some bacon right now so you guys sit tight re-watch this story and once you’re good once
you understand the syntax of how a while loop works you’re ready to move on to my next video
so again thank you don’t forget to subscribe and I’ll see in the next video what’s going
on guys is Bucky and welcome to your 19th C++ tutorial and you know what all these nice
little quick examples are fine and dandy but let’s go ahead and build a program that is
actually somewhat useful a program that you can use and you know maybe your teacher assigned
you something or maybe your boss came to you and is like you know what Bucky I want you
to make a program that allows the user to enter five different numbers and then I want
you to total the numbers for them but you have to use a while loop so we’re saying alright
no problem so here is how I would go about making this program and by the way this is
some nice little programming tips you start with the basics of your program and you do
it in little baby steps so first of all I want to make a loop that runs five different
times so what I would do is make a loop int X set this variable equal to one this is just
going to be the looping variable while X is less than or equal to five let’s just go ahead
and print out something make sure our loop is working see out X end line so that’s going
to print out X and you know how I said that in order to add 1 to X each time you go ahead
and write x equals x plus 1 well you can do that but let me let you guys in on a little
secret if you go ahead and write x plus plus this is the exact same as writing x plus x
equals x plus 1 it’s just a quicker and easier way to do things and that’s actually where
the name c++ came from they wanted it to be queue in you know anyway so that’s the history
c++ it came from an old language called C and they added two little pulse signs to it
thought it was cute so anyways that’s your history C but what we’re worried about is
making sure that we have a working loop that runs 5 times so let’s go ahead and run this
so all right 1 2 3 4 5 our lip is working perfectly we can now move on to next set and
by the way this was just a tester line we don’t really need that in our program so Centauri
our boss told us to make a program so the user can enter 5 for numbers so we already
know that the CIN is the way that the user can enter a number but we probably need to
store that number in variable so let’s go ahead and store that number in a variable
number and since we don’t know the value yet we can’t set that equal to anything yet so
what we would do in order to have the user enter a number five times is inside this loop
go ahead and write CIN number now our loop is going to run five times and the user is
going to enter a number each time before the program quits so what I would do now is test
the program again just to make sure it’s working all right 43:54 66 2 & 3 enter our programs
done so now we see not only do we got a loop that runs five times but we also have the
user enter five different numbers so our program is working five and dandy so far service NRA
now what’s the last thing he said oh yeah he wanted to find a sum of all those numbers
and printed it out to the user or something so saying all right now not only a number
variable but we need another variable to hold the sum or the total of all those numbers
so what I would do is go ahead and put int total and by default their total is going
to be zero because they didn’t enter any numbers yet that’s what we know so now what we can
do is this you might be thinking this so just go ahead and set total equal to number but
whenever we run this it’s not going to work because say they enter ten the first time
we’re then total is equal to ten okay it’s working fine so far but the next time they
enter eight and we are expecting 18 well this isn’t being added the total total is now being
changed to eight so instead of being 18 it’s just a so this isn’t going to work so what
we need to do is do this total equals the old total plus the new number so now what
happens is this whenever they enter 10 it’s going to be 0 plus 10 total is equal to 10
the next time they enter 8 8 equals number so 10 plus 8 equals the new total of 18 so
let’s go ahead and once this loop is done running let’s go ahead and just give them
a nice little print out on the screen on your total is total and might as well end that
line why not so let’s go ahead and run this program and now let’s go ahead and run it
and make sure it works so let’s go ahead and put 10 enter 8 enter 2 enter 5 enter 5 enter
your total is 30 so all right let’s go review this one last time it seems that everything
is working fine let’s just go ahead and make sure well I guess I can cover that up that
says end line there now I’m covering it up so basically the first time the first thing
we did is we got a loop that worked then we wanted to have the user enter a number and
then we did something with that number so what happened is this the first time the user
entered a number it was 10 so the total which was zero before is now equal to zero plus
10 so now total equals 10 the next number they entered was 8 so now the new total is
10 plus 8 which was 18 the next number that entered was 2 so 18 plus 2 now total equals
20 next number was 5 now it equals 25 the last one is 5 also now it equals 30 so this
is basically saying all right take the new value of total and set it equal to the old
value plus the number that they entered and at the end we just said your total is 30 works
pretty good so let’s go ahead and just to make sure that it’s working good let’s really
test this add some big numbers like this why why do I keep entering the same number each
time and so you know okay I’m not going to check this in my head but you guys can go
ahead and check that on a calculator and if it works then it means we just built the best
calculator on earth so I’m actually going to go ahead and copy this and email this the
Microsoft and hopefully they pay me you know just a couple million dollars for it so you
go ahead and what I want to stress in this tutorial is this not only this is how you
build a basic program to you know it’s just an example of why a while loop would be useful
but also whenever you’re building programs take it in baby steps do the most basic thing
first and then once you got networking going to the next step and then one stat work and
you can go on to the final step and now we can do something like maybe we can find the
average of all these number or maybe we can find the product or the quotient or something
stupid so again take your program and baby steps that way you don’t try to build a whole
program and then at the end when you got ten different errors you’re saying all right what
went wrong so that is the basics of not only how to be a good programmer but also how to
use the while loop in a meaningful way so for now thank you guys for watching um like
I said just going to go ahead cut this and paste it to Microsoft and yeah I’ll see you
in the next tutorial when I’m a millionaire what’s going on guys welcome to your 20th
tutorial and in this story I’m going to be teaching you guys how to build a Sentinel
controlled program now let me explain you guys what that is you remember in the last
tutorial where we built that program and we ran a loop five times and each time we allow
the user to enter a number well sometimes we’re going to want to build programs where
we don’t know how many times the user wants to enter a number so if you like we’re calculating
the average grades for kids in your class you know how many kids are in your class if
you are calculating the average age of like people in your family you know how many people
in your family but if we build a program and we just sold it to everyone in the world everyone’s
family has a different size every teachers class size is different so we don’t know exactly
how long to make that loop last so what we do instead is we give a special code and this
code is usually something like negative one and we use this because ages in weights and
you know number of things cannot be negative one so whenever we answer negative one this
is when our program knows to stop and you know make its calculations or do whatever
it does so let’s go ahead and I’m going to be building a program that we enter the ages
of a bunch of people how many we don’t know yet but we’re going to enter the ages of a
bunch of people what our program is going to do is figure out how many people we entered
and calculate the average age of all those people so let’s go ahead and it’s pretty much
an age average er if I was to make this program I would name an age average er 5.0 Wi-Fi point
no I don’t know it’s just like five so let’s go ahead and we’re going to need some variables
the first variable we need is some place for the user to enter and store that age each
age at a time so they’re going to enter someone’s age like 32 we’re going to store in the variable
age after this we need some way to keep track of all those ages a total so if they enter
32 then 32 we want this this sort of total which would be 64 because we’re going to be
averaging it later on so int just name it like age total and this of course is equal
to zero first but it’s going to be the total of everyone’s age combined and the third thing
we need to keep track is how many people did they enter so you take the total over how
many people they entered and this gives us the average age so int number of people entered
and I don’t really like making variables that long because it’s easy to make a typo the
longer your variable is but it’s descriptive for this story so the first thing we want
to do is just write a prompt on the screen see out um enter first person’s age or negative
one to quit so what we’re going to be doing is our code is negative one whenever we get
the input negative one from the user we know we know that they want to quit the program
so let’s go ahead and then give them something to enter age so they’re going to enter a number
and it’s going to be stored in the variable age so now we just need to test make sure
it’s not equal to negative run and we need to loop this program as many times as possible
as long as the value is not equal to negative one so as long as age is not equal to negative
one go ahead and run this program and at the heart of this program is this we’re going
to be doing is we’re going to be taking that age total and it’s going to be a running total
of the old age total plus the new value that they entered for age so the first time say
they enter six is going to be 0 plus 6 the next time they enter 10 is going to be 6 plus
10 so the total would be 16 so then when we divide it by the number of people which would
be 2 their average age is 8 so let’s go ahead and now each time this loop runs it means
that they entered a new person so let’s go ahead and just write number of people entered
plus plus because remember plus plus is the same as adding one to it each time so now
we said all right our H total is good and the number of people entered is good so now
every time we run this loop we want to give them another opportunity to hit negative one
or enter someone’s age so make another C out and just write something like enter next person’s
age or negative one to quit so right now they’re going to enter negative one or someone’s age
and let’s just go ahead and end that line so now semicolon there we go so now we’re
going to store that value an age so they’re going to keep running this and every time
it runs they’re going to have a chance to input input an age what this loop is going
to do is check and it’s going to run the loop over and over and over and over again until
the user enters negative one once they enter negative one is going to say all right that’s
my sign to bail out of this loop and go ahead and make my calculations so for calculations
let’s first go ahead and make sure our program is going to work properly by saying let’s
first tell them number of people entered and I know I spelled that wrong big deal who cares
and now we just output number of people entered looks good in line and after this what we
want to do is we want to find the average age of all those people so C out average h
and then we just go ahead and the formula for average age is age total over number number
of people entered for example if your h total is a hundred and you enter ten people then
the average age would be ten so let’s go ahead and run this program first to see if it works
see if I forgot any semicolons or anything all right so the program is up and running
it looks good so what I like to do whenever I build programs like this is I like to enter
values that I already can calculate in my head just to make sure that computer is calculating
so let’s go ahead and enter a person with the age of 10 the person with the age of 20
and a person with the age of 30 now I know that whenever I average these together 10
20 and 30 well that’s 60 so 60 divided by 3 is 20 so I’m going to be expecting 20 and
that that way I know that my program is working correctly so let’s go ahead and quit by hitting
negative 1 and go ahead and hit enter and it says number of people entered 3 which is
correct 10 20 and 30 average age of them is 20 because 60 divided by 3 is 20 so that is
pretty cool and our program works fine number of people entered it got that right and it
calculated their average age right so that’s what I like to do before I enter big numbers
like 87 68 5 and all those like fives a real big number so anyways how our program work
is this what we did is we built three variables well let me just go ahead and skip to the
good stuff it says alright enter the first person’s age and we went ahead and we entered
10 right here so it says alright is 10 equal to negative 1 nope so let’s go ahead and run
this right here I’m going to go ahead and store 10 and H total and change the number
of people enter it to 1 and now go ahead and give me another age well we went ahead and
we enter 20 so now 20 plus 10 our age total is now 30 and our number of people entered
was 2 so it said all right give it to we enter 30 so now our age total was 30 plus 30 which
is 60 and number of people entered was 3 so then they said are 8 now give me another age
or negative 1 to quit we gave it negative 1 to quit so what bailed out of this loop
and it went ahead and is make the calculations for this calculation it just printed out the
number of people entered which was 3 and 4 this one is said the H total which was 60
divided by the number of people entered which was of course 360 over 3 is of course 20 so
again that is called a sentinel control loop because we can run this program in it doesn’t
matter how many numbers we enter you know we’re not limited to 5 or 10 or anything as
soon as we hit negative 1 it calculates how many people we entered and their average age
so again this is a much more dynamic program in the sense that you’re not only limited
a certain number but you can disperse this you know maybe a bunch of different types
of people and it doesn’t matter their sample size any size any amount of people in this
program is still going to continue to work fine so that is the beauty of a central control
program these are the loops that you’re going to be using more often rather than you know
a fixed number so get used to it become familiar with it and once you are you’re good go so
for now that’s all I have for you guys in the next tutorial don’t know what I’m going
to be covering but my throat is dry now so I need to go get a drink water and you need
to watch more of my videos or something I don’t know so thank you guys for watching
don’t forget subscribe and I’ll see you next time what’s going on guys welcome to your
21st C++ tutorial and before I even get started with the story I just want to mention one
thing that you guys probably notice already and that is I don’t have return 0 at the end
of my main and that is because I just found out that well let me say this in the old C++
ways you had to have that return 0 statement but now the new version or whatever whenever
you don’t include return 0 at the end of your main C++ just assumes you return 0 after the
last statement so you know if you go ahead and type your program you can either put it
if you’re you know old-school programmer or you cannot include it and it would just assume
you have return 0 but for the rest of these stories I’m going to exclude it so you know
don’t forget that it isn’t there so anyways let’s go ahead and get to the heart of the
story and that’s assignment operators and increment operators now we already know well
let’s go ahead and get a variable first set X equal to 10 looks good so we already know
we can do something like this X plus equals 10 and this is basically the same as writing
x equals x plus 10 so then whenever we let me just go ahead and this line and print it
out we go ahead and we get 20 just like that so what it does is it takes to this value
adds it to X and sets equal to the new X variable so I already told you guys plus equals is
a shortcut I also want to go over all of them so when you see them in program you don’t
get confused X well let’s change this to 5 it’s weird if they’re the same number so X
minus equal 5 equals 10 minus 5 and assigns that to the new value of x so let’s go ahead
and see that that is 5 now if we go ahead and see multiplies equals what this would
do is do 10 times 5 and set it equal to 50 and the last one without getting in the weird
stuff is divide equals 5 and this of course is 2 so what this does is basically takes
the old value of x it divides it by this value and sets it equal to the new value of x so
aside from that there’s one more and I rarely use it this way I hate writing in like this
and I hate seeing other people rate it like this because it’s confusing this is going
to be a bad example because ten divided by five is two with no remainder so it will be
zero but if we go ahead and throw like three something like this what this does is it takes
the old value of X divides it by three and it stores the remainder in one so now X is
going to be equal to one right like that so those are all your assignment operators plus
minus multiply divide and modulus which is remainder so now whenever you come across
them in programming you’ll understand what they mean so now what I want to talk to you
guys about is those increment operators so let’s go ahead and get new value 20 that’s
a little bit better it’s my favorite number so you know how I told you guys that you could
do something like this X plus plus and this was the same thing as adding one to X simple
enough right not exactly because there are two different versions of this increment operator
and I’m going to show you the difference with little differences between both so let’s go
ahead and see out X n line let’s go ahead and run this and take a look at what happens
so our initial value of X was equal to 20 and then we ran this line of code and it printed
20 all right and then we ran this line and it was equal to 21 well what the increment
operator is doing whenever you write variable with plus plus after it it’s going to go ahead
and run this line of code and then it’s going to add 1 to that variable so runs this line
of code X is still 20 then when it gets done running it assigns 21 to X and then it runs
this one so that’s why it was 20 21 so saying are a simple enough what’s the other version
the other version is this whenever the increment operator is before the variable like plus
plus X instead of X plus plus what happens is you can see whenever you run the program
it goes 21 21 so what this does before the variable is it adds one first and then it
runs the statement so adds one right here and then runs it so right here the value of
X is getting set to 21 and then run it 21 still 21 21 so it’s basically if your pluses
are before the X it’s going to add one before it runs it if your pluses are after the X
it’s going to run the line of code and then add one to the variable so it you know if
you’re building a program in you’re off by one you know maybe you’re running a loop or
something and you’re always off by one chances are your pluses are on the wrong side either
this side or that side so this is a common error and it’s actually something that people
have a hard concept understanding but just remember where you add your pluses indicate
when it’s going to add one before or after the statement and if you remember like that
it’ll be simple enough so now you understand that you don’t need a return zero you understand
all these scuse me ooh assignment operators that’s actually kind of a tongue-twister assignment
operators assignment operators just hyman operators got it and now you understand increment
operators which is plus plus X and X plus plus oh and one other thing there’s also minus
minus X and what this does of course is subtracts it and X minus minus but I don’t need to show
you guys that so just remember those four different increment operators all those assignment
operators and you don’t need a return zero anymore and you’ll be good to go so thank
you guys for watching my twenty first tutorial no idea what I’m going to cover in the next
tutorial but it’s going to be sweet so thank you guys again and I will see you guys later
what’s going on guys is Bucky and welcome to your 22nd C++ tutorial and in this tutorial
I’m going to be talking to you guys about a different kind of loop than the while loop
this is called the for loop now if you guys remember a while loop was basically something
that took a bit of code and ran it over and over and over again this isn’t only for a
while loop but all loops do the exact same thing they take a bit of code and they run
it over and over and over again the only difference is different loops you have to set them up
in different kinds of ways so anyways I already told you guys how to build a while loop simple
enough so now let’s go ahead and take a take a look at the cindex for a for loop and the
first thing you need is the word f o R now after this you add parentheses now inside
your parentheses your for loop requires three pieces of information a starting value and
ending value and how much do you want to increment it by for example if we started at 10 and
we ended at 20 and we wanted increment it by 2 it would be 10 12 14 16 18 20 so let’s
go ahead and build a simple loop that starts at 1 ends at 10 and it increments by 1 pretty
much 1 to 10 counting by ones so the first thing we need is to initialize a variable
so int x equals 1 now every piece of information you separate it with a semicolon now after
this it’s pretty much the same as the test in the while loop how long do you want to
let your loop run well let our loop run as long as X is less than 10 so it’s going to
go from 1 to 9 because remember when it gets 10 10 is not less than 10 so it’s going to
go 1 2 3 4 5 6 7 8 9 but how does it know to go one at a time instead of two at a time
or five at a time well that’s that last piece of information that this loop needs X plus
plus and if you remember from the last story this means add 1 to X every time um you can
also do X plus equals 1 for X plus plus is a lot easier it saves you 1 character and
you know only two different characters but anyways I’m rambling enough of me talking
so basically the syntax for for-loop is this initialize a variable with a starting value
give it an ending value and say how much you want to increment it by each time so now we
would just do something like this see I’ll print out X on the screen and print it out
on a new line every time so let’s go ahead and build and run this and see what we get
1 2 3 4 5 6 7 8 9 so we started at 1 and we ran the loop the whole time as long as X was
less than 10 so that’s why 10 isn’t print out because 10 is not less than 10 but all
these values are and since we wrote X plus plus it went by 1 each time 1 2 3 4 or 5 kept
adding 1 until we got to 9 so now let me go ahead and show you different uh you know what
just add some different values so you guys can clearly see instead of 1 let’s start at
5 by the way 0 & 1 are going to be your most basic starting points but you know you can
use anything you want but people typically stick with 0 or 1 let’s go all the way to
50 and let’s go ahead and increment it by 5 by writing X plus equals 5 which means add
5 to X each time and assign that to the new value so now if you go ahead and build and
run this we go ahead and you have 5 all the way to 45 by 5’s and we start at 5 right here
well all the way till we were less than 50 and that’s why it doesn’t print out 50 because
50 is not less than 50 is equal to 50 but 45 and all these values are less than 50 and
we told it to add 5 to X each time simple enough so basically starting point ending
point how much you wanted to change each time so now those are you know the basic Bucky
terms but if you’re taking a test I want to give you guys the technical terms this is
called initialization where you start off a variable this is called the loop continuation
condition basically when you’re on the end and this is called the increment so with those
three things it has all the information that your loop needs where to start where to end
how much to go by so this is a really basic example of the for loop all we did is print
out numbers and you didn’t really do anything useful except learn the for loop so in the
next tutorial I’m going to be showing you guys of an example of why the for loop you
know it’s basically this I’m going to show you guys the for loop in the cool program
and I’m going to show you guys how it’s going to be used to do stuff like calculate how
much money you can make in the stock market so if you don’t know what I’m talking about
yet just check out my next tutorial because it’s going to be awesome and we’re going to
be building a super cool program so thank you guys for watching don’t forget subscribe
and I’ll see you guys next video what’s going on guys is Bucky and welcome
to your twenty-third tutorial and in this tutorial I’m going to be building a simple
program and I’m going to be using the for loop and I’m just going to be using it to
show you guys that it is actually useful outside of just showing you guys examples so what
we’re going to be doing is we’re going to say we’re going to start with $10,000 and
we’re going to invest in the stock market and we’re smart enough to earn 1% each day
on our money so it’s end of 20 or 30 days how much money did we make so you know it’s
a little funner program than what we’ve been programming before it’s a little cooler a
little break from just basic examples so before I get started I want to mention this in our
formula for calculating interest rate we need to use a function called power and that’s
included in the see math library so make sure you include this before you get started in
this tutorial so let’s go ahead and get down to it now we’re going to be calculating interest
rates and stuff like that so we’re going to be needing to work with decimal points like
0.01 0.02 for 2% but we already know int so can we use integers when working with decimal
points like this no we can’t it is for whole numbers only like 98 for 54 it’s or whole
numbers without a decimal place so in order to use a decimal place we need to have a new
type of variable and that’s called float float is basically the datatype that allows you
to work with decimal places there are a couple different ones but this is one of them one
of the most popular ones so let’s go ahead and make a variable called a and this is going
to be the amount we’re trying to figure out how much money did we make the next variable
we need is called principle so floats P equals your principal amount in in everyday terms
how much money did you invest how much money did you start with well say we just had $10,000
laying around so let’s go ahead and start with that I wish I had $10,000 around but
hey we can dream can’t we so now let’s go ahead and say this our last variable is rate
so I’m going to set it equal to R and that’s how much interest do you want to earn each
day and let’s say we can earn 1% each day and that’s point o 1 if you had 2 percent
it would be 0.02 make sure not to put 2 or 1 for this you need point o 1 and or else
it’s not going to work so let’s go ahead and use the for loop to run through each day so
what this is going to be doing is looping through each day one by one one two three
four five six seven days and each day it’s going to give us a dollar amount how much
money did we earn that day at one percent so of course the for loop is going to loop
through days let’s go ahead and make a variable called day in today and we’ll start at day
one obviously and the next parameter needs is how long do you want to invest your money
for so let’s say on 20 days so day is less than or equal to 20 days and of course day
plus plus because one day at a time we’re not skipping any days and we’re not taking
any vacation time here so now the formula for interest rate exists the amount of money
that you earn is equal to the principal amount times and in parentheses put this actually
no not yet the principal amount times the power and this is where you put parentheses
1 plus the rate and this is would be let me go ahead and type this out I can’t talk and
tell you guys financial formulas at the same time the formula for calculating interest
rate is this the amount of money you’re going to make is equal to your principal investment
times 1 plus rate in the rate would be point zero 1 in this case 2 the day exponent so
it would be like umm one percent 1.0 one percent to the 20th power so I mean it’s a weird formula
but that’s how you calculate just trust me it works so anyways now what we have to do
is just print out on the screen how much money we’re going to make so let’s give it a nice
little print thing arm let’s go ahead and print the day first and then just want to
give it a nice little spacer here there we go and then we go ahead and write a a is the
amount of money that we’re making and then go ahead and we’ll print this on new line
each time so let’s go ahead and run it and you’ll see what I’m talking about whenever
I run it so here is the day to the left one through twenty we start off and at the end
of the first day if we invest $10,000 at one percent we earned a hundred dollars at the
end of that first day at the end of the ninth day we almost earned eleven thousand dollars
if we invested $10,000 and we earned one percent each day for twenty days at the end of your
twentieth day you would earn over twelve thousand dollars well you would actually earn two thousand
dollars but calculating the money already had in there you would have over twelve thousand
dollars that’s pretty sweet huh so now you know why everyone wants to get in stock trading
because basically by just sitting at your computer and clicking buttons making trades
you can earn two thousand dollars in twenty days pretty sweet huh so since you know this
is fake money and we’re just dreaming let’s say that we could we were a genius like Warren
Buffett or Bill Gates and we were smart enough to earn 3% each day now let’s say we invested
this for 30 days so go ahead and let me run this whole mama check this out if you invest
in money for 30 days and you earn just three percent on your money each day at the end
of 30 days or one month you would over double your money you would have twenty four thousand
dollars and more and enough to go out to Taco Bell so that is pretty sweet and this makes
me second-guess my career as a computer programmer and kind of makes me want to get in the stock
market but by the way before you guys get into the stock market earning 3% a year would
be good nowadays earning 3% a day is virtually impossible unless you’re like Warren Buffett
so anyways there is your tutorial on a useful way to use the for loop in program and also
you know some financial advice – the formula for APR so there you go and I hope you guys
learned at least something from this thorough and hopefully I didn’t get you addicted to
the stock market because hey we got more c-plus supposed to learn guys so thank you guys for
watching don’t forget to subscribe and yeah I’ll see you guys later
what is going on guys it’s Bucky and welcome to your 24th tutorial in C++ and in this story
we’re going to be covering the do-while loop but you guys probably already knew that because
I got a feeling that the title is right above this video and you know what else I do a lot
I tell you guys this is probably going to be a short video and I just realized that
the time is right under the video so I don’t know why I say those things but it’s habit
I’m going to continue to say those things so get over it so anyways we’re going to be
covering the do-while loop in this tutorial and if you remember from I don’t know however
many tutorials those go I talked to you guys about the while loop and the while loop was
basically a loop that you had a test and if that test was true then you ran a bit of code
inside while the do-while loop is basically the while loop flipped upside down what the
do while loop does is it runs the bit of code first and then it runs the test so let’s go
ahead and let me show it to you guys and you guys will understand better one means let’s
go ahead and get a counting variable called X and set it equal to one this is our starting
point now the syntax for do-while loop is do and then you go ahead and add some code
in the body so let’s just go ahead and print out X and then after this we need to increment
X or else the loop is going to run forever so X plus plus and now after the body immediately
after it you write while and then after this you have your test ended with a semicolon
a lot of people forget that semicolon because then basic while loop there is no semicolon
if it’s not in the body so don’t forget to put this in my Coleman right here so let’s
give a simple test like X is less than 10 now go ahead and run this and check it out
just like the wild loop it ran the same bit of code print it out X on the screen from
1 all the way to 9 pretty cool huh so we’re saying all right a couple of things are kind
of weird here first of all why is this loop even useful because it seems like a bad while
loop because isn’t the point of a loop to run the test first and if the test is true
then run a bit of code but if it’s false then don’t run the bit of code well people use
do loop mainly for this reason whenever you have code in here and you need it to run at
least one time it’s guaranteed to run at least at one time and the reason is this say you
have a test that is false like 99 well this test is going to come false as soon as it
hits it 99 is not less than 10 99 is a whole lot bigger than 10 so whenever you run this
is just going to print out 99 so as you can see it ensures that you run your code at least
one time before testing it at all so no matter what this value is it’s going to do it one
time and then test it so whenever you want to run your code guarantee that it runs at
least one time this is what you do so that’s the main reason that people use a do-while
loop to ensure their code runs at least one time again if you have a basic program and
you just want to loop it normally your best bet is to run the while loop or the for loop
because those are your basic loops where you test it and then depending on the test you
run your program so again this is basically a weird a kind of loop this is probably the
most uncock Loup but whenever you come across it I wanted you guys to pick up on it understand
what it’s doing and why some people prefer it over the wall loop so now that you have
knowledge of the do-while loop and you guys know that it’s really not that common in programming
so you don’t want to use it that often you at least know what it does and how I was going
to say how it worked well yeah that makes sense and how it works so anyways that’s all
I have to cover for the do-while loop pretty simple basically a while loop flipped upside
down so thank you guys for watching don’t forget to subscribe and yep I’ll see you in
the next video what is going on guys welcome to your 25th
C++ tutorial and in this story I’m going to be talking to you guys about a really cool
feature of C++ not only C++ this is basically in all computer programming languages but
it’s called the switch statement in the switch statement in your computer programming career
is going to save you a whole lot of time and headaches so let me give you guys an example
of when you would use this say you have a variable like age and you want to test the
age of the user and you had a bunch of different um like statements you want to print out depending
on how old they were so if they were 16 you would say congratulations here’s a new car
since you can drive now if they are 18 you would say go buy a lottery ticket if they
are 21 you would say go buy some beer so you have all these different values that you could
apply this same variable so with our knowledge so far we can build a program like this doing
this if age equals 16 let’s go ahead and type her body and then after this if age equals
this yadda yadda if age equals that and then that well all those if statements are getting
pretty old so wouldn’t it be cool if we could combine this all in kind of a compact statement
well it would be cool well unfortunately we can so on with the next tutorial in the next
store I’m just kidding we actually can do this and that is through the use of something
called a switch statement now in order to use a switch statement here is what you do
type the word switch now in parentheses right after that write the variable that you want
to test well we want to test the value of age so let go ahead and type age right in
there now after this go ahead and add the body to your switch statement now inside your
body you’re going to give it all the values which you think age is equal to and we’re
going to be giving it in the form of cases so pretty much we’re saying this in case they
enter 16 but don’t write in case it’s only case so in the case of 16 and then write a
colon now after this colon you write whatever statements you want your computer program
to perform or computer program to perform you’re the computer programmer this is computer
programming there I got to straighten my brain so encase the value of age equals 16 let’s
just go ahead and put something stupid out on the screen like see out hey you can drive
now and go ahead and end line why not and after this add a keyword that says break now
I’m going to be talking about break later on because it’s easier if I make the whole
program first before I talk about it now after this we can write something like alright what
if the case is 18 then what are we going to print out then um see out on the screen arm
go buy some lotto tickets because you can do that at 18 now in line and of course after
this at that break statement that we don’t know what it does yet but we will and let’s
go ahead and give it one more case why not in the case of 21 the best one um let’s go
ahead and see out buy me some beer and go ahead and end that line so now you can see
through the use of this switch statement it basically saved us the trouble and the time
of making a bunch of different if statements so instead of three different assignments
foot foot foot three different if statements we only had one switch and we just tested
a bunch of different cases so the last thing is default and what default means is if none
of these cases were true if they entered a value like two or something like that it ran
through all these cases was equal to 16 nope 18 nope 21 nope so what do you want me to
perform by default and of course you don’t need like default 2 because it doesn’t have
a value it’s just if all of these values failed then what want me to do by default by default
we’ll just write C out armed sorry you get nothing because 16 18 and 21 that’s real bit
where the important stuff happens everything else is just the birthday so let’s go ahead
and end that line and one thing you might notice um whenever we build and run this you’re
saying our rights sorry you get nothing because you’re two but before you go ahead and go
and run this why did I add a break at the end of default and what exactly is break well
this break keyword right here pretty much means end the switch so we’re saying all right
why would I want to end it why does it just keep you know going through the rest of stuff
well the reason you had break after each case is because of this say you had 16 and you
wanted to test it we know that whenever it ran it printed out hey you can drive now and
that’s because we tested age and age is equal to 16 so in our first case it’s say hey this
is a match let’s go ahead and run this bit of code right here so since we got a match
in our first case there’s no need to test this one this one and we’re know we’re not
going to run the default because this is the only one we were on a run right here so what
break says is break the switch it pretty much saves your program the trouble of having to
run through the rest of these cases because it’s not going to be useful to run through
the rest of them as soon as you found a match so let’s go ahead and say we had 18 in there
what we would do is it runs it and it prints out go buy some lotto tickets since we have
that break in there it did this all right in the case of 16 nope you’re 18 in the case
of 18 bingo we have a match so let’s go ahead and print out go buy some lotto tickets and
break out of this loop so no need to test 21 and no need to use the default since we
broke out of it we found our match so again what break does is it basically saves you
from going through the rest of the loop and why do I not have one at the end the default
because by the time we get to the end of default it’s at the end of the switch anyway so why
would you need to break out of it if you’re just going to exit out of it after the default
anyways so that is basically a switch statement it basically takes a variable and test the
value against each one of its cases 16 18 21 if it isn’t any of those values it runs
the default now I have three cases in this example but you can have one you can have
a thousand you can have a million if you want and yeah that’s all so anyways that is how
a switch works and that is what break is and I just want to mention one more thing before
I let you guys go break I’m using it in this example of a switch but it’s a keyword that
we’re going to be coming across a lot in C++ programming not only with switch so whenever
you see it later on whenever we’re doing you know just regular loops or something I just
want to let you guys know it isn’t only limited to the switch statement we can actually use
this all throughout C++ so whenever you come across it they’ll be like hey isn’t it supposed
to go around that because we’re not so for now thank you guys for watching understand
the switch and once you do you’re ready to move on to my next video so like I said thank
you guys for watching don’t forget subscribe and I’ll see you guys in the next video
what’s going on guys is Bucky and welcome to your twenty six tutorial and C++ in this
tutorial I’m going to be talking to you guys about something called logical operators now
let me go ahead and build a simple program right now say we were the owner of a nightclub
and we wanted to calculate whether a person was allowed in or whether they weren’t so
first we looked at their age and we’re going to make sure it’s you know over 21 because
we don’t want to get sued and the next thing we’re going to be looking at is how much money
they have because we don’t want to allow poor people into our you know 5-star nightclub
so let’s say they need more than $500 so let’s go ahead and make this like person has six
hundred and fifty dollars so we can test this like this if H is greater than twenty one
and if money is greater than what I say five hundred armed where would I just hit insert
where the hex might insert key on this keyboard this keyboard is really my stuff now that
look about it so it took me like five minutes to find the insert it was actually by my up
and down arrows that’s weird so anyways now that we had our nice little break we can go
ahead and just write something like C out um you are allowed in and make sure you sell
it with a nine and you and make sure you spell in ion or else this program just won’t work
so let’s go ahead and run this and see what we get we are allowed in because we indeed
are greater than 21 and we have more than $500 so resent are a pretty cool program maybe
I’ll open up a nightclub now but check this out this can get kind of confusing after a
while because this brace goes to this and this goes to this we’re pretty easily distinguishable
but what if we had five different tests that we need to check what if we had 20 different
tests well one little mistake say we had this over here or program still going to run fine
but it looks like this goes to this and this goes the main or something who knows where
this one goes I mean putting a bunch of if statements inside other if statements is not
always the best idea so we have a better way to test two or more things and that’s the
reviews of something called logical operators and this allows you to make all your tests
on a single line so here’s what we can do in this case go ahead and make your if statement
and then might as well make our C out right now like you are allowed in explanation point
with endline and in our if statement go ahead and write your to test like what do you put
age is greater than 21 and whenever you join them you use this logical operator and and
and now go ahead and write money is greater than 500 now let’s go ahead and run this and
check it out same results a whole lot less programming so what and and means do this
test and do this test if they’re both true then run this bit of code and since they both
were true it ran this bit of code if one of them must fall say we only had you know four
dollars like that well yeah this test is going to be true because we’re great entering turn
one but this is going to be false because four dollars is not greater than 500 and whenever
you run it we are not allowed and check it out and also if both the tests are false like
we’re only two and oops right or wrong space for that say we’re only two years old and
we only have four dollars probably from tooth fairy of course we are not going to be allowed
in the club not cool but you know we’re two year old who needs to go clubbing when they’re
two anyways so basically a double + sign means this test all these tests and if they’re both
true then you can run this bit of code but if one of them is false or both of them is
false then we don’t run it simple enough on and I say and with this test in this test
but you can also have an and in another test and even in an in another test and it’s just
going to run all of your tests and make sure that they’re all true so the only way this
bit of code is going to run is if all your tests and this is true so if we send alright
I understand that logical operator all the tests need to be true in order to run but
what if you say this all right I’m on you know not the most professional nightclub owner
so I’m going to allow them in if only one of these tests is true if they’re over 21
and they’re loud and of course or if they have over 500 dollars I’m gonna lie to allow
him because you know if 20 year old walks in with like 8 grand I know he’s not 21 but
I think we should let him in anyways so say we want to do that make sure only one of the
tests is true what we do is use a logical operator called the or logical operator and
that’s if you see above the enter key on your keyboard and hold shift you get these two
little things I call them pipes but I think some people call them pipe lines tell you
truth I don’t know what they’re called um it’s like a straight line up and down as you
can see but that’s what little that’s what it is and two of them in a row is the or logical
operator and what the or logical operator says is this unlike the end where this one
and this one had to be true only one of them has to be true and the other one can be false
and it still runs the bit of code so check this out we pass this test is true 26 is greater
than 21 but we don’t have more than $500 so this is going to be false so true and false
so we run it and it allows us in even though one of these was false but if both of these
were false say we’re on the six years old it wouldn’t allow sin so anyways what you
have to remember is this that and logical operator both the tests have to be true with
the or logical operator one of the tests have to be true and another thing if both of the
tests are true and/or it’s going to run your bit of code so let’s say you were you know
64 and you had you know a crapload of money it would run and even though they’re both
true it would allow you in anyway so with or this one or this one or both of them have
to be true and with and this one and this one has to be true and of course you can add
another or test and another or test and another or test and when you ran a program like this
it’s going to run through all of your tests and as long as one of them is true for example
this one could be true and all these other ones could be false and it was still around
this bit of code an order basically means look for at least one that’s true and if it’s
true run the bit of code so now that I’ve repeated that about 800 times you guys should
understand the difference between an and and or logical operator and when you would want
to use one versus the other I guess it depends on what kind of nightclub owner you want to
be but for now that’s all the code on I’m going to do so thank you guys for watching
this tutorial don’t know what I’m going to cover in next tutorial but if you click on
the next link you can see so thank you guys for watching and I’ll see you then
what is going on guys it’s Bucky and welcome back to your 27th C++ tutorial and this tutorial
is actually going to be a pretty fun one because we’re going to be talking about random numbers
what they are you know a couple functions that are going to help us build a random number
generators and I’m actually going to teach you guys how to build a sweet random number
generator so before we get into that we need to include a header file so include and the
header file is called C STD Li B from the C standard library and we include this because
we want to use a function from it called R and not run Rand and what this does is whenever
you call this it basically returns to you a random number so let’s see let’s run it
and we got 41 in this case so as you can see whenever we print out R and it pretty much
takes a random number and gives it to you sorry saying Wow there you go there’s our
random number generator perfect well they’re actually a couple problems with the rand and
we’re going to be talking about those later on but for now I want to show you guys how
to build a basic random number generator and let’s go ahead and just call Rand like 25
times so let’s go ahead and make a for loop and go ahead and make an x set equal to 1
and make X is less than 25 and of course X plus plus so now we have a loop that’s going
to run 25 times or sometime around that so now let’s just go ahead and see out umm well
let’s just go ahead and see out Rand and see what happens and then might as well end that
line so it looks so nice and pretty let’s go ahead and print it and see what we get
okay so it looks like we’re getting a huge list of random numbers all right looks pretty
good but you know what if I’m going to build a random number generator and I don’t want
numbers quite this big in tens and thousands and stuff like that say we’re building a random
number generator to simulate people rolling dice or something well I only want the numbers
1 to 6 so here’s a neat little trick where you can get that result set in order to get
a certain set of numbers go ahead and take your R and and put modulus 6 what this is
going to do is it’s going to give you six random numbers it’s going to take a big number
like 25,000 or something divided by 6 and give you the remainder of that so the remainder
whenever you divide by 6 can either be 0 1 2 3 4 or 5 so let’s go ahead and take a look
at that whenever you divide by 6 you’re limited to only the number 0 1 2 3 4 & 5 you can’t
have 6 as a remainder because anything divided by 6 with 6 remainder it would just be another
one so you can say all right well I’m trying to simulate a dice here and they have the
numbers 1 through 6 not 0 through 5 so we got 6 numbers but we don’t want 0 through
5 we want 1 through 6 so Bucky can you just show me how to take all these numbers and
shift them add 1 to them well that is very simple just go ahead and surround your R +
6 and shift put 1 plus now this is going to do is it’s going to take a random number divided
by 6 and whatever the remainder is which would be 0 through 5 it would add 1 to it so now
you’re giving 1 to 6 so now you can see that 0 is eliminated and we have 6 now so 1 2 oh
let me find it three three four five and six so that is how you build a random number generator
using you know a specific result set so remember however many numbers you want you put as a
modulus of R and and however many you want to shift to by just add that many to it but
let’s go ahead and take a little closer look at this random number generator okay let’s
read these numbers five six five one one five okay they look like you know they’re pretty
random to me let’s go ahead and run it again five six five one one five that seems kind
of familiar let’s go ahead and run this one more time five six five one one five all right
what’s going on with our random generator we built this program perfectly which we did
so I mean we use a random RA everything is good to go so what’s the deal why did it give
us the same numbers every time well in order to understand this I need to talk to you guys
about computers and random numbers now listen very closely because this is actually very
important no computer random number generator is truly random the truth is that computers
are not humans computers have to follow a certain algorithm a certain set of instructions
a certain pattern so for example whenever you go into a casino and you see those automated
slot machines and it looks like people are just spinning and skipping them random numbers
well the truth is that it’s not actually given them truly random numbers those slot machines
even though they’re supposed to be random number generator there have to follow a certain
aggregate rhythm a certain pattern and you know whenever you’re looking at like game
shows or whenever you buy electronic and a flash like random lights well those lights
are not random either everything in electronics that is supposed to be random is not random
it’s a computer it has to follow a complex algorithm in a complex pattern now the reason
that these are so complex is that’s what gives the appearance that it’s random to humans
we don’t understand this algorithm right away only the computer does so whenever we see
numbers like this five six five one one five three six those are random numbers to us but
the computer it’s really just following a big algorithm so we’re saying all right so
whenever we’re building games and stuff we don’t want our numbers to be predictable so
what can we do in order to change that algorithm in a little bit well let’s go ahead and let
me include another no I wanted this yet but the rand function right here runs on a very
complex algorithm and I don’t know what the algorithm is and I don’t think many people
do but it’s basically a large function that computers excuse me that humans aren’t meant
to understand so what C++ the developers allowed us to do is seed a random number and you use
that in a function called s Rand now what this function does is it allows us to pass
in a random number any number we want like 67 and now whenever we build it we get different
results six two one six one six now let’s go ahead and pass another one like 215 and
now we get you know four one three one one or whatever so whenever we see it around a
number we throw you know any old random number in there and it changes the algorithm so now
I don’t even know you know what happens when I type in 43 I get truly random numbers so
if you’re saying all right so let’s go ahead and put 43 in there every time and we get
four four one five one okay now let’s run again four four one five one OMG Bucky what
are you doing all you did is you change the algorithm a little bit and you know we didn’t
really make it a truly random number January we just gave a different algorithm and follow
well all right I’ll show you guys one last trick in in order to do this we need to include
another header fire and and this is called see time and what this does is allows us to
access the clock or the time in our computer and now let’s go ahead and pass in a function
called time and as a parameter of time not tema time go ahead and pass in zero now let’s
go ahead and run this and see what happens before I explain it okay two five two two
okay let’s run again four six one for sweet and one more time five four six four six so
now we have a random number generator that works perfectly so now let me tell you guys
why we know that we can use s R and to change the algorithm a little bit but if we just
threw the same number in here every time oh just you know have a different algorithm but
with the same results every time so what time zero does is time zero calculates the seconds
sense like 1970 or something and what this does is every second this value is changing
so now it might be a million seconds now a million and one now a million two people don’t
have access to how many seconds are you know counting since 1970 so this gives the appearance
that this number generator is truly random now so this value is ever changing and that’s
why every single time we run our program we’re going to get a different result so that is
how people build random number generators that are is truly random or they’re not truly
random but there is random as we can make them so again what we did is here’s the algorithm
all ran does is it runs a complex algorithm s R and allows us to throw a number in there
to change the algorithm a little bit and time zero counts the number of seconds and this
value is changing every single second so that’s why since this value is changing this whole
algorithm is changing every single second and that allows us to make a truly random
number generator so there you go I hope you guys enjoyed this tutorial thank you guys
for watching and don’t forget to include your header files because you can’t use this program
without it and there you go I’ll see you guys next time
what is going on guys welcome to your 28th C++ tutorial and in this Israel as you can
see I’m going to talk to you guys about something called default arguments so of course we’re
going to be building a function that takes a couple arguments and a default argument
is basically exactly what it sounds like you know how we can have a function to like calculate
the area of something and then the user would need to enter arguments like length and another
value for width well they can either enter the values explicitly or we can have a default
value already in that function so let me go ahead and we’ll build a really basic function
here just to demonstrate this point so let’s go ahead and calculate the volume of a just
something a box or something and so we have three arguments or three parameters int length
and width and int height so we can go ahead and make this function right now and well
let’s actually go ahead and make this function right now go ahead and copy this and under
main make sure you’re not in main when you make this go ahead and paste it and calculate
the volume in case you don’t know it’s just length times width times height so now we
can go ahead and call this function C out volume and the user can enter three values
like four five and five so this should go ahead and print out 100 because four times
five is 20 20 times five is 100 so the volume of this box is 100 so alright we have you
know just a regular function here with three values or three parameters that it takes and
the user goes ahead and passes in those values and it calculates it perfectly but in order
to add default arguments or default parameters here’s what you do in your function prototype
right here instead of just having you know the variable type and a variable you can go
ahead and set these equal to something automatically like one what you set them equal to one right
now so if a box is one by one by one we can go ahead and assume that well it’s basically
this if the user doesn’t enter any values explicitly then it’s going to use those default
values of 1 1 and 1 so whenever we call this function without any values check it out it
gives us the value of 1 because a box 1 by 1 by 1 has volume of 1 now let’s go ahead
and say that the user enters a number like 5 okay so the user enters a number like 5
and it gives us the value of 5 but you’re saying all right so does that function use
that 5 for length width or height well when a user enters one value and you have a function
that has three default values the computer is automatically going to assume that you
meant 5 equals the first parameter or 5 for length so if you go ahead and run this again
we get 5 that’s because it’s 5 by 1 by 1 so if you go ahead and enter something like 5
& 6 the user is automatically going to excuse me the computer is going to assume that you
met 5 for length 6 for width and by default height was one so the only thing you’re really
overwriting is length in width so it’s basically the rules this you can have a function that
has default values and by default it’s going to use those values unless the user overrides
it with values of its own whenever the user does override it with values of its own it’s
going to pass them in in order of which it would receive them so if it has send one number
it’s going to replace default value for that number if you pass it in to number it’s going
to replace the second value for that number right there and if you just pass it in two
numbers is going to of course keep that same default value of one for the last one so you
get 30 for your value so of course I just want to point out that you can you know go
ahead and add three values if you want but you know what’s the point of having default
values if you’re just going to have three parameters you’re going to pass in anyways
so basically that is how you have a function that uses default arguments in other words
default parameters and one other thing that people typically mess up you only have to
put the default value in your function prototype right here whenever you create the function
in the function body itself down here you don’t put the values down here because you
already sent all right if I don’t explicitly pass in any values I want to use these as
defaults so you don’t need to re-type it right here so people get this wrong a lot so don’t
forget to you know just remember that simple rule so now you understand the basics of having
default values in your function why they are useful in how you can pass in a different
number of functions like six or six and five or six and five and seven and it will all
work fine basically whatever you pass in it replaces the rest with the default values
so that’s all I have for you guys for this tutorial hopefully you understand if you don’t
just go ahead and ask me one of the questions on my forum the new busting comm slash forum
and I will be more than happy to answer it for you guys so thank you guys for watching
don’t forget subscribe and I’ll see you guys in the next tutorial
what is going on guys it’s Bucky and welcome to your 29th C++ tutorial and in this tutorial
I’m going to be talking to you guys about something called scope I can’t remember if
I talked to you guys about scope already but I need to talk to you guys about it again
anyways so anyways I’m gonna be talking to you guys about scope not the mouthwash a little
bit different and also something called the unary scope resolution operator but I call
it the colon colon because you know whenever we’re typing it it’s just a colon colon so
let’s go ahead and first let me build another function aside from main I’m just going to
build another function called Bucky and all this is going to do is well we won’t even
have it do anything yet so right now we have two functions main and Bucky they’re both
good to go so sometimes people build variables inside a function like in X and set it equal
to 10 well let’s go ahead and make some interesting variables I’m tired of X so int tuna and set
it equal to 69 now let’s go ahead and build another variable called tuna and set it equal
to 20 well I just want to demonstrate one thing some people would declare variables
outside functions and some people declare variables inside functions now it’s not just
like some people have blue eyes some people have brand eyes there are people nonetheless
and it doesn’t really matter where I color they have well actually this does matter it’s
not a personal preference it actually does matter in your computer program where you
declare or make your variable whenever you make your variable outside the functions then
every single function can access this tuned variable so this function can use it Bucky
can use it every function has access to this variable but for example say you only made
your variable inside one function so you only made this variable inside main that means
that whenever Bucky tried to use the variable tuna it’s going to get an error message so
check it out it says tuna what the heck is tuna because main functions are basically
greedy whenever they create a variable they’re the only ones with access to that variable
so that’s why people typically like to use variables in declare outside all of the functions
because whenever you make a variable outside the function then every single function in
your program is going to have access to that so say for example main wanted for an Altoona
and book you wanted to print out to know well you can go ahead and run this program right
now and check it out both programs are going to print out tuna just fine so the reason
Bucky didn’t print it out and you only saw tuna once was because we didn’t call it anywhere
but anyways that’s what the scope of variables is basically where you create variables depends
where you can use them so if you declare a variable in here only main can use it if you
make one in here only buck you can use it if you declare outside both the functions
can use it but now we come across a little bit of a conundrum and let’s go ahead and
delete Bucky for the rest of the store oh because he was just an example a dang fine
one but a he was an example what if a user did something stupid like this they made a
variable called tuna outside main so of course main has access to it but they made another
variable called tuna that’s inside main as well so main can access that one too so now
when you try to print out tuna as a choice do I use this one or do I use this one let’s
go ahead and print this out and see what happens it chose 20 and why did it do that well computers
pretty much take the most recent or most inner variable that they can they say all right
here is a variable that I can use but why use that one when I have one right inside
my function I’m going to assume that since this one was built right inside my function
that that’s the one you want me to use so these are called local variables and these
are called global variables and when it has the choice it’s always going to choose that
local variable so you’re saying all right let’s go ahead and say that I’m building a
function just like this and I want to call both of these variables tuna but I actually
want to use this variable tuna out here well how do I tell my program that all right I
know you have a local variable called tuna but I want you to use this one well in order
to do you need to use something called the unary scope resolution operator in other words
the : : : : tells your program all right you have two functions called tuna one of them
is local and one of them is global whenever you have this : colon before tuna that means
use the global tuna in other words use the tuna outside your function so now whenever
you run the program check it out it uses that global tuna instead of the local tuna and
the reason people actually do this is because when they have variables with the same name
of different data types that’s when it comes in handy so for example if you had an INT
tuna and the double tuna and you ran it it would use the double tuna which is 20 but
if you use that colon colon which is called again the unary scope resolution operator
if you’re taking the test or something then it uses a 69 so I just don’t want to show
you guys if you’re stupid and put in twice people typically write variables with the
same name whenever they’re using different data types so just wanted to point that out
so what to take away from this tutorial whenever you create a variable inside a function you
can only use that variable inside the function where you created it you can’t use it inside
any other function whenever you create a variable outside of function you can use it in any
function in your entire program so if we had another function called Buckey and another
one called Toni and another one called Jessica we can use this tuna in all of those functions
so that’s why you can decide whether it makes this outside or inside your function and the
last thing I want you guys remember from this story whenever you have two variables with
the same name one is local and one is global and you want to use the global variable in
your function you need to use this dot dot or colon colon unary resolution operator whatever
the heck you want to call it it’s two colons before your variable and that tells C++ hey
C++ I know I have variable in here but I want to use this one the global variable instead
and if you remember these three rules you’ll be good to go so that’s all I have for you
guys for this tutorial again basically a really easy tutorial just a little weird concept
that you have to understand so thank you guys for watching don’t forget to subscribe and
I will see you guys in the next video what is going on guys welcome to your xxx
C++ tutorial 30 tutorials that’s like a milestone or something and in this tutorial I’m going
to be talking to you guys about function overloading what it is why it’s useful and when you would
use it so let’s go ahead and say we wanted to build a function – I don’t know maybe we
wanted to have the user and put a number and we just wanted to print it out on the screen
really stupid function but really simple so let’s go ahead and make two variables we’ll
make an end of your variable a and we’ll set this equal to you know 54 and we’ll make another
flow variable and remember a flow is basically a number with decimal point so we’ll use like
32.4 yadda yadda something something stupid so now we have two different types of numbers
an integer and a float well we have a function that we are gonna build called print number
so let’s go ahead and call this function and again we didn’t build this yet so for your
looking where did print number come from we’ll build it in a second so we’re going to go
ahead and try to use this function for both of these variables print number a and we’re
going to try and print out B 2 so then we say alright now we’re going to go ahead and
build this function to print out a number so we go up here it’s not going to return
anything so we’re just going to be void of course it’s named print number and then we
get to this part right here alright let’s go ahead and think for a second if we go ahead
and put int X then we’re going to allow to pass in an integer so this is going to work
but this isn’t going to work so I know what I’ll do I’ll just put flow X well then this
is going to allow me to pass in this number but then this number isn’t going to work so
how do we solve this little puzzle right here well we do this through the use of something
called function overloading and function overloading is the idea that in C++ you can build more
than one function with the same name in this case we’re going to build it be building two
functions called print number that take different types of data depending on what you pass in
for example we’re going to build a print number right here that takes in your value and just
go ahead and see out I am printing and integer and then go ahead and print out the integer
X and then in that line well that goes ahead and takes care of the integers that the user
might enter and now we can build another function with the same name which is print number again
if you double click it you’ll see the exact same name the only difference is it takes
a different type of parameter this one takes a float number instead so now whenever we
call this method and it has an integer it’s going to use this one and whenever we call
this method and by method I mean function I saw sorry I could switch it on and off but
I teach too many programming languages to keep it straight so whenever it calls a function
with a float it’s going to use this one it’s smart enough to automatically know what type
of data you’re passing it in so this one is going to print out C out now I am printing
a float which is X and n line so as you can see these functions are basically identical
it just runs whatever pretty much whatever type of Daddy pass in it looks for whatever
function is appropriate for that type of data and it uses that function so let’s go ahead
and build and run this and check out what we get at first we pass then an integer 54
so that’s why I used this one that took an integer and it said I am printing an integer
and after that we pass then in the same function a float which was 32 point four so that’s
why it chose this one now I am printing a float 32 point 4 so as you can see function
overloading solves the problem of having one function so check this out instead of trying
to explain our boss that they need to use a bunch of different functions depending on
what type of data we can make them happy and say alright anytime you want to use a function
just go ahead and use this print number function and put in any number you want this is going
to be behind the scenes stuff that they don’t have to worry about they don’t know that we
had to make two different functions just because they were annoying and they couldn’t understand
that they needed to work with different types of data so we’re going to make it really easy
for the user through the use of function overloading now whenever the user enters a number like
54 we already have a customized function that is smart enough to realize that they are entering
an integer and prints out that value but if someone another user like user B enters a
float then we of course can handle that too because our program knows it’s smart enough
to realize that we’re passing it in a float so that’s why it chooses this so basically
whenever you use a function your computer program is going to do this it’s going to
look for a function with that name if print number if it has more than one choice then
it looks what type of data are you passing it in well if I pass it in a float it’s going
to look at this one the zips print number take a float nope it takes an integer does
this print number take a float yes it does so I’m going to use this one right here so
that’s why whenever you create a function you need to write the name and also the type
of data that you’re passing in so then whenever the user tries to use it it knows what function
to use so that is basically all function overloading is it’s basically making programs that are
really simple for the user even though they might take us a little bit of extra coding
hey when we want to build software and we want to sell for million dollars we’re going
to want to make sure that it’s easiest for the user to use as possible because I mean
when we become a millionaire this extra function is not going to be a real big problem for
us so that is how you make million dollars and also that is the basics of function overloading
basically many functions with the same name just different data types so thank you guys
for watching don’t forget to subscribe and I don’t know what I’m going to be teaching
in the next tutorial but I got a feeling you guys definitely want to check it out so again
thank you don’t forget sub in I’ll see you next video
what’s going on guys is Bucky and welcome to your 31st tutorial and in this story I’m
going to be talking about something that every single computer science student on earth just
absolutely hates and that is called recursion now this is a topic that’s somewhat confusing
but I’m going to try to make it really simple for it you guys so what recursion is is the
idea that a function can call itself pretty weird huh so you know how like in main we
could call another function called Bucky and Bucky would print something out on the screen
well recursion is the idea that Bucky could call Bucky that’s interesting so let’s go
ahead and take a look at this right now say we had a function called Bucky and all it
did was print something out on the screen like see out um print out something stupid
oMG WTF vbq oh my god WTF BBQ and then it went ahead and it called the function again
so now you would say alright well isn’t this function gonna go ahead and we’re going to
run main first and main is going to say okay go ahead and run Bucky and Bucky is going
to say okay print this out and then run Bucky okay Bucky print this out and then run Bucky
again print this out run Bucky print it out run Bucky and whenever we go ahead and run
it we can see that this is a function that basically never ends it prints out oh my god
WTF BBQ until check it out watermelon exe my computer just crashed and gave up on this
so I’m like alright so what’s the point of using recursive functions other than don’t
use them because you know first of all if you build a recursive function like this your
computer’s going to crash just like – and it’s not the most useful way to go about things
so one reason that people use recursive functions is first of all let me mention this whenever
you build a truly perfect recursive function you need to have something called a base case
now a base case is pretty much an ending point for your function so one example that I’m
going to shown you guys is how calculate factorials now if you remember from math class factorials
are pretty much when you say five factorial and it’s written like this five explanation
point this is pretty much the same same as saying 5 times 4 times 3 times 2 times 1 um
7 factorial would be 7 times 6 times 5 times 4 times 3 times 2 times 1 whatever factorial
it is is just whatever number times all the way to 1 so then the value of like 5 factorial
is 120 the value of 3 factorial is 3 times 2 times 1 which is 6 so let’s go ahead and
write a program to do that and this is one of the instances where recursion is actually
a good idea so let’s go ahead and make a function called we’ll call it factorial final so int
first of all third spell that right int arm factorial finder I think I spelled that right
and it’s going to take a parameter which is in X can I not spell int today int X and the
parameter it takes is what value do we want to find the factorial of so if we passed it
in 5 hopefully it would give us 120 if we pass the N 3 it would give us 6 so on and
so forth so the first thing we need build is something called a base case without a
base case this factorial finder would never end just like Bucky you know how I ran forever
until my computer crash this is what this going to do if we don’t give a base case so
we need to say okay if X is indeed equal to 1 then we want to go ahead and return 1 so
whenever you return something remember your function ends and that’s the point that we’re
trying to get to so in order to do this we’re going to assume that they’re going to be passing
in numbers other than 1 so we’re going to go ahead and do this else if they pass in
the number aside from 1 like 5 or 3 or 20 or something like that go ahead and return
the value of x which is you know 5 320 and then go ahead and multiply that by factorial
finder X minus 1 so what it’s going to do is yeah it’s going to call the same function
again but it’s going to call it with a different value it’s going to call it with 1 less than
before so if you go ahead and you pass in 5 to this function what it’s going to do is
say all right I’m going to call 5 times the 4 factorial or factorial 4 I don’t even know
how to say it I’m not in mathematician but anyways what it’s going to do is going to
say this I’m going to call 5 factorial 4 well what’s the value of that I don’t know the
value of that is 4 factorial 3 what’s the value of that I don’t know 3 factorial 2 what’s
the value of that I don’t know 2 factorial 1 what’s the value of that I don’t know oh
wait I do know 1 factorial is 1 so now that it has that bit of information it can go ahead
and solve the rest of the questions that it asks you know how I said I don’t know I don’t
know well now it can say all right Bucky 1 factorial is 1 so now I know that 2 factorial
is 2 3 factorial 6 4 factorial is 24 and 5 factorial is 120 so that way you can see that
whenever we go ahead and run this on factorial finer there we go I didn’t feel like printing
out the whole thing that way when you print out factorial 505 and let me just go ahead
and end that line and we run this program it says 120 yeah it was 120 but there is a
lot of stuff going on behind the scenes that the user didn’t have to see what it did is
said all right it ran this function like 5 different times searching for 5 factorial
and then it caught it again searching for 4 factorial then did again searching for 3
2 and finally when it got to 1 it could answer all the rest of those questions so this is
how you can use recursive functions in a useful way again our recursive function is just using
the function inside the function body itself whenever you do this you need to have a base
case you need to have an ending point that actually answers that question so in this
case whenever they got to one it could solve the rest of it could of its questions because
if we didn’t have that base case like before when we printed out oMG WTF BBQ it would just
run forever and ever and ever without an end so that’s why you need to give it a base case
in other words a point to get to where if it solves that question then all the rest
of the questions can be solved so again you guys can probably see why people hate recursive
functions but why sometimes you actually need it because there’s a lot of stuff going on
behind the scenes that we don’t get look at but we just have to you know assume in theory
that this is going to work so it’s a little bit different than regular programming in
the fact that you don’t explicitly call a function like this that a function is going
to call a function for you however many times it needs to so there’s a lot of theoretical
programming that you need to think of and a lot of different things that can go wrong
with recursive functions so that’s why I wanted to you know show you guys a quick example
of the bad way to use recursive functions in the useful way to use recursive functions
but now that I tell you guys at soryo we can finally you know get over cursor functions
I you will be happy to know that I don’t use recursive functions a lot in programming and
we’re probably going to be you know done dealing with them for a while but you know it was
a topic that we definitely couldn’t skip and now they watch this tutorial I’m glad I taught
it to you guys so thank you guys for watching don’t forget to subscribe and don’t forget
to put a base case in your recursive functions or else your guys’s computer is going to crash
so thank you guys don’t forget subscribe and I’ll see you next video
what is going on guys welcome to your thirty-second C++ tutorial and in this tutorial I am super
excited because I’m going to begin talking to you guys about something called an array
so an array is basically one of my favorite parts of C++ because an array is basically
a variable that can store multiple values and you can do a bunch of cool things with
them and like I said it’s one of my favorite parts dealing with C++ so let’s go ahead and
get started like I said an array is basically a lot like a variable but instead of just
storing one value like 8 or 20 or you know 87 it can store multiple values so you’re
saying alright that’s kind of weird but let me go ahead and show you guys how to make
one in order to make an array you first write what data type you’re working with let’s go
ahead and make a simple integer array next just like a variable you type something you
want to call it I’m going to call my array Bucky because it’s pretty cool name and now
right after this with no space in square brackets you go ahead and you’re right how many numbers
in this case how many integers you want to store in your array so let’s go ahead and
say 5 so what the compiler is going to do now is it’s going to create an array that
can store 5 integers so whenever you run this your compiler is going to set aside some space
to hold 5 integers so we’re saying all right so now 1 array can hold 5 different integers
pretty cool huh so here is how well there are a lot of different ways you can create
an array but they’re probably the most basic and easiest to understand is called an array
initialize your list and an array initializer list is basically just a list of numbers and
you have to put it in between these squiggly brackets I don’t know the technical name for
them but they’re above the square brackets on your keyboard and in between it you write
a comma-separated list of 5 numbers so let’s go ahead and print out like um 66 75 2 243
and 99 and that is basically all it is to create an array so we went ahead and we created
it array called Bucky and Bucky was able to hold five integers and whenever we gave it
its 5 integers we had to put it inside something called an array initializer list so we have
566 75 243 999 so now whenever we want to use those values you’re saying RA say I wanted
to print out one of these values like two or something so let’s go ahead and see out
on Bucky okay well I can’t do that because if I just save the array name it doesn’t know
exactly what value I’m talking about here so in order to let C++ know what value you
want to use from that array we have to use something called the array index so let me
tell you guys this first when I said an array is made up of a bunch of different values
the technical term whenever you’re working with array is called elements and all they
are is values but you know we have to be professional here so we’re going to call them array elements
so 66 is one element seventy-five is one element two is an element so on and so forth so whenever
we want to index a specific element in our array we go ahead and write the index number
inside our square brackets so you might be thinking all right the index number for this
one is 1 this one is 2 and this one is 3 so let’s go ahead and try to print out Buckey
3 and see what happens let’s go ahead and end that line and we should be expecting 2
right here so let’s print it out and check it out 43 what the heck I thought it was going
to be 2 okay so 43 is the fourth one not the third one so what’s going on Bucky well the
reason that I printed out 43 is because whenever we’re dealing with computers I don’t know
if you guys know a lot about binary and zeros and ones but you know how people start counting
at 1 1 2 3 4 5 6 7 8 9 10 I learned how to get to 10 well computer they start counting
everything with a zero so whenever we create our array this first element even though it
would be number one to us to the computer it’s actually element zero so let’s go ahead
and print out Bucky zero and as you can see whenever we run this we get the number 66
so whenever we are referencing indexes or excusing the elements in our array we have
to remember that our array starts counting is 0 so 0 1 2 so this is actually the two
index so let’s go ahead and print out Bucky – and now we go ahead and we get to right
there so again the basic things I want you guys to remember is this an array is basically
a variable that can store multiple values the easiest way to create an array is this
right the data type right your array name and then right however many elements you want
your array to hold and we stored five integers in this case so that’s why we passed five
integers in in something called an array initializer list so basically whenever we create Bucky
five we’re telling our computer our right Bucky is going to be holding five different
values so compiler go ahead and set aside some space in our memory to hold five integers
so that’s what it did whenever it created this line and aside from that I want you guys
to notice this each value in the array I say value sometimes but technically they’re called
an array element and in order to reference one of those elements we use something called
an array index and the first element in every array is index zero so if you wanted to print
out 99 it would be zero one two three four so Bucky four whenever you’re around this
program is 99 pretty cool huh so that’s basically the most simple way I can explain array see
you guys again I said that one way to create an array was to use an array initialize your
list and the next tutorial I’m going to show you guys a different way to create an array
and it’s a lot cooler way so if you don’t quite understand arrays yet and quite understand
why they would be useful then just stick around and I trust me what I say I trust me of course
I trust myself and trust me you guys will in the upcoming story so again thank you guys
so much for watching and I’ll see you in the next tutorial
alright guys welcome to your 33rd C++ tutorial and in this story I’m going to teach you guys
an easier and quicker way to create an array and that’s through the use of loops so in
the last tutorial I tell you guys what an array was and also that we can create an array
using something called an array initializer list and this is actually pretty useful when
you’re dealing with smaller arrays but whenever we’re working with bigger arrays say that
store a hundred elements or a thousand elements we’re going to need a quicker way to create
that array instead of making a huge long list so let me go ahead and show you guys exactly
how to do that so in our very first line we’re going to declare our array we aren’t going
to set it equal to any values just yet so I’m going to make an array called Bucky and
Bucky can hold nine integers so in this line of code we’re pretty much telling our compiler
and compiler whenever you run this program you better set aside some space in our memory
enough space to hold a nine integers so our compiler says yes sir now what we need to
do is well let me just go ahead and whenever I’m done with this tutorial I’m going to print
out the elements on one side and the value so I might as well give it a nice little prompt
now element and value so what we’re going to be doing is we’re just going to print out
the element on one side and the value on the other side so let’s go ahead and take a look
at this right now one space for elements and I’m going to list those on this side and right
across from it I’m going to list their values right here so we’re saying all right you talk
to talk now time to walk to walk one way to create an array like I said before is the
use of loops and in this tutorial I’m going to be using the for loop so let’s go ahead
and see what parameters we add for this so we already know that whenever we create an
array that holds the nine elements every array starts at zero so I think whenever we created
this int X zero would be a good place to start and we also know that if we created an array
that can hold nine integers the last index in that an array excuse me the less index
in that array is going to be 8 because count with me get your fingers out 0 1 2 3 4 5 6
7 8 I only counted to 8 but since we had 0 in there that takes care of our 9 elements
pretty cool huh so we only want to loop from 0 to 8 so that’s why you’re right X is less
than 9 we can make it easier X is less than or equal to 8 oops not X comment less than
or equal to 8 either way it works fine X is less than 9 is a little bit quicker but you
know what’s one extra character so now of course we’re going to use X plus plus the
increment X by one each time so now whenever we see out X and line let’s go just go ahead
and run this real quick so I can show you guys we get that 0 through 8 and you’re saying
that’s pretty cool so now we have the numbers 0 through 8 and those just happen to be the
exact numbers that we need in our array so now what we can do is something like this
Bucky X equals 99 or something stupid so what this is going to do is it’s going to go through
your array each element one by one and assign the value 99 so the first time it loops it’s
going to set the Bucky zero element to 99 the next time the Bucky one element to 99
so it’s basically going to set every element in your array to 99 until it gets to eight
and then it’s going to do it one more time and stop at 9 so pretty cool huh so let’s
go ahead and print this out and see exactly what I did so see out first we’ll go ahead
and print out X which is the element number same as this so this is going to be 0 through
8 on the left-hand side and now just go ahead and give us some space to work with and after
this just go ahead and write the value of whatever element is stored in there so this
is going to be 99 if we coded it correctly so let me go ahead and end this line and whenever
I run this you guys will clearly see what’s going on so what we did is we loop through
each array element one at a time first Bucky 0 then Bucky 1 the monkey 2 3 4 all the way
to 8 in each time we loop through it we set that element equal to 99 so now we have an
array called Bucky that has nine elements 1 2 3 4 5 6 7 8 9 and each of those elements
is equal to the value of 99 there are actually a lot of nines in this soil but you know it’s
good number so you know it feels appropriate so let me talk you guys through this one more
time in the code in this line of code we’re basically declaring a 9 element Bucky array
we’re telling our computer make sure you set set aside enough space to hold 9 integers
in the array Bucky so it that’s what it did in this line and this line all we did was
print out a title basically and in our loop we knew that since we’re creating Bucky 9
array that the indexes that it need was 0 through 8 so we made a simple loop that basically
loop through the numbers 0 through 8 in with those numbers we could reference the Bucky
element and assign it equal to the value of 99 for each one and then of course we just
printed out the index which was 0 through 8 and then the value which was 99 each time
so simple enough so that is basically two different ways to create an array um again
whenever you’re working with a really big arrays in I don’t know it’s just a lot easier
than using an initializer list because if you created an initializer list with like
a hundred values then you would go ahead and have to type a hundred values in here and
it will just get messy really quick so this is a faster way um it takes a little more
thinking but it’s definitely worth it so for now that’s all I have for you guys thank you
thanks for watching don’t forget subscribe and I will see you next door Oh
welcome back eyes through 34th C++ tutorial and in this lesson I’m going to teach you
guys how to use an array in calculations and it’s actually a pretty simple program definitely
useful definitely something you want to stay tuned for so let’s go ahead and make an array
right now let’s call it tuna I’m getting tired of using Bucky every time and let’s set the
tuna array big enough to hold five integer values looks good enough and now let’s go
ahead and use an array initializer list um let’s go ahead and throw in 2050 476 832 and
whatever this number is right there so now we created an array called tuna and it has
five different elements or five different values in it so now what we want to do is
say our boss came to us are a teacher and he’s like build a program that calculates
the sum of all of these elements so we’re saying all right the first thing we need is
a variable to hold that sum and by default since it doesn’t have any elements at first
we’re going to set it equal to zero so what we could do is this sum equals looks I try
to type Bucky again tuna zero plus tuna oh you know what I’m getting a bunch of typos
and this is just taken too long not very efficient way of doing it although it would work so
let me go ahead and show you guys a much easier way to use an array in calculation so instead
of all that typing let’s go ahead and make a for loop instead so now what we need to
do is just like before find out which array index is our array is going to need and just
use our for loop to loop through those array indexes so of course as always we need to
start at zero because the first index in every array is zero alright now let’s do a little
bit of thinking but not too much tuna holds five values so that means the last index in
that array is four so if you’re right X is less than 5 what it’s going to do is going
to go from zero to four it’s not going to do five because five is not less than five
so now when you press X plus plus we now have a four loop that loops from zero to four so
it’s going to go zero one two three four those are going to be your X values so what we can
do now is just use some plus equals tuna X now what this is going to do is this the first
thing it’s going to do the very first time it runs the loop is going to say this all
right X is equal to zero so add tuna zero which is 22 the sum now the sum is going to
be equal to 20 the next time it’s going to be one so add tuna one which is 50 for equal
to the sum which is 20 so now it’s going to be 74 so it’s going to go through each of
these elements one by one and add each value to the value of sum so let’s go ahead and
just print out this on the screen um see out some end line and now you can see a better
idea of what’s going on so like I said one by one the very first time it runs it’s going
to add tuna zero equal to sum the next time tuna one equal to some tuna – two – three
– four so if you add up all these values right here you get the value of 1528 so one last
time what we did is this we created an array called tuna using an array initializer list
that holds five integers 1 2 3 4 5 now what we wanted to do is we wanted to calculate
the sum so we made a variable called sum and of course by default since it didn’t have
any value we set equal to 0 then we made a for loop using the value in here excuse me
the variable to loop through each index of our array so it started at 0 and since we
know this one was two and a four that’s what we put for here X is less than five and of
course we want to go one at a time we don’t want to skip you know this one in this one
or anything so then what we did is we loop through each value and added it to the variable
sum and then we just printed out some on the screen and that’s what we got 20 74 115 9
82 15 28 so that’s a real simple program in order to use arrays and the array indexes
and elements and values and calculations yadda yadda yadda all that fun stuff so now you
guys know how to use arrays in calculations you’re ready to move on to my next tutorial
we’re going to be talking I’m going to teach you guys something else about an array that’s
actually pretty cool so don’t forget to watch that tutorial thank you guys for watching
and don’t forget to subscribe what’s going on guys welcome to your 35th
C++ tutorial and in this tutorial definitely don’t want to miss this one because I’m going
to teach you guys how to pass arrays into functions and there are a lot of weird tricks
that you need to watch out for so make sure that you pay attention to every single detail
so let’s go ahead and make a really simple function I’m going to make it really easy
the only thing it’s going to do is print out an array you know one by one so far array
was like 4321 eight it will print out 4321 eight really simple but you know it’s a nice
clear example so let’s go ahead and make sure you’re working outside your main function
right here and call it anything you want I’m just call mine print array and it’s going
to take some parameters the first parameter that’s going to take is the array itself that
we’re going to pass it in so let’s go ahead and say we’re passing it in an integer array
go ahead and write int and then give your rate a name the array and whenever you pass
an array into a function you need to write those square brackets right after it if you
don’t write those square brackets it’s going to think that you’re going to try pass in
a variable and we want to tell our compiler no our first parameter whenever we use this
function is going to be an array hence the square brackets so what people typically do
whenever they pass an array into a function they usually want to pass in the size of the
array too because you know that’s information that you definitely need to know later on
so just go ahead and name the size of array and this is of course going to be how many
elements are in the array so now like I said this is the easy part let’s just go ahead
and build a function that prints out on you know each element in the array so for int
x equals zero because remember we always want to start at a zero so that’s never going to
change the only thing that’s going to change is this second thing we need to pass in X
is less than size of array so check it out we can go ahead and write you know later on
when we create our array instead of using size of array x is less than 10 or X is less
than 20 but now whenever we use this function we can pass in any array and any size we want
and it’s going to work perfectly so instead of setting this equal to a number like 10
15 5 I like to set equal to a variable because then it’s a lot easier later on and of course
X plus plus incremented by 1 so now whenever we pass in an array it’s going to loop through
each of its elements 0 to the last element so now let’s just go ahead and print out on
the screen the array and of course for here we plug in X and endline so now once I fix
this right here what we built is a function that bit basically takes two parameters an
array and the size of the array we needed this information because whenever we created
our loop we needed to know how long to make that loop last and of course for each line
all we did is we print out whatever element whatever value that element is equal to so
now let’s go ahead and prototype this function right now because remember we’re going to
be using the function right here in the main but whenever it tries to use this function
it’s not going to know that it’s a function that we built in less we prototype it so let’s
go ahead and copy this and if you remember from like I don’t know the 10th tutorial something
around then if we prototype it now we don’t get an error message whenever we try to use
this function because it knows that it’s a function that we created so let’s just go
ahead and make two arrays to work with int Bucky and we’ll set this equal to a 3 injury
and go ahead and add any three integers you want um 20:54 and 675 sounds good to me and
actually let’s go ahead and make another array just to make sure our program is working fine
I like to name my arrays names and I’m going to name this one Jessica and she’s going to
hold 6 integers so set this equal to get that elbow way and 54 on why do I use 54 L time
24 7 8 & 9 and how many is that 5 and another 199 good go so now we have 2 arrays Bucky
which has 3 integers and Jessica which has 6 integers so whenever we want to use this
function that we just created just go ahead and type the name of your function print array
and give it the two pieces of information needs but you know I have to begin this story
I said that there are a couple of tricky things that you guys need to watch out for this is
one of them you might think in the first parameter you might type Bucky or Bucky square brackets
or Bucky square brackets 3 but actually the only thing you type is the name of that array
so of course you well how can I explain this you already created an array called Bucky
so now whenever you pass it in it knows it’s on the right you don’t have to tell it that
you’re passing in an array because obviously if it created it already it’s going to know
so that’s why whenever you pass it into a function you don’t use those square brackets
at all and of course the second piece of information it needs is how big is this array so let’s
go ahead and make sure 3 and passing 3 4 here so now let’s go ahead and build and run this
program and check it out 20 20 54 54 675 675 and now let’s go ahead and use this for Jessica
just cut array again with no square brackets and six so let’s go ahead and build and run
this and it looks good to go 54 24 seven eight nine ninety nine pretty good so one last time
here’s what we did in the very first line of code we built the function prototype because
if we didn’t have that prototype let me just go ahead and comment this out and we try to
run it we’re going to get an error message so this error message pretty much means this
our writes I did this I did this and now you want me to do this what the heck is print
array so when we do include that prototype and try to run it it knows that Oh print array
is actually a function that you guys built alright so now I’m going to run this and I
know to look for a function so anyways that’s what prototyping did in the main we just made
two arrays and in this line we just called that function and passed in the array name
and the size of the array so then whenever we called Jessica six it went down here to
print array for this parameter it was Jessica and for this parameter it was six and then
it looped through it pretty much looped through a loop six times and said all right for the
array which was Jessica print out each index of that array zero to five and that would
give you your six elements so this is basically how you use an array in a function and the
only couple things you need to watch out for is this don’t forget to prototype your functions
don’t forget whenever you are making your function itself to include the square brackets
but whenever you use the function in your main program do not include the square brackets
because your compiler is smart enough to realize that whenever you use Jessica it obviously
is an array because you told it right above it so as long as you remember those simple
things you will be good to go and that is how you use an array side of function so it’s
kind of complicated at first but if you just do it like two times you’ll be a pro at it
so that’s all I have for you guys for this story thank you guys for watching and don’t
know when we talk them out in the next tutorial but it’s going to be cool so I’ll see you
then what’s going on guys welcome to your 36th
C++ tutorial and in this story I’m going to talk to you guys about something called a
multi-dimensional array now a multi-dimensional array is basically an array inside another
array so yeah I think about that for a little bit and get freaked out you might be thinking
whoa this reminds me of Inception a dream within a dream what’s going on and you guys
are thinking oh man this tutorial is going to be super confusing I barely even understand
arrays and now you’re going to say an array inside an array all right well here is an
easier way to think about it if you ever you know use Microsoft Excel or maybe use MySQL
think of it as a database or a table a multi-dimensional array is basically made up of rows and columns
so this is the easiest way where you can think of it in its basically whenever someone has
information and they want to organize it in rows and columns they use a multi-dimensional
array so let’s go ahead and create one right now and I’ll show you guys the syntax for
it go ahead and pretend you’re creating a basic array I’m going to name mine Sally and
instead of a 1 square bracket actually takes two square brackets in the first square bracket
you write how many rows do you want to have in your array let’s let’s keep it simple and
say we want to have an array with two rows and for the second one it’s basically columns
how many columns do you want to have in your a so let’s go ahead and add 3 columns so now
we have a row excuse me now we have basically a table that’s two rows by three columns aka
there are six elements in this array so in order to create um an array inside an array
it’s basically exactly like you would think go ahead and make your array and just to tell
you guys this code blocks gets freaked out whenever you try to put square brackets inside
other squirly brackets so that’s why it might be messing up when you try typing it but anyways
go ahead and as the first element in your array you actually add an array so let’s go
ahead and add two-three-four so check it out we have an array two three four inside another
array and this smaller array is the first row so now let’s go ahead and make our second
row or second array add a comma and like I said codeblocks gets freaked out whenever
you try to do it so it might be weird but let’s go ahead and in this array put eight
nine and ten so we basically made it two different arrays one array is two three four and the
other array is eight nine ten so we said our a we’re creating a multi-dimensional array
consisting of two different arrays each array is a row in each value is going to be a column
and remember to separate your arrays with a comma so basically this is an array made
up of two smaller arrays so let me go ahead and draw this for you guys visually like I
said there are two different rows one is two three and four and the other one is eight
nine and ten so visually this is what our array looks like this is all one array that’s
basically made up of this array 2 3 & 4 and this array 8 9 & 10 2 3 & 4 is on top 8 9
10 is on bottom two rows three columns pretty simple huh so anyways the basics of this is
anytime you want to create a multi-dimensional array go ahead and list number of rows number
of columns and for the number of rows that’s how many arrays you create and for the number
of columns that’s how many values you add to each array and then you end up with something
like this so if you’re saying alright now how do I reference you know how do I get to
each element in that array well let’s go ahead and just print it out on the screen um of
course print out Sally and whenever you reference it it just takes the two brackets the first
index you need is what row is it in and remember the first row starts with 0 so anytime we
want to access element in this first row we need to go ahead and put 0 now remember 2
is going to be 0 oh three is going to be one and four is going to be two so if you’re right
zero two it’s going to look at the zero with Rho which is the first run and the second
element which is actually zero one two so it’s going to go zero two and that’s going
to give us our four it’s basically just like coordinates I don’t know why I didn’t say
that before I didn’t cross my mind but basically multi-dimensional arrays are exactly like
coordinates you’re asked the coordinate here start with zero in the coordinate here so
if you want to reference nine that would be zero one so go ahead and type one for that
and zero one right there so that let’s go ahead and print that out and I got my nine
so basically a multi-dimensional array just like Inception is an array or more than one
array inside a bigger array it’s pretty much an array made up of arrays instead of values
and what it does is it gives you the idea that you can create a table basically made
up of rows and columns and anytime you want to access one of the elements you just go
ahead and type in the coordinates which would be the row number and then the column number
and you print it out and there you go so if you guys understand this then you’re ready
to move on to my next topic in next tutorial I’m going to teach you guys a really simple
way to print out a multi-dimensional array so instead of you know just having to comment
this right here you have a nice visual of what it looks like so if you guys don’t understand
it then go ahead and just go to my forum and ask me a question and I’ll explain it better
there but if you do or you know maybe if you’ve got any good inception jokes then leave them
in the comments below but for now thank you guys for watching don’t forget to subscribe
and I will see you in the next tutorial alright guys welcome back to your 37 C++ tutorial
and in this is tutorial I’m going to show you guys a really easy way to print out multi-dimensional
arrays so let’s go ahead and first thing we need to do is create a multi-dimensional array
go ahead and name it Bertha and Bertha can be two rows by three columns same as before
so now what we need to do is go ahead and make an array of arrays so the first thing
I do is put my two arrays in there separated with the comma so now we have two arrays which
would be two rows and now put three values in each like um we’ll just put one two and
three in the first one and seven eight and nine in the second one so now we have an array
called Bertha that has two rows and three values in each row so it’s basically a table
that’s two by three so in order to print this out what we need to do first is we need to
loop through each row one by one we’re going to loop through the first row and then we’re
going to loop through the second row so let’s go ahead and make a for loop and int well
we might as well name this row and of course it always starts is zero because the zero
if index is the first element in your array so row zero and the second one is Row is less
than two so what this is going to do when we put row plus plus is it’s going to go from
zero to one so row zero then Row one it’s not going to go row two because of course
two is not less than two so now we’re looping through each row one at a time and now of
course you guys can probably guess what I’m going to new next we need to loop through
each column or each element in the row so now in order to do that go ahead and make
another for loop and this is going to look really familiar familiar there we go int column
make sure I spelled it right this one starts at zero as well again all array start at zero
even there even if they’re inside another array column is less than three because that’s
how many columns you have you basically write the maximum the number of rows in the number
of columns right there simple enough you know what that kind of looks like a heart hmm that’s
kind of cute anyways moving on column plus plus so now what we’re doing is this we’re
looping through each row one by one the first time it loops through Row one it’s going to
loop through each of those elements 1 2 & 3 and then it’s going to loop through Row 2
and then it’s going to loop through each of those elements 7 8 & 9 so after we get through
looping through each column we want to move to the next line see out and line because
again each row is on a different line so that’s why when we’re done looping through the last
element in the row we want to move to the next line so now we can just go ahead and
print it out on the screen like see out a Bertha was your name and you put the row number
which would be this 0 or 1 and then you put the column number which is going to be some
value 0 to 2 and that would be these values right here so let’s go ahead and make sure
you don’t write end line on actually what we do need to do though is add a space there
but make sure you don’t write end a line on this line right here because these are your
column values you don’t want every single one of these numbers to go on new line you
only want it to go to a new line after you’re done looping through the last element in that
column so let’s go ahead and print it make sure I didn’t forget any semicolons which
I did somewhere and let me see what’s wrong are oh I see what I did here is I put a call
come on eyelet put a comma instead of a semicolon so now let me check make sure to run it again
and there we go in our first row it printed out one two three and in our second row it
printed out seven eight nine so this is a perfect way to visually see multi-dimensional
arrays so pretty cool huh so again one last time here’s what we did I’ll just leave this
down here we made an array that was two by three and what that means is it was an array
consisting of two different arrays with three values each and what that means is basically
made a table that was two rows by three columns as we can see right here the first thing we
needed to do is loop through each row loop through this row the first one and then loop
through the second one and we did that by writing row 0 rows listen to yahya data all
that fun stuff you basically the only thing you need to worry about is make sure this
value right here is the same as this value right here so we said all right for each row
we loop through we wanted to loop through each one of the elements one at a time so
that’s why I wrote start with zero one two one at a time and why we had this little spacer
right here is because if we didn’t have that it would put one two three real close together
so what it did is it basically printed out a row one element at a time and then when
it was done it said see out end line and that’s why it jumped to this next line and printed
out seven eight nine and it did that until it was done printing out all the rows and
then it just ended so that is how you print out a basic multi-dimensional array again
of course this is a whole lot to see visually it’s a whole lot better looking at than this
right here so that is why you actually use this quite a bit whenever you’re programming
so that’s all I have for you guys for this tutorial thank you guys for watching hopefully
you understand how to print out a multi-dimensional array now because we’re going to be done talking
about arrays for now in the next tutorial I think I’m going to be covering something
called vectors which is kind of like an array but it’s a little bit different so thank you
guys for watching don’t forget to subscribe if you have any questions check out my website
and I’ll see in the next video alright guys welcome back to your 38th C++
tutorial and in this tutorial I’m going to be beginning to talk about something called
pointers now pointers is something that confuses a lot of people so I’m going to try to explain
it really simple for you guys now you remember how we made some variables that contain strings
of text and we made some variables that contain integers and we made some variables that contain
floats well a pointer is a special type of variable that contains a memory address as
their value pretty weird huh so let’s go ahead and the first thing we need is a plain old
integer variable so let’s go ahead and make a variable called fish and set it equal to
the value of 5 it doesn’t really matter what the value is to be honest so anyways we have
a plain old variable named fish now let me go ahead and show you guys how to retrieve
the memory address of this variable so as you probably already know this variable called
fish is stored somewhere on a computer somewhere in the memory you know who knows where so
sometimes we need to be able to access where indeed that memory is so in order to do that
you go ahead and you write that and sign and the and sign is called the address operator
and now whatever variable you type after it this means the address in your memory of fish
so if we go ahead and print this out on the screen using C out and might as well end that
line hello can’t type in line today and L there we go I’m going to go ahead and build
and run this we see that we get this weird hexadecimal who knows what this is this is
actually the memory address of where the variable fish is so again like I said the first thing
we need to find the address of something is of course a variable because we need to find
the address of something and in order to find the memory address of the variable you go
ahead and you write the and sign right before the variable and then those two combined pretty
much tell your C++ compiler all right I want to print out the memory address of fish not
the variable fish itself so this is of course the address of where your variable fish lips
now you’re saying all right well you just want over address operators I thought you
were going to go over something called pointers well let’s get into that right now a pointer
like I said is a special type of variable that points to a memory address in other words
um a pointer is a variable that contains memory addresses a value so let’s go ahead and I’ll
show you guys how to make a pointer in order to make a pointer go ahead and write the type
of data you’re going to be pointing to so we’re going to make a pointer that points
to an int value or in other words an int variable so this is going to be an int pointer simple
enough now in order to tell C++ we’re making a pointer instead of just another variable
we need to add an asterisks that little star above the eight on your keyboard and now we
can go ahead and name a variable like fish pointer and this tells our compiler that we’re
going to be making a pointer instead of a regular variable again if we just had it like
that our compiler would think that we’re just making a regular variable but we’re not we’re
making a pointer so that’s why we needed that little asterisk before fish pointer so we
went ahead and we declared a fish pointer right here pretty much telling our program
we’re going to be making a pointer called fish pointer and what this fish pointer is
going to do is it’s going to point to a memory address so now we have to of course assign
a memory address so let’s go ahead and whenever you assign a memory address to a pointer you
don’t need to include the star again because you only do that whenever you’re first creating
the variable from now on whenever you use the variable fish pointer C++ already knows
that it’s pointer because you told it whenever you created the variable so make sure you
don’t put star fish pointer twice only one time C++ is smart enough to recognize this
now go ahead and set it equal to the memory address of variable and we only got one variable
fish so now fish pointer is equal to the memory address of fish it isn’t equal to five it
is an equal to the word fish fish pointer is equal to the memory address of where the
variable fish lives that long hexadecimal thing so now when we go ahead and see out
print fish pointer and go ahead and then that line now these two lines should be identical
because first we’re printing out the memory address of fish and next we just are basically
printing out the memory address of fish so let’s go ahead and make sure these lines are
the same so I don’t look like a fool and see there we go so basically we know how to print
out the memory address of a variable using the memory address operator that’s a pretty
nice name for it so then I taught you guys how to create a pointer and all a pointer
was a special type of variable that had a memory address as its value so again whenever
we ran this we can’t assign this to an INT because it’s not an integer we can’t assign
it to a float because it’s obviously not a float so we have to assign it to a special
type of variable called a pointer so that’s why you need to be able to use pointers and
of course when we just printed it out it printed it out the memory address pretty cool huh
so that is your basics of what a pointer is how they’re created and how you obtain the
memory address of a variable so now with all that knowledge we can begin programming and
we’re actually you’re going to use these pointers and programs and they’re actually useful for
something believe it or believe it or not so thank you guys for watching and hopefully
you understand everything in this story if you have any questions don’t forget to check
out my website the new Boston.com and I’ll see you guys in the next tutorial
what’s going on guys welcome back to your 39 C++ tutorial and in this story we’re going
to be covering pass by reference with pointers and we actually have a ton of stuff to cover
so I don’t even know if I’m gonna have enough time in this one store oh I might have to
break it up into two different tutorials but let’s go ahead and get started anyways so
we already know from our last tutorials that whenever we pass an argument into a function
passing argument into a function basically means giving our function any additional information
that it needs like in order to calculate the square root of a number you actually need
a number so you have to pass a value into that function but believe it or not it isn’t
quite that simple there are two different ways that I’m going to be teaching you right
now they’re actually more than that but for now I’m going to be teaching you two different
ways to pass arguments into functions one of them is passed by value and one of them
is passed by reference now what do I mean when I say that what we’ve been doing is basically
passing by value and whenever we create a function and we pass it by value we pass a
copy of that variable into the function so the original variable itself cannot be changed
until we set it equal to another value so that way the function only has access to the
copy of that variable not the original variable itself simple enough values pretty much mean
passing in the copy okay now pass by reference means this whenever you pass in a variable
by reference you pass in the variables address and whenever you do that you basically give
the function direct access to that variable so in that sense the original variable can
indeed be modified inside your function so why would you ever want to do that it seems
kind of dangerous well the one benefit is it’s basically less stress on your computer
instead of passing by value where every time you call that function it has to make a copy
of the variable suppose you had 100 variables that would be pretty stressful on your computer
if you pass by reference and you pass in the value itself it doesn’t have to make copies
of every variable so let’s go ahead and let me start off by making two variables int Betty
and I’ll set this equal to 13 and let’s make another person int sandy and set this equal
to 13 to now what we’re going to be doing is passing this one in by value and passing
this one in by reference and see how they turn out so we’re going to be needing to create
two different functions first let’s go ahead and create a function that we’re familiar
with pass by value so void pass by value and let’s just go ahead and create a basic function
it will take a parameter of one integer and what we’re going to do is take this integer
and set it equal to 99 so basically we’re going to be doing is Betty pretty much X is
a copy of the variable Betty so then whenever we set X equal to 99 it doesn’t set Betty
equal to 99 it just sets this copy equal to 99 so that’s what’s going to happen now let’s
go ahead and make a function pass by reference so void pass by reference hopefully I spell
that right there are way too many E’s and efference reference for YZ I mean come on
guys enough of these so functions that receive a memory address they need to have a pointer
argument because they need to have a pointer because a pointer is the thing that receives
the memory address so now instead of in X you need to type int pointer X because the
pointer X is now going to be able to hold that memory address so now when I whoa one
little extra thing there so now what we can do is something like this points or X is equal
to 66 or something like that so now instead of the copy of X being to 99 and now the variable
itself sandy X is the address of Sandy so sandy is going to be changed at 66 so whenever
we pass Betty in this one her values not going to be changed whenever we pass sandy into
this we’re basically passing in sandy directly into this value into this function her address
and everything so now this is a direct access to the variable sandy so let me go ahead and
first of all we need to prototype these functions so copy this and paste got a copy it or did
I just do something weird I might’ve had a extra character somewhere better not get error
out so I’m going to be ticked off so copy that and then copy this reference and pass
paste this in and now check it out before we go ahead and well let’s go ahead and just
use these right now and then I’ll talk to you guys about the thing I was going to so
let’s go ahead and pass by value and we will pass Betty into this one so we’re going to
pass 13 in that but her value isn’t going to be changed now let’s go ahead and pass
by reference sandy and we’re pretty much given this function direct access to sandy so that
value is going to be changed so now what we need to do is here’s what I’m going to talk
to you guys about since we’re passing in the memory address of course you remember from
the last tutorial we need the ampersand because sandy is the variable and this function takes
a memory address so the symbol for the memory address of the variable is ampersand sandy
this is basically equal to some long hexadecimal address of where that variable is located
on your computer so now let’s just go ahead and print out the values and see what we get
see out arm Betty is now and then just go ahead and print Betty and end the line and
now do the same thing for Sandy is now sandy and lime so let’s go ahead and run this in
I guess I can see if I messed it up anywhere nope looks good so as you can see we basically
built identical functions however when we passed in Betty it stayed 13 and when we pass
it in sandy even though it was 13 it changed it to 66 so now you can see whenever we pass
let me move this down right here whenever we pass a variable into a function we can
do it in one of two different ways we can pass a copy of it like we did in the Betty
case pass by value and what this does is it basically makes basically X is a copy of Betty
it doesn’t have any access to Betty itself it’s just a variable with the same value so
then we set X equal to 99 it’s just setting 99 equal to that copy not Betty itself so
if we pass by reference what we do is we take the memory address of that variable and remember
your memory address is some part on your computer it doesn’t make a copy of it it passes in
that memory address itself that memory address has the value of 13 and we’re saying alright
now take that memory address and set it equal to 66 so no copies involved here it’s taking
that same address the entire time and changing the value of it so now you can see the difference
of whenever we use a function like this it makes a copy and we get whenever we use a
function like this it works with the direct variable itself so that way whenever we ran
the two functions right here and printing it out on the screen that’s why Betty stay
the same because that copy had no effect on the original and sandy changed because it
did indeed change the value of that one address that variable was only a one address and we
passed it in so it had direct access to it so I am out of breath and hopefully you guys
understand at least the concept of pass by value and pass by reference just remember
when passing by value you’re passing in the copy when passing in reference you’re passing
in the variables address if you understand that you’re good to go at least for now so
in the next tutorial I’m afraid we’re not done well I got like Fleming my throat I can’t
talk I just had a milkshake for McDonald’s and anyways moving on so in the next tutorial
unfortunately we’re not done covering pointers yet so yep more about pointers I’m sorry so
thank you guys for watching don’t forget subscribe and I’ll see you guys in the next video alright guys welcome to your 40th C++ tutorial
and in this tutorial we’re going to be taking a quick break from pointers and we’re going
to be covering something on the size of function now the sizeof function is actually a function
that you use a lot when working with pointers but for this demonstration just to give you
guys an idea of what the sizeof function does I don’t want to include pointers because that’s
too much information for one tutorial so we’re just going to be seeing a simple demonstration
of the sizeof function and then later on we can apply it to pointers but for now let’s
go ahead and check it out so what the size of function does is it basically determines
the size of either array or maybe a variable or a constant it pretty much determines the
size of something in bytes and remember bytes is just the size of memory like 4 bytes 1
byte yada yada yada so let’s go ahead and make a basic variable um I don’t know if I
tell you guys the chart yet but char just is a variable of character type it basically
sword’s a character so let’s go ahead make a variable char and we’ll call the variable
C let’s go ahead and see how the sizeof function works so you go ahead and type size of and
then in the parameter you type what do you want to get the size of well let’s just go
ahead and get the size of this variable right here and then we’ll just end that line and
that’s it you go ahead and run it and outputs the size of character which is one byte so
any character we store in our computer it takes one byte of memory all right simple
enough so let’s go ahead and make another type of variable like um an integer variable
so how many bytes do integers take up on our computer well if you don’t get the same number
as me don’t worry but because every computer is a little bit different on my computer an
integer takes up four bytes of memory so again if you have like a 2 or a or maybe you have
4 same as me I you know don’t think you’re running the program wrong every computer stores
memory differently mine says 4 so anyways we know that a char takes one bite an integer
takes 4 bytes and now let’s go ahead and see double well we already know an integer can
store numbers without a decimal point and those take up four bytes a double is pretty
much a more precise integer so that’s why whenever we run this we’re expecting to see
8 bytes or something higher than 4 and the reason that doubles take up more memory than
integers is because they’re more precise so anyways what I want to stress is the more
precise a value is the more memory it needs to take up so that’s all I wanted to stress
in now let’s jump into something else instead of gee these basic datatypes let’s say we
had something like an array now you know an array is basically like 10 integers strung
together or you know maybe 15 double strung together so let’s go ahead and make a double
array and we’ll call it Bucky and it can store 10 doubles now remember a double took up 8
bytes on my computer so we have an array of Bucky and it has 10 doubles we’re expecting
to see 10 times 8 bytes so this entire array should take up 80 bytes of memory so let’s
go ahead and see if our prediction is right size of Bucky you go ahead and just write
the name of the array in there and then go ahead and inline not Ned line and line there
we go so now whenever we run it check it out 80 bytes right there and that is because each
double is eight bytes and we’re going to store ten of them so that’s why this entire array
took up 80 bytes of memory simple enough but you’re saying all right so I know what the
sizeof function does but why would I ever need to know you know the size of bytes I
mean that’s the computer’s operating systems from I really I’m just the programmer I don’t
have to worry about memory that much well one useful way you can use this function right
now before I you know jump into pointers and all that stuff is maybe you want to find out
how many elements are in our array so you know maybe you had the user make an array
or something and you know how many elements ahead and you need to know how many elements
this array had in order to make a calculation well of course we know that this array has
ten elements but say we were trying to figure it out it anyways something you can do is
this see out what you can do is get the size of Bucky remember this would give you the
80 bytes this is the entire size of your array 80 bytes and what you do is divide it by the
size of one element and do that just go ahead and write size of Bucky zero which is the
first element and then whenever you printed it out in line you should get ten so let’s
go ahead here’s the 80 before and now we get 10 which means ten elements in our array and
again why did this happen what we did is we took the entire size of our array Bucky which
was 80 bytes and we divided it by one element size which was eight so 80 bytes divided by
eight bytes gives us ten elements so again any time you want to get the number of elements
in your array say you know couldn’t see that line for some reason take the entire size
of your array and divide it by the size of one element and your answer is how many elements
you get in your right so again here is a basic example of the size of again all we did is
find the size of different you know data types arrays variables yadda yadda but later on
we’re going to be using this to work with pointers and it’s actually a lot more useful
than just finding how many elements are in your array so you know now you guys know the
size of now whenever I teach you later on you won’t get confused so for now thank you
guys for watching don’t forget to subscribe and I will see you in the next video
alright guys it’s time to jump back into pointers and specifically in this tutorial I’m going
to be covering pointers and how we can use math with pointers and even more specifically
I’m going to show you guys you know how to add integers to pointers and why the result
isn’t exactly what you might expect so let’s go ahead and start off by building a basic
integer array I’ll call my Bucky and it can hold five integers now we already know that
each integer on my computer takes four bytes of memory on the older computers they usually
either take up two bytes or four bytes on some of the newer computers they might take
up eight bytes so you know if you’re running this tutorial with me and it’s not the exact
same results don’t freak out it’s just how your computer stores integers so let’s go
ahead and make a couple pointers I’m going to make a pointer named BP BP 0 and this is
going to be equal to the address of Bucky zero in other words it’s going to be equal
to the address of the first element so let’s go ahead and print this out right now and
see what the address is so on just give a nice little prompt like BP zero is at memory
address and let’s print out BP zero that will print out the memory address and of course
that’s in that line so now let’s go ahead and build and run this and we see that BP
zero is at the memory address ff0 eight okay simple enough so since we built an array and
since we know that each integer takes up four bytes we’re expecting the next pointer well
let’s go ahead and build a couple more pointers first and we’re expecting the next pointer
to be a memory dress for away from that so if that one was eight the next one will be
at twelve and the next one will be at sixteen so let’s go ahead and one this will point
to the second element and this will point to the third element so now let’s go ahead
and print all these out on the screen and when I do we can clearly see what’s going
on 1 & 2 & 2 so basically we’re bringing out the memory address of this and then the memory
address of this and then the memory address of this so let’s go ahead and print this out
and check it out bp0 is it location FF 0 0 and since we’re storing integers and they
take up 4 bytes the next one is 4 bytes away FF 0 4 the next one is 4 bytes away FF 0 8
and if we were to go on I’m not going to but we can clearly see that each integer takes
up 4 bytes of memory on our computer and since we’re building an array all these integers
are stored one right after each other so that’s why each element in our array is stored 4
bytes after the next one basically whenever you make a ninja on computer it reserves 4
bytes of memory for it and you know nothing else can go inside that memory except that
integer so let’s go ahead and say we’re playing around and we decided to do something stupid
like be p0 and we set it to add to 2 you know this memory address so let’s go ahead and
put plus equals 2 well now let’s go ahead and print out the address of b p0 again and
see what happens remember before it was stored at arm the address FF 0 0 so now when we add
it to to it we should probably be expecting this to be a FF 0 2 but let’s go ahead and
run this to see what happens okay like before the first element will store to FF 0 0 then
F F 0 4 for the next one then FF 0 8 for the next one but what we did is we took this first
one FF 0 0 and we added 2 to it so we should be expecting an FF 0 2 right makes sense that’s
how it works in our brain but our computer said b p0 is now it ff0 eight well that was
the address of this one right here so what is going on what happens is because pointers
don’t work like math works in our head whenever you add integers third pointer it doesn’t
change the memory address of that pointer it only changes what element it’s pointing
to so for example whenever you run this and we try to add two to this pointer right here
it didn’t change the memory address right here what it did is said all right I’m going
to take your pointer and add two to it and basically I’m going to point to whatever is
two elements away so again what I want to stress is whenever you’re working with math
and pointers you’re not changing the memory address you’re only changing the element that
appoints do so again you’re not changing this piece of memory right here you’re changing
what element it’s pointing to simple enough like I have a really hard time explaining
that but you guys can see what’s going on so for example if I put be p0 plus plus it
wouldn’t change it to FF 0 1 it would change it to FF 0 4 because let me go ahead and prove
it to you guys first so we added 1 to this it didn’t change the memory to 0 1 it changed
it to 0 4 because 0 1 it would overlap this one which would overlap this one and cause
a whole bunch of problems so what it does is and change it instead of changing the memory
address it just says alright if you’re adding 1 to this pointer then you probably want to
just point to this pointer right now so that is basically how pointer of Arithmetic works
basically instead of changing the memory address it just changes where it points again you
guys can clearly see in this example what’s going on so you know just look at it and you
guys can probably understand that a whole lot easier than me trying to explain it so
there you have it that’s how pointers work with math and why you might be getting a different
result than you might expect so thank you guys for watching don’t forget subscribe in
the next tutorial no idea what I’m going to be covering but I’m probably going McDonald
so you guys chill out wait a couple seconds and I guess I’ll see you guys in the next
door alright guys in this video I’m going to be
showing you guys what the arrow member selection operator does and when you should use it but
I was thinking and I was like you know what we haven’t created a new class in a while
and it actually would be kind of useful in this tutorial so let’s go ahead and start
working with the new class so just a refresher from I don’t know I probably did this like
20 tutorials ago in order to create a new class press file new class and then you go
ahead and name request anything you want I’m going to name mine Sally and I don’t think
we covered destructors yet so let’s go ahead and uncheck those and make sure we’re working
in the same directory as our main.cpp so everything looks good go ahead and click create and of
course it’s going to give you these two confirmation yeah of course I wanted to do both of those
and now we got two new files a Sally header file and a Sally CPP file and remember the
Sally header file is basically where we’re going to be making all our prototypes and
the Sally CPP is we’re going to be doing the actual coding so in our main CPP we need to
do a couple housekeeping things first go ahead and copy these and of course it’s your iostream
and name space and go ahead and copy it right in here and of course we need the Sally header
I forgot I always try to right-click and select copy but there is no copy so anyways go ahead
and get our Sally header and paste it in our main dot CPP so basically the beginning of
main.cpp and Sally CPP should look pretty much the same so now a couple things I like
to do actually the only thing I like to do is this go ahead and take out this comment
because useless comments annoy me and I guess I might as well go over this one more time
in FDF and define and end if all of this means the stuff in green right here they put that
there in every class they automatically build for you and it pretty much means don’t include
a header file if you don’t need it this literally means if not to find Sally header file then
define it so basically whenever you’re running in main.cpp all these classes are going to
have it in it’s like if you already included Sally header file then don’t include it again
because you only need to include it once obviously so that’s what all that means so now all my
explaining we can finally get to the torial so in this tutorial the first thing we need
to be able to do is I’m just going to create a random stupid function in the Sally class
so in order to do that let’s go ahead and it’s not going to return anything so I’ll
just put void and then write your class name Sally and then we need that double colon operator
and then print scrap I’d already have my my favorite function stored forming so basically
we need this double colon operator again this is a reminder because without it it wouldn’t
know what function print crap blonk do so we’re saying alright this belongs to the Sally
class and you know if we didn’t have that then we would get a so write your class name
double colon and then your function name whenever you’re building functions so print crap and
all we’re going to have this do is see out um very something see like did someone say
steak and n line so basically we have the Sally class and we have our constructor function
which doesn’t do anything and we have this function called print crab that pretty much
prints crap out on the screen so now we can go ahead and prototype it and remember whenever
you’re prototyping in class go ahead and copy that and actually what we need to do is this
don’t even copy that just under your public right boid print crap and that’s all we have
to do to prototype it we don’t have to copy it because this Sally is basically telling
this header file it belongs to the Sally class but whenever we’re prototyping the class it
already knows that of course we’re working with the Sally class so that’s why we don’t
write something like this Sally like that so you know just so a nice little note to
keep track of so now that we have basically a class made with one function in it I want
to talk to you guys about what I’m going to teach you in this tutorial and that is the
arrow member selection operator and that’s this little dinging right there saying what
the heck is that what’s it do what’s the point of it well in order to explain you guys what
that does let’s go ahead and first build Sally objects so Sally make a Sally object called
Sally object and we know with our Sally object then we can access the stuff inside the Sally
class using Sally object using the dot and then go ahead and you know use what every
one use a variable or function or whatever so now when we go ahead and build and run
this it says did someone say stake pretty cool huh so we know that these facts are true
in order to access stuff from the Sally class we need to use a Sally object and then we
can use that by putting the object first using a dot separator and then writing whatever
we want to access after it so that is how we access stuff inside the Sally class but
now I want to teach you guys that there’s a different way to access the stuff inside
the Sally class this is by making a pointer to the Sally object so let’s go ahead and
see what I’m talking about in order to make a pointer you go ahead and write what type
of data you’re going to hold I’m going to be holding Sally type of data because a class
is actually a type of data that you create yourself and now you go ahead and name your
pointer using pointer and I’m just going to name mine Sally Poynter and now we set it
equal to the memory address of Sally object so basically the Sally pointer is pointing
to this object right here simple enough so they’re basically the same thing I mean I
shouldn’t even say that because a lot of people are grammatical pointers and so I’m putting
this little drug but you know they’re basically the same thing so now what we can do is we
can use that pointer to access the stuff in the print crap class I’m going excuse me the
Sally class so let’s go ahead and put Sally pointer and you might be thinking all right
so use the dot separator and then write print crap just like that and I’ll try to run this
it’s going to work fine but why am I getting error message right here and that brings me
to the point of this tutorial whenever you using a pointer to access stuff inside a class
you need to use something called the arrow member selection operator so again whenever
you run this it’s going to work fine and dandy so this is accessing it with the object and
this is accessing it with a pointer pretty cool huh so it’s basically two different ways
where you can access things inside a class arm right now I just want to show you guys
how to do it and then later on I’m going to show you guys when you would need to do it
but not in this tutorial for this tutorial I just want to make sure you guys understand
this information you have two different ways of accessing things inside of a class one
way is to use an object and then you know the function name and another way is to use
a pointer and then the function name or variable name whenever you use an object you use the
dot separator whenever you use a pointer you use the arrow and that’s actually pretty easy
to remember because whenever you’re using a pointer it kind of looks like a pointer
so I mean it’s not can’t get easier to remember than that so that’s all I want to teach you
guys for this tutorial hopefully you understand the concept behind it you probably don’t understand
why you would use one versus the other but you will later on so for now just understand
this and don’t forget to make sure that your beginning of all your dot CPP files are basically
the same so that’s all I have for you guys for this tutorial thank you guys for watching
them to subscribe and I guess I’ll see you guys in the next video
what’s going on guys welcome to your 43rd c++ tutorial and in this tutorial I’m finally
going to be explaining to you guys those things called deconstructors you know whenever we
created class and said automatically create a Deconstructor or whatever and we had it
unchecked well I’m going to explain you guys what they are when they’re used and then you
know when we’re creating in class from now on we can understand what they mean so the
first thing I want to say is a Deconstructor is basically code that runs automatically
upon the destruction of your object and that’s the technical term for it but basically at
the end of your program one all your memories getting freed up in your programs ending and
all your objects are getting destroyed and you want a less a little bit of code to run
that’s when your D constructor is going to run so you know as soon as you create an object
the constructor runs well as soon as it’s deleted the Deconstructor runs so that’s basically
you know when that happens so let’s go ahead and first before I even start talking about
D constructor let’s go ahead and type some code into our constructor like um C out I
am the constructor and make sure you spell it wrong and then in that line so now we can
see if we go ahead and main dot CPP and create come on keyboard type regular and create a
Sally objects so Sally um s o and then after this we really firetruck are you really gonna
run right behind me in the middle of this tutorial no that’s not annoying well that
was kind of annoying but anyways continuing on so we went ahead and we created a sally
object now as soon as we create this object our constructor is going to run and all our
constructor does is shout out I am the constructor on the screen so now we go ahead and type
another line of code like C out armed oMG what DF is this on my shoe or something stupid
like that and in that line so let’s go ahead and run our program so far so our object was
created and it shout it out I am the constructor automatically we didn’t have to call a specific
function it just did on automatically and then our program ran that said oMG WTF is
this on my shoe so now let’s go ahead and add a D constructor now to create a Deconstructor
it’s as simple as this the name of your D constructor is the tilde and that’s above
the tab on your keyboard but under the escape ill well that’s where it is on mine if you
look the left of the one umm you see that little squiggly sign it’s called the tilde
I think at least that’s what I call it so go ahead and type that and then type your
class name which is Sally so basically your Deconstructor looks the exact same as your
constructor except it has that little squiggly tilde above it so now let’s go ahead and create
the body of it and again it’s the exact same as your constructor except it has that little
squiggly tilde right before it so another thing I want to point out is this whenever
you’re creating a destructor it has no parameters no matter what you can never give it parameters
also it never has a return value you can’t even give it void and aside from that there’s
no Deconstructor overloading you have one D constructor and that’s it you can’t have
different versions of your D constructor so basically it’s a point another fire truck
are you serious this is gonna run right past me you guys hear that yeah I’m just continuing
on with my story line is and I really don’t feel like redoing it so sorry guys so basically
whenever your object is first created your constructor is going to run and whenever your
object is automatically deleted or you know um destroyed I mean that’s the technical term
for it your Deconstructor is going to run so let’s go ahead and type some code and see
out I am the D constructor and make sure you type that very wrong and then go ahead and
end that line and let’s go ahead and take a look at our main and I’ll explain you guys
what’s going on our first line of code we created a Sally object so right after it right
around here our constructor is going to get called in our constructor shouts out I am
the constructor so then after this it goes ahead and runs the program OMG WTF is this
on my shoe so the next line of code is going to be did is going to be this excuse me now
after this line of code is running it’s going to see that it got to the end of the program
whenever it gets to the end of your program C++ automatically destroys all your objects
so by default automatically is going to call your D constructor for this object right here
so let’s go ahead and build and run this and see what happens I am the constructor OMG
WTF is this on my shoe and I am the D constructor so as you can see it automatically called
the D constructor and constructor for us so another thing I want to point out is when
your program is finished running that’s when your D constructor is going to get called
so again your constructor in your D constructor you don’t have to explicitly column like you
would another function inside your class they called automatically and again remember whenever
you’re creating your constructor it’s basically a duplicate the constructor except you need
a little tilde right there and unlike a constructor you can’t overload it it can’t have parameters
it can’t have a return value and make sure you don’t even put void is there a return
value it’s basically a dumbed-down very simple function so that’s the very basics of a constructor
and a D constructor and later on I’m going to be showing you guys why D constructor is
useful other than just printing out stuff on the screen but for now just understand
the concept and I’m going to go ahead and help these firetrucks put out a fire so hopefully
in the next tutorial they will stop running past me but for now thank you guys for watching
don’t forget subscribe and I’ll see you guys in the next video
what’s going on guys welcome back to your 44 C++ tutorial and in this tutorial I’m going
to be teaching you guys about a couple things specifically about the keyword Const which
stands for a constant now whenever you use the keyword Const it means that whatever you
use it on you typically use on variables and stuff it means that that variable remains
constant throughout your program aka it can’t be changed or modified in any way so let’s
go ahead and take a quick look at that say you have an integer variable X and you set
equal to 3 whenever you first make it and then later on you want to change it to something
like 5 while your program is just going to run fine and whenever you try to print out
X or use it in any other way it’s going to run perfectly fine as expected so what the
constant keyword does in that constant keyword Co n St whenever you’re creating something
you put the constant keyword in there and it’s pretty much telling your compiler all
right now whenever I create this variable I’m going to set equal to value and I don’t
want you to change it for the rest of the program so now whenever you change try to
change to something else like 5 in try to run this program is going to get error because
it’s saying hey Bucky you called this variable constant you promised me we’re going to change
it and now you lied me right there so that’s why I’m giving you an error so that’s basically
what the constant keyword does and make sure that whatever is constant is unable to be
modified so that being said later on in your programming you’re going to learn that some
objects you create you don’t want them to be able to be modified some objects are just
regular objects and other objects you want constant you don’t want them to be able to
change so you use this constant keyword whenever you create an object so therefore you have
an object that can’t be modified and you might be thinking RA so I can just use it with normal
functions and not exactly whenever you have a constant object you can’t call regular functions
with them you can only call functions that they themselves are constant so constant objects
need constant functions and of course whenever you create a constant function you need to
make sure it’s constant both in the prototype in the header file and in the definition aka
the C++ source file so let’s go ahead and just create a regular function first and then
I’m going to be running it aside a constant object I’m going to be doing a couple things
so let’s go ahead and make um a sally function and we’ll just name it like print shiz and
this is just a regular function I’m not doing anything weird yet so see out I’m just like
I am a regular function and then end that line and then of course since we created a
function we need to prototype it so then it is void I think I named it print shiz just
like that so now we can see that we can just go ahead into our main program create a Sally
object and then we can use that cell object to call our regular functions such as print
shows just like that and whenever we run it everything is going to go smoothly it’s going
to print out I’m a regular function so I did that and we already know that so let’s go
ahead and jump into constant objects now whenever you create a constant object you do so doing
this throw your keyword Co n st before you create your object and everything else is
basically exactly the same you type class you want it to be from and also what your
name your object like Const object looks like a pretty good name search and IRA all I need
to do is Const object and then I can go ahead and call it my other methods like print shiz
well whenever we try and do this we get an error message because it’s saying alright
you created an object that it was constant and you try to use a function that wasn’t
constant see this function isn’t constant is just a regular function so we need to build
special constant functions whenever we create a constant object so you’re saying alright
let’s go ahead and do that then so let’s go ahead and create another method on a constant
excuse me another function talking in Java another computer program language but let’s
go ahead and create another special constant function so void Sally let’s just name this
print shiz – and in order to create a constant function you go ahead and you type the keyword
constant right after your function definition so Co n s T so now what happens is right after
this we build the body for it so this is just going to see out umm I am the constant function
just like that and of course we’ll end that line right there and now like I said before
we need to prototype it is a constant two so we need to basically tell every single
file that we’re going to be working with constant objects and in order to prototype it here’s
what we do go ahead and just like before type avoid print chiz yeah that was called printers
– and you just write Const right there and now you’re telling your compiler alright this
is a constant object prototype so a lot of people might think you do this add the Const
before make sure you add it after the parameters and you should be good to go so now whenever
we try and run this constant function with the constant object remember this is our constant
object and this is our constant function print is – now whenever we try and run it it should
be good to go I am a regular function I am the constant function pretty simple so so
as you can see whenever we build objects if we build a regular object we can use the regular
functions with them whenever we build a constant object we’re telling our C++ compiler all
right this object we don’t want anything to be able to modify it or change it so it told
us all right I can do that but you need to build me some constant functions if you do
that for me I’ll run your program just fine so that was our little deal we had with the
compiler and that’s why this one ran with the right wave function and this constant
object ran with the constant functions so now you should understand how to prototype
constant functions how to create them and also why constant objects need constant functions
and again later on you’re going to be seeing why in like bigger programs um you need to
be able to create some objects that you want to be able to make constant and why they were
useful for your compiler so for now I just want to teach you guys the very basic so in
the next story we’re probably going to be covering constants a little bit more because
there’s actually a lot to go over with constants and classes so for now that’s all you get
thank you guys for watching don’t forget subscribe and I’ll see you guys in the next video
what’s going on guys welcome to your 45th C++ tutorial and in this story I’m going to
be talking to you guys about member initializers now I don’t even know if I told you guys this
but a member is pretty much fancy term for anything in your class so when some people
talk about variables they call them members it’s basically anything in your class is a
member of that class so let’s go ahead and let me explain this we’re going to probably
have a bunch of variables in our class some of them are just going to be regular variables
and some of them are going to be constant variables so we know constant variables have
a couple of weird rules and creating them is one of the weird rules so remember a constant
variable is basically a variable that we can’t change and we also have to initialize it using
a special it’s a special syntax it’s called member syntax now the cool thing is we can
also initialize regular variables this way but you don’t have that option of when using
constant variables you have the option of initializing regular variables this way or
the way we learned before but constant variables definitely need to be initialized using member
syntax so let’s go ahead and first thing let’s go ahead and make two private variables I’m
going to make one just regular variable because some be integer variable reg var stands for
regular variable and I’m going to make another constant variable so constant just make this
one the injur as well and we’ll name this one Const var so now we have just a regular
doubled variable and a constant variable so now what I want to do is whenever we first
create our an object let’s go ahead and assign a value to this variable and assign a value
to this variable so our constructor is going to take two parameters one value for this
one and one value for this one so go ahead and just type in a and enbe and we’ll be tossing
numbers in there later on but now that we have values assigned to these variables we’re
going to need a way to print it out on the screen so go ahead and we’ll make a function
called print simple enough so now we have a constructor that takes two values assigns
one to their regular variable one to our constant variable and then we’ll print it out on the
screen so now we probably need to change our constructor around a bit the first thing we
need to make a do is take those two parameters a and int B now you’re saying alright I probably
go ahead and in the body somewhere in here I’m going to type all that syntax that special
syntax you’ve been talking about but check it out you actually don’t write anything in
the body yeah think about that for a second it’ll freak you out you actually don’t write
any of your code when initializing variables in the body so you’re saying where the heck
do you put it and this is the weirdest place you put it in between the parameter list and
the beginning of the body so between the parentheses in the curly brace this is where you’re going
to put it so that’s what confuses the most people and that’s why they have a hard time
understanding because usually nothing goes here but that’s where we’re going to be putting
the syntax why they made it go here I don’t know and the syntax for it was this the first
thing we need is a colon and the colon tell C++ that we’re going to be working with member
initializer syntax now it’s actually quite simple all you need to do is write your variable
name such as red var and inside parentheses you give it an initial value which this one
we’re going to be passing it in a now whenever you have multiple variables you want to set
equal to you know the initial value you just separate them with a comma it’s basically
just like a list so our next variable is Const I was a constable and this one is going to
be equal to B and what people sometimes like to do is put their variables on new lines
but it really doesn’t matter and another thing make sure that you don’t put a semicolon at
the end of this these aren’t statements it’s just a list it knows when you’re done because
the body is going to begin so basically start with a colon and make a list using the syntax
variable value if you have another one separated with a comma variable value so I know it’s
really weird syntax but what this is going to do is we’re going to be creating a constructor
that takes two values and it’s going to be setting them equal to a regular variable and
constant variable and like I said before whenever you have a constant variable in your class
this is how you need to do it of course we can set this variable equal to you know initialize
it like we have before but you know we might as well use this list since we’re you know
learning this tutorial so now let’s just go ahead and make another function avoid print
just to print this stuff out on the screen so void okay there void sally print and all
this is going to do is be like see out regular ver is and then just put reg var and then
put constant variable is : Const VAR just like that and now let’s go ahead and and that
line and you guys can’t even see a you guys still can’t see there we go em line definitely
sped that wrong and that line so now I’m just going to print out our regular variable is
10 and our constant variable is 20 or something stupid like that as long as I spelled this
very let me just double check alright we should be good to go so now all we need to do is
create an object in our main so let’s go ahead and create a Sally object just name it s oh
and remember this is a constructor so it’s going to take two parameters just make like
three and eighty-seven two random numbers just make sure they’re integers and now as
soon as we created that constructor it’s going to take three and assign it to our regular
variable and it’s going to take 87 and assign it to our constant variable so now reg var
equals three cos Phi R equals 87 so now when we call that print method and let me just
I don’t like typing stuff on the first line for some reason um s Oh dot print it should
print that out unless I forgot a semicolon or something so let’s go ahead and see and
check it out regular variable is three constant variable is 87 mmm my spelling right there
that actually does spell variable well I’m close enough so anyways one last time here
is what we did we had a constant variable in our class this pesky little thing right
here so whenever we have a constant variable in our class we can’t initialize it like before
we need to initialize it in a special way and that’s through the use of member initializers
whenever you want to create a member initializer list and basically this is just a fancy term
for setting your variables equal to values you need to make a list in between your parameters
and the body of your constructor and make sure whenever you’re making that list to start
with the : that tells c++ you’re going to start making a mem list and just use this
in text and don’t forget to separate each variable with a comma and then of course whenever
you create your object you need to pass it in the two parameters that need it because
right here it said it asks for two parameters and then later on we can just use those variables
in any old function just the print amount like regular variables blank constant variable
is blank and then actually I got inside shrek you guys not have this function works actually
I want to ask you guys this now I’m thinking about it is there a way to a like word wrap
because like whenever I’m working with this you see I was going off the screen um let
me know if there’s a way to automatically word wrap so you guys well I guess I’ll just
scroll from now on I don’t like my lines of code on new lines anyway so forget that like
the last 30 seconds of this tutorial but anyways now that you guys know how member initializers
work um yeah I’m just going to quit this tutorial now so thank you guys watching don’t forget
subscribe and I’ll see you guys in the next video
alright guys welcome to your 46 C++ tutorial and this tutorial might be a little longer
than normal I might even have to split it up into two tutorials because I’m going to
be talking about a subject called composition and I’m going to need to build a few classes
so anyways composition is the idea that you know whenever you build a class it has members
aka variables and usually a class just has some regular functions in it and some regular
integer variables maybe a couple string variables well aside from just functions and regular
variables a class can also have objects of another class as its member so you can stick
objects inside classes as well so you know like whenever we created an object we always
created it in main right here well you can actually have it inside other classes so let’s
go ahead and take a look at that and again composition is what it’s called so the first
thing we need to do is create a couple new classes so file new class I know you can’t
see that but I already showed you guys before so what I’m going to be doing is I’m going
to be creating one class called birthday and that’s pretty much just going to be storing
a birthday which would be mine like twelve 2886 that’s its only job so go ahead and create
that and go ahead and press yes and okay so now we have a blank birthday class with nothing
in it except you know the template so now what I want to do is build yet another class
so file new class and I’m going to name this class people now go ahead and uncheck that
and go ahead and press create and of course yeah I want to add it to my project and go
ahead and whatever that thing said so what I’m going to be doing is every single person
who’s live has a birthday so wouldn’t it be cool if inside this people class we can store
a birthday or a date as one of its members so I’m basically going to be taking an object
from a birthday class and storing it inside the people class and then running the main
program for main so let’s go ahead and get started first so in your birthday header file
go ahead and delete protected because I didn’t teach you guys that yet but in our private
we’re going to have three private variables in month which was the month of the birthday
and and I said int in day and int year so that’s man I cannot talk I keep saying for
some reason anyways this birthday class has three variables it has a month a day in a
year and what we’re going to be doing is in can in our constructor we’re going to be passing
it in that information so let’s just go ahead and pass in M for men and D for day and int
Y for Year so we’re going to go ahead and set the birthday whenever we create the object
so now it’s going to have one more function in it and that’s void print dates and all
this is going to do is print out the birthday on the screen simple enough so now let’s go
over to birthday CPP and it already includes birthday header but we already know that we
need to include this stuff as well so go ahead and copy this and paste it right under here
so now it includes everything we need it to include so of course the first thing we do
is delete this comment because I hate comments and in our birthday constructor remember it
takes three pieces of information a month a day in the year so let’s go ahead and just
actually steal this from over here copy that and paste it right in there so now it has
three pieces of information that we passed in a month a day in the year so what we can
do is use those values the set equal to the variables we needed we have a month variable
add a variable in a year variable and all of those variables need a value so go ahead
and for month set equal to the value passed in 4m four-day set equal to the value passed
in 4d and for a year set equal to the value you passed in for y so as you can see now
whenever we pass in our three pieces of information it sets em to month B for day white a year
and this is going to be 12 equals month 28 equals day in 1986 equals year because that’s
when I was born so that’s what I’m doing so aside from that we already know that we have
to create this one extra function called print date and now this function does is print a
date out on the screen so let’s go ahead and make that right now void birthday print date
it already knows that we need it and of course it doesn’t take any parameters and all it
does is print out something on the screen so C out month day and year and well might
as well go ahead and add a end line for that so what it’s going to do is print out the
month slash day slash year slash and I know that this is formats kind of weird I think
it’s different in England and Great Britain but for those of you overseas yeah this is
how we do things here in America we also measure like links with twelve instead of ten so yeah
that makes it a whole lot easier like 12 inches in a foot who the heck decided there should
be 12 inches in a foot is that it just you know 1010 would make things a lot easier but
you know we have two stupid American system over here but anyways there’s my little rant
for the day now I can go ahead and work in my other class so after we’re done working
with birthday class and check it out we go ahead and we made everything here and everything
here we are done with it let’s hop over to our people class and the first thing that
we need to do is include a couple of things um whenever I’m making a excuse me a people
object I’m going to be giving it a name like Bucky Roberts so since we’re going to be passing
it in a name we need to be able to work with strings so include string and that will include
the string class and aside from that we also need to include this birthday header file
so go ahead and copy and paste and the reason we need to include this birthday header file
is because as one of the members aside from just regular variables we’re also going to
be storing a birthday object in this class so if we don’t have this birthday header file
it’s it would think we’re just trying to make a weird variable or something and we’ll get
a error so that’s why we need to include the birthday header file and aside from that we
need the using namespace standard to get all our standard uh where you have people had
her there you go all our standard crap so let’s go ahead and start working on the people
class so the very first thing why I should probably tell you guys is what information
is going to be in this class well as I said before everyone has a name and everyone has
a birthday so go ahead and put string name and go ahead and as a birthday we’re going
to go ahead and use a birthday object as a member of this class so whenever you want
to use an object of another class inside this class you go ahead and first type the class
name birthday and then you go ahead and give it a variable like date of birth so now this
class people H every person in this class is going to have a name and a date of birth
but we’re not just going to pass it in the date of birth we’re going to pass it in a
date of birth object pretty cool huh so aside from that um we might as well go ahead and
we’re going to be passing in the constructor so we’re just going to pass the name in is
variable X and we’re going to pass the birthday in as a birthday object so we’ll call it Bo
so whatever name we pass it in for X is going to set equal to name and whatever birthday
object we pass in a Bo it’s going to set it equal to the date of birth pretty cool huh
so now let’s hop over to people without CPP and you know I think I’m going to wait to
do this until my next tutorial because this one’s getting kind of long and I hate long
video files they always mess up if they get too long so um in the next tutorial I’m going
to show you guys how to write your people that CPP class and also how to tie everything
together in main so the program works fine and smooth so thank you guys for watching
that’s all you get for now so hopefully I’ll see you guys in next tutorial
alright guys welcome back to your 47th C++ tutorial and this tutorial is just a continuation
from the last oriole so what we did in the last tutorial is we pretty much we’re working
on the people h header file and now we’re going to hop over to the people dot cpp to
start actually building our functions but now that I think about let’s go ahead and
have hop back over to the people header file and aside from just the constructor let’s
go ahead and build another function just so we can print it out it will make everything
a lot easier to see so let’s go ahead and make a function called what was of this one
called I want to make sure I don’t name the same thing this one is called print date so
we’ll call this one print info I guess so print info so this is pretty much going to
create our object and this is going to print out the information simple enough so now that
we have this people class created in our constructor we said it was going to take two things every
person needs two pieces of information a name and a birthday so what we need to do is we
need to assign the name that they passed in and the birthday that the pass then to the
private variables name and date of birth so in order to do that what we need to use is
a member initializer list anytime you’re working with um a class inside of another class you
need to use a member initializer list so go ahead and remember don’t put it inside your
body right here you put it between your parameters in your body and again all a member initializer
list is it’s basically setting name equal to X and it’s going to set the variable date
of birth equal to Oh would we pass in Bo so Bo and also remember that you don’t put a
semicolon at the end of this it already knows it’s done because the body will start so that’s
all we need to do for this right here so again we’re going to be passing in two pieces of
information we’re going to be passing in a string and then a birthday object it’s going
to take that string and set it equal to name and it’s going to tape that birthday object
and set it equal to the day of birth so now this class has a name and now it has a birthday
object that you can use so let’s take the information and we can use that whenever we’re
building this other function print info so let’s go ahead and um yeah I guess I don’t
need to do anything before that so let’s go ahead and put void people print info and of
course this print info doesn’t take any information but it’s just going to print something out
on the screen so what it’s going to do is see out your name variable which would be
Bucky Roberts and then go ahead and write something like was born on and then make sure
you don’t end the line because I want all this to be on one line and then after this
in order to print that date remember we have a function inside our birthday class that
prints the date right here and we already passed in a birthday object inside this class
so now this class has access to the birthday functions so we can go ahead and use this
date of birth object right here that we created in order to use this print date function so
let’s go ahead and just write date of birth right there dot print date pretty cool huh
so now whenever we print the info it’s basically going to say Bucky Roberts excuse me Bucky
Roberts was born on twelve 2886 pretty cool huh so now let’s go over oh wait a minute
I almost forgot to do this we need to include a bunch of stuff inside this people class
um I’ll talk you guys through it the first thing we need to include is our iostream and
STD because you know we need to include those basically in every class and aside from that
we also need to include this birthday header right here and the reason we need to do that
is because whenever basically whenever you’re working with another class you need to include
the header so yeah I definitely just messed that up accidentally highlighted something
so one more time okay let’s not copy and paste it or in spots this time all right so let’s
go ahead and in our main CPP um everything looks good go so far actually what we need
to do is since we’re going to be working with birthday objects and people objects in this
one we need to include include those headers as well so go ahead and grab this and include
it right there and go ahead and grab this and include it right there so basically you
see whenever you’re working with the object you need to include it simple enough so check
this out since the people class in order to create an object you need a birthday object
whenever we run our main we need to create that birthday object first so remember in
our birthday object when we first created our object needs three pieces of information
a month a day in the year so let’s go ahead and create a birthday object if I spell write
birthday and we’ll just name it birth object and then these those three pieces of information
a month a day and a year so now we have a birthday object created called birth object
and this birthday object pretty much took month day and year and set equal to twelve
2886 so now we can go ahead and create a people object people I’m just going to name mine
Bucky Roberts and you’re saying all right well what information does the people class
need well whenever we create a people object it needs a string as the name and it needs
a birthday object right there so first let’s give it a name Bucky the King and the second
piece of information it needed was a birthday object well it just so happens that we created
one up here so let’s go ahead and pass that in right there so after this what we need
to do is might as well just print out the info right here so remember in the people
class we had a function called print info and it just printed out the name and the birthday
so go ahead and type your object named Bucky Roberts dot print info and remember it doesn’t
take any parameters and let’s run this and see how many errors I got one so hold on once
and I see what’s going on all right see this up whenever we create this constructor we
forgot to pass it in the parameters so just go ahead and copy these parameters copy and
paste so hopefully that’s the only errors we got and it looks good go so Bucky the King
was born on 12 28 1986 works awesome so let me talk you guys through one last time what
we did we just created a basic birthday class and this birthday class had three variables
a month a day in a year so whenever we created a birthday object it took that month day in
a year and assigned it to the three birthday variables month day in a year so we got the
birthday class taken care of and then we decided to build another people class now the people
class whenever you built a person and need the two pieces of information and needed a
string which we gave it Bucky the King and it needed a birthday object so when for example
whenever we had a birthday object right here we could pass it in second and what it did
is in the main program it took that name and set it equal to the variable name and it took
the birthday object and set it equal to the object date of birth so that basically gave
us access to the birthday class so from there we were able to use data birth and the function
in the birthday class to print out the birthday so basically whenever I run this this is a
function from the people class and this is actually a function from the birthday class
we just tied them together in one function right here pretty cool so that is basically
what our composition is compositions basically using other classes inside your class using
objects of other classes inside your class um that’s all I’m going to go over for composition
now hopefully you guys understand if you don’t definitely go check out my forum and just
type in whatever question you have and a ton of people are willing to help you but for
now that’s all I have for you guys this is a pretty long tutorial so go take a break
go grab a milkshake or some and when you’re done you’re ready to move on to the next tutorial
so thank you guys for watching don’t forget to subscribe and I’ll see you guys in the
next video welcome back I steer 48th C++ tutorial and
in this tutorial I’m going to be going over the keyword friend so basically every class
can have friends and a friend of the class is something that’s totally separate from
the class but it still has access to the stuff inside your class like your variables and
your functions so in other words if a class wanted to have a function it can make that
function its friend and now that function had access to the variables and the functions
inside the class pretty cool huh so let’s go ahead and take a look at it so the first
thing we need is a class and I’m going to name this class stank fist so let’s go ahead
and make our class right now the first thing I like to do is get everything set up so public
and private so this class is going to have one private variable and int stinkie variable
so now the only thing that has access to this thinkI variable is whatever is already inside
this class so first of all let’s go ahead and build a constructor for this stank fist
and all this constructor is going to do is take that stinky variable and set it equal
to zero so now as soon as you create a stink fist object and make sure I capitalized that
consistency on my fist it’s going to take this stinky variable and set it equal to zero
simple enough so now what we need to do is we need to build a function so let’s go ahead
and build a function outside actually let’s go ahead and make it right here so in order
to make a function your friend the very first thing you need to do is prototype it inside
your class so go ahead and put void one name this function stinkies friend and we’ll go
ahead and as a parameter it’s going to take a stink fist object so stank fist and we’ll
just name this object s fo4 stink fist object so as a parameter it’s going to take an object
of this class so we’re saying all right so that’s all you do in order to make it your
friend prototype inside your class one know in order to make this function a friend of
this class before the prototype you need to type the keyword friend now this function
has access to everything inside this class because it’s its friend so now when we go
ahead and copy this and paste it and create it we can use the stinkface object to access
all the stuff inside the stink fist class pretty cool so first of all let’s go ahead
and change this stinky variable equal to something so using our stink fist object let’s go ahead
and access the stinky variable and set equal to 99 or something different than zero and
now let’s go ahead and just print it out on the screen see out SFO stinky variable and
lime so basically what this does is even though there is a function right here you can see
that it’s totally different totally outside this class but since this class said you know
what I trust this function it’s my friend now this function has access to the stuff
inside the class such as stinky variable and another thing I want to point out before I
go on a class has to give away its friendship for example this function and this function
can’t demand to be friends with this class that’s why you don’t have the friend keyword
right before it this class needs to give away its friendship other places just can’t take
it it’s kind of like real life you can’t make someone be your friend they have to accept
you into being your friend make sense trust me I tried it didn’t work out so well so let’s
go ahead and now that we got everything set up we can begin using this in main so our
main what we need to do first is create a stink fist object so stink fist and I’m just
going to name my object Bob and now we can use that Stinky’s friend function right here
and of course this function takes a stink fist object as its parameter so let’s go ahead
and as this parameter type in Bob and now we’ll go ahead and run it and check it out
as long as I didn’t forget anything it should print out 99 pretty cool home so even though
we were using this function and this function was outside this class it was able to access
the stinky variable and print it out on the screen pretty cool huh so basically to recap
one last time we wanted to build a function that can access this class but we didn’t want
to put it inside the class itself so instead what we decided to do was make this function
a friend of the class now whenever this function tried to use the variables and functions inside
of this class it was able to because this class said you know what you’re pretty cool
you can be my friend so then we made a function that took a stank fist parameter as its object
so the first thing we need to do is create a stink fist object and we pass it in as its
parameter and then ahead access the distinct fist class so that is basically the UM I don’t
know the lesson behind this tutorial whenever a class wants something to be its friend it
can access the members of that class and aside from that a class has to give away its friendship
other functions can’t just demand to be friends with it it has to give it to them so there
you go that’s what friend means and that’s why it’s useful again this is a real simple
example but later on I’ll show you guys how it’s really useful so just stick around and
you’ll see so thank you guys for watching don’t forget subscribe and I’ll see you guys
in the next tutorial what is going on guys it’s bucky and welcome
back to your 49th C++ tutorial and in this tutorial I’m going to be talking to you guys
about the this keyword but before I get into that let me tell you guys I’m sorry it’s been
a couple days since I made a tutorial but I had to upload 200 PHP videos to YouTube
ND guys have any idea how long it takes to upload 200 videos to YouTube quite a while
so anyways that’s my excuse so now we can get back into C++ so let’s go ahead and for
this example I’m going to be making a new class because if I put it all in the same
file it’s going to get a little clustered so I’m going to name my class Hannah and actually
I already messed up yes okay I actually made a Deconstructor and I didn’t want to so let’s
go ahead and just delete that right there and now we can get coding our project so let’s
go ahead and first let’s do a couple housekeeping things take this hand eh and paste it in here
and now I’m going to take this and paste in here so basically your dot cpp files see this
one and this one should basically look the same at the very beginning and what i also
like to do is get rid of this protected crap right there because we didn’t learn that yet
so you know no use using it so basically what this hannah class is going to do is it’s going
to have a private variable I’ll just name that int H now whenever we create an object
we’re going to pass in a value and integer value and all that’s going to do is you know
say we pass in 20 it’s going to set H equal to 20 so that’s all we’re doing in the constructor
and then we have another method to print out the H variable on the screen so we’ll name
that print crap because that’s my favorite name for a function if you couldn’t tell already
so basically what we’re doing is we have a variable called H and we’re going to print
it out on the screen and you’re saying alright then what the heck’s the point of the story
well I’m going to be printing out this variable in three different types of ways the first
way is normal like we talked about before and the two different ways are using the this
keyword now we don’t know what those mean yet so that’s why I need to watch this tutorial
so now that I got you hooked and anxious let’s go ahead and start coding this constructor
so again as I talked about we are going to pass it in a variable and X oh I mean I’m
we’re going to be passing in a value there we go I got a little tongue-twisted and we’re
going to set the variable H equal to that value so you say we pass it in 66 it’s going
to take H and set equal to 66 simple enough so now let’s go ahead and start coding our
print crap method right here so let’s go ahead and make void an a print crap and it’s not
going to take any parameters like I said it’s just going to print some crap out on the screen
and let’s go ahead and make a template first and in line so this is what I do whenever
I code by myself I probably shouldn’t do this whenever I’m making tutorials but I basically
make a shell of whatever I can and then fill in the blanks so the first thing we were going
to want to print out is what h equals and it’s just going to be you know H so say we
passed in so say H is equal to 99 it’s just going to print out H equals 99 on the screen
so now let’s get in the good stuff the meat of this tutorial and that’s the this keyword
now I’m actually going to be doing this story a little bit different than before in order
to teach you guys the full concept I’m actually going to make a my entire program first and
then walk you guys through I usually you know um walk you guys through what I’m doing as
I’m typing but it’s going to be easier to understand if I actually make my whole program
first so this H equals this H this is going to pronounce me you guys will know by the
end of this tutorial and actually this weird kind of H is also gone let me add my equal
signs equal signs I know you guys are anxious to see what this all is but I’m not going
to tell you guys yet so now we’re basically printing out H in three different kinds of
ways this way this way and this way get it this way it’s a cheap little joke but anyways
let’s go ahead and now we have to create an object to actually call those functions so
Hana H oh and let’s just pass it in like 23 or something like that so first what this
is going to do is it’s going to take the value of 23 and set it equal to the variable H so
when everyone’s program is going to print out 23 23 23 so now after that we of course
need to call print crap right like that so let’s go ahead and build and run this and
see if I got any errors which I did because I forgot a semicolon let’s run it again and
check it out H equals 23 make sense we already knew that now this arrow H equals 23 all right
oh we’re not exactly sure what that means and whenever we use parentheses star this
that H that equals 23 2 so basically we can see that these are three different ways of
printing out the same variable so we already know that whenever you print out on a private
variable we can just use that private variable name and it prints out the value so we already
know what that one does from the you know the last 80 tutorials so let’s go ahead and
skip right on down to this one and that’s this arrow H well remember in tutorial like
42 or something I told you guys whenever you use the arrow it means you’re going to be
working with a pointer so that’s kind of weird so that means this is a pointer well yes it
does in a C++ program whenever you use the word this it’s actually a keyword that identifies
a special type of pointer the key word this stores the address of the current object that
you’re working with it sounds kind of weird huh so that’s why action needed to build this
entire program first because I needed to build an object and the object we’re currently working
with is H Oh so whenever we use that object H oh it took the address of it and by the
way H o is stored somewhere on our computer and memory deep down you know some weird location
but it took that location and it stored it in the key word this so now that this key
word identifies a special type of pointer like I said that stores the address of H oh
if we made another object called Bo it would then hold the address of Bo so whatever object
you’re working with it’s going to take that address and store it in a pointer called this
so basically it’s saying the address of H Oh use that object in that object has a variable
H so that’s why this and this are basically the same thing and you’re saying all right
so if they’re the same thing why does this work and why does this work this whenever
you print out H it’s just implied that whenever you’re working whenever you’re here let me
say it like this whenever you print out this variable right here c++ assumes that you’re
working with a curved object whenever you print out like this way using Heuer this it’s
basically explicitly telling c++ so this is usually you know just assuming that you’re
working with the correct but this is explicitly telling c++ you’re working with this current
object it just you know a different way to do things but actually this keyword is pretty
important I’m going to show you guys why it’s actually useful aside from just printing out
variables so basically what I want to stress you guys in this soil is whenever you use
the keyword this it stores it as a special type of pointer and that pointer stores the
address of the current object in this case was H oh so this is saying the address of
H Oh get that object and that object has a variable H and H is equal to what do we set
equal to 23 so that’s why this prints out 23 now that I confuse these guys let’s move
on to this one down here now this is basically a different way to access the object the this
with a weird pointer and then surrounded by parentheses this is called dereferencing a
pointer and what this means is it’s literally taking the memory location of the current
object which is you know some long hexadecimal x 0 j 42 yada yada and it’s taking that memory
location of the current object and calling the variables value right there so this is
basically saying alright whatever current object we’re working with what is h0 that
h0 has a variable called h now print out the value of h in this you know just assumes we’re
working with h oh right here we’re saying we’re going to be using this keyword to talk
about h oh and right here we’re using the explicit memory location of the object h oh
so this is basically implied explicit explicit just two different weird ways of doing it
so now that i taught you guys that in the next store i’m going to show you guys why
you would use the this keyword and you know it’s not just something that you can use the
show off that you know more about sleep plus plus it actually is useful in some programs
so maybe the next tutorial will be a little bit more useful but for now that’s all I got
for you guys so hopefully I can fuse the eyes if you need some more information check out
my website the new Boston comm and you can ask me a question there but for now thank
you guys for watching and I’ll see you guys in next door
alright guys welcome back to your 50th C++ tutorial and I know in the last tutorial I
told you guys I was going to be building a program using the this keyword but I was building
it and I noticed that there was a lot of concepts I didn’t teach you guys so we’re going to
have to come back that after I taught you guys a couple new concepts so we’ll get back
to that but for now I want to talk to you guys about something called operator overloading
now operator overloading is a weird feature with C++ well operator overloading is is it
allows you to use C++ operators like you know plus and multiply and it allows you to take
these operators and change the way that they’re used so for example maybe you have a class
and you want to add two objects together well how the heck does C++ know how to add your
two custom objects together well that way you can use operator overloading to specify
what you want this operator to do so what I did already is I made a new class called
Sally and I also include the Sally header in here and I included an IO Sriman namespace
in here so now my dot CPP files look the same just to save this tutorial some time so now
let’s go ahead and get into the coding so let’s go ahead in the sadi sally header file
and notice that i took out both private and protected because we’re just going to make
everything in public it’s going to make everything a whole lot easier so the Sally class is going
to have a couple things in it the first thing it’s going to have is a variable called num
and this is the only variable it’s going to have um it just needs a variable pretend this
is like Sally’s age or something I just named it num because it’s easier to work with now
let’s go ahead and create two constructors the first constructor is pretty much just
going to be empty constructor we’re going to be building a sally object and whenever
we don’t pass any parameters to it it’s just going to create a blank sally object and we’re
also going to be creating another constructor and whenever we create a Sally object and
we pass an integer into it what I want to do is well let me go ahead I guess can code
this right now whenever we create a Sally object and we pass an integer into it called
like integer a or something what this is going to do is I like it right up there what this
is going to do is take that number T and set it equal to a so we’re going to create Sally
objects in two ways a blank Sally object or a Sally object that already has a number simple
enough so now let’s get to the last thing in the Sally class and that is you know what
I’ve been talking to you guys about this thing called operator overloading now again like
I said operator overloading allows you to use C++ operators in a special way inside
your classes so in order to use an overloaded operator you need to make a function and you
need to give it a special name so whenever you build a function in C++ you need to first
give it a return type now this function is actually going to return a sally object so
go ahead and type a sally right there and now you have to name your function and you
have to name it this whenever you’re using an overloaded operator you have to start it
with the named operator and then right after it you write what operator do you want to
overload and this can be multiplied divided but we’re going to be overloading the addition
operator and what this is going to do is take two of these number properties and add them
together so let’s go ahead and write operator plus this is the name of our function now
and this is also going to take a parameter what this function is going to do is it’s
going to take one Sally objects number and add another Sally objects number to it so
we’re going to need to pass it in another Sally object to add to it so let’s go ahead
and call it like um Sally well all we have to put is that Sally so now let’s go ahead
and build the function itself so let’s go hop over to Sally dot CPP and let me make
sure it I forget any semicolon looks good so now let’s go ahead over to Sally about
CPP and start building this function again the very first thing you write is their return
type this is like void and well we’re returning a Sally object so go ahead and write Sally
right there and now we have to go ahead and write what class are we working with Sally
what’s the name of our function and remember that’s operator plus since we’re overloading
the plus sign and what parameter does it takes well it takes a Sally parameter and just name
this variable like ASO for another Sally object so now what we want to do is this we’re going
to be returning a brand-new Sally object so let’s go ahead and make a new Sally object
right now Sally and name this object brand-new simple enough so now let’s go ahead and add
the number property to this brand-new object because we created a brand new object but
it doesn’t have anything for its number property right here so let’s go ahead and put brand-new
dot num and what do we want to set this equal to well what we want to set equal to is the
current objects number value plus another Sally objects number value
plus another sali objects number value so basically we’re going to have to Sally objects
already we’re going to have a Sally object that say has a number of 20 and another Sally
object that has a number of 30 and whenever we create the brand new number it’s going
to have a value of 50 so that’s where we’re going to be returning the brand-new Sally
object so in order to do that all you do is this return brand-new just like that and what
this is going to is return the brand-new Sally object so basically we’re going to be calling
this on a Sally object that already exists and what it’s going to do is this line is
going to create a brand new Sally object right here we give a number value to the brand new
Sally object and right here we’re just returning that brand new Sally object simple enough
so let’s go ahead and well I guess now we can hop over to main and I can actually you
know show you guys what’s going on so the first thing we need is two already existing
Sally objects with the number property that we can add together so Sally well we call
this object a and we’ll give it a value of like 34 now we need another one Sally B and
we’ll give this value 21 now we need a blank one because whenever we call this value remember
it doesn’t have a number property yet so that’s what we’re going to be giving it so let’s
go ahead and create a blank Sally object with a C and now all we need to do is this you
might be thinking are a whenever I wanted to before like give a number property to the
C value I would do something like C equals a dot add and then you would add B to that
and I mean what that would do is whenever you wanted to add a number property to see
it would take a and add B get some integer and add it to the property of C right there
well with this overloaded operator you don’t have to do that anymore here’s we need to
do C equals a plus B in that way whenever we use the plus sign C++ already knows that
we have a special meaning for it right here so whenever it sees the plus sign call it
on one of these objects it’s going to know to run this function right here so let’s go
ahead and run this in now that we use that plus sign the C object has a number operty
so let’s go ahead and see out um c dot num and then let’s go ahead and end that line
and let me go ahead and run this real quick and I’ll show you guys that it says 55 so
what it did is basically this it took this number value 34 added this number value 21
it gave us 55 and through the use of our overloaded operator that’s where we got the Sally num
so let me talk you guys through one last time what’s going on maybe this will clear things
up for you in our very first line of code we created a Sally object and then um had
a value of 34 so we know whenever we created a Sally object and we passed in an integer
then assign that integer to the property of num so we have a Sally object in a Sally object
is num 34b Sally object is num 21 and then we created a see Sally object and remember
whenever we created a Sally object that we didn’t pass in a parameter it just created
a blank Sally object right here so we said all right with our blank Sally object we want
to use a plus B so set all right I know this plus since you’re trying to do something weird
I know that you’re trying to use this overloaded operator so it said RAC goes a plus B that
means you’re trying to use the Sallee object with this function right now so what it did
is it took the current object which was a right there and this B was treated as the
parameter another Sallee object so what it did it was it took the current object’s number
which was 34 it added another Sallee objects number which was 21 and it returned a brand
new sally object with 55 is as its number so as its returned value we said equal to
C so C now equals a brand new sally object with 55 as its number so that way whenever
we printed out our brand new sally object with its new number that’s why you printed
out 55 on the screen right there so again like I said operator overloading is a little
weird because it basically takes your plus sign and you know you don’t have to use a
function name anymore so instead of this it pretty much changes that all into a plus sign
so I know it’s kind of a weird syntax at first and to be honest I don’t use it a whole lot
in my programming so if that gives you confidence that you’re probably not going to be seeing
this a whole lot then you know you can relax but I just want to give you guys the idea
of what operator operator overloading is because you know whenever you’re at a job and people
are talking about it you don’t get lost or whatever so that’s what it is but you won’t
be seeing it all too much in my programs so now that you guys understand the concept of
it thank you guys for watching don’t forget subscribing you’re probably going to have
to re-watch this video a couple times and do some examples of your own and if you have
any questions don’t forget to check out my form the New Boston comm slash for them and
hopefully I can answer for you so anyways one last time thank you guys for watching
don’t forget to subscribe and I’ll see you guys later
alright guys welcome to your 52nd C++ tutorial and in this tutorial I’m going to be talking
about something called inheritance now inheritance is a topic that confuses a lot of people but
I’m going to be giving you guys a really simple explanation and showing you guys a really
simple example of what inheritance is now inheritance is the idea that you can inherit
things from another class for example say you have a class that has a couple functions
in it and a couple variables in it and you wanted to make a new class that also had the
same functions and variables in it well instead of you know copying them all and pasting them
all into your new class you can just inherit from the other class and whenever you inherit
from another class you get all the stuff you get all the functions and variables so let’s
go ahead and make two classes file new class I’m going to make one class named mother and
this class is going to have a couple things in it well it’s going to have a function in
it for the sake of this tutorial so let’s go ahead and create this add it to project
and I’ll press ok and now I’m going to create a new class called a daughter now what the
daughter is going to do is inherit this stuff from the mother so go ahead and press create
yes and okay so the first thing we need to do is go to our dot CPP copy this paste inner
main and also go to our daughter copy this paste in our main and then copy all this and
paste it and paste it so now all our dot CPP s look the same that’s why I like doing all
my tutorials so let’s go ahead and our mother header and actually in both our header files
take out protected and private because um I didn’t tell you guys what those mean right
now so let’s forget about it for now they’re actually pretty important when we’re talking
about inheritance but like I said in this story we’ll just want to give you guys a really
simple example so before I go on let me give you guys the technical terms see we’re going
to be inheriting all the stuff for a mother and we’re going to be putting it in the daughter
class but now the technical terms if you’re on a test or if you’re working for a job or
something whenever you inherit from a class this main class that you’re inheriting from
is called the base class so in this example mother is going to be the base class now the
stuff that does the inheriting the daughter that gets the stuff this is called the derived
class so the base class is the class with all the stuff in it and the class that is
going to get all the stuff is called the derived class so let’s go on and start typing our
example so let’s go ahead and throw one function into the mother class and we’ll just you know
void say name so this is the Robert’s family we’re dealing with so let’s go ahead and build
this function right here right here all right we don’t have anything in the constructor
because the constructor is annoying now let’s go ahead and build this void mother say name
function and all this function is going to do is see out I am a Roberts because that’s
what my mom shouts all the time and she doesn’t know how to spell so that’s why I have to
add some typos in there so now if we go ahead in our main CPP we can build an object called
like mother named it mom and what mom can do is say name and of course it would say
I am Roberts on the screen and say you wanted to build another object a daughter object
and we’ll name this Tina and you wanted Tina to have that same function because I mean
she’s Roberts – she should be able to say it well right now whenever we build it it
doesn’t work because Tina doesn’t have that function only the mom does so saying RA I
know in this case go ahead and copy this put in the daughter header then copy all this
crap put it in the daughter CPP and it should work fine well yes you can do that and it’ll
work fine but I got an easier way to do things and that is through inheritance now whenever
you want to use inheritance all you have to do is this in the header file whatever class
you want to inherit the stuff from right after the class name put a colon and then right
after this write the word public and then write the class you want to inherit from which
is mother now we’re going to be talking about this keyword why I put public right there
basically it means um all this public stuff is going to be public in here but we’ll be
talking about later there’s a couple of weird things but you know just put public for now
so basically what I’m saying to C++ is this alright in my daughter class include all the
stuff from the mother class so what it’s going to do is go over to this mother class go ahead
and grab this function right here it’s not going to grab the constructor that’s another
silly role but it’s going to grab any public variables and any public functions and right
now we only have one public function in that same name and it’s going to throw it inside
the stoddard class so now this daughter class is in hairatan from the mother class so now
check it out even though we didn’t explicitly write that same name function in here it inherited
it so it’s there even though we can’t see it so now let’s say we want to take away this
part right here and we try to run this we don’t get any errors and now the daughter
is able to use that same name function so again what inheritance is is this whenever
you have a class and you want to inherit the crap from another class you can write a colon
and then public and then that name of the class and what this does is it takes everything
from this class and it sticks it in this class so this is called the base class and this
is called the derived class whenever you inherit everything from the base class gets thrown
in the derived class so now even though we can’t see the same name function it’s there
so that way whenever we created a daughter object the daughter object was able to use
the mothers functions even though it wasn’t explicitly written in the documentation or
dot CPP file whatever the heck you want to call it so that’s why we were able to run
this daughter object and use the same name function on that so that’s what inheritance
is it’s basically inheriting stuff from another class but you know how I said that there are
a couple weird rules like you needed this public thing and whenever you inherited it
grabbed all the stuff from this mother class and throw into the daughter class well that’s
not exactly true I don’t want to say I lie to you guys but for the sake of this tutorial
I just wanted to make a simple arm there are a couple weird exceptions in whenever you
inherit from another class it doesn’t actually grab everything Aronian grabs a couple things
and also you need this public keyword or out some weird stuff will happen if you try to
use protected or private and that’s what I’m probably going to be talking to you guys about
in the upcoming tutorials but for now what you have to remember is this inheritance is
basically grabbing another class and throwing all the stuff in to a derived class so this
is called the base class this is called the derived class that’s what inheritance is pretty
simple huh you inherit this stuff from another class that’s all it is so now that I talked
about that we’re ready to move on to next tutorial so thank you guys for watching don’t
forget to subscribe hopefully you understand the concept of inheritance if you don’t watch
the video again and I will see in the next tutorial
alright guys welcome to your 53rd C++ tutorial and if you’ve been following these stories
you know that I’ve been gone for like a week and I might be kind of rusty with C++ but
uh I’ll get the hang of it I’ve actually been planning something and I’m not going to tell
you guys what it is but I will say that is awesome and it’s going to happen in about
a month so that’s Sarah a little teaser so now it’s good back into C++ let me get my
head straight actually just open this program so I think what I was supposed to teach next
was about protected because that’s what’s looking like so I think in the less story
I just covered basic inheritance I probably should have you know watched that video watch
the last video before I started doing this one but who has time for that so let’s go
ahead and talked about what protected member is you know whenever I first made a class
like this mother class right here automatically it’s stuck in public protected and private
those three variables or mean those three code words and you’re supposed to put all
your functions and variables in there and I usually just deleted private or protected
and I’m like don’t worry about that we’ll worry about this later well now is the time
to worry about it so let’s go ahead and in our mother class just throw in those keywords
again so public we already know what this does let’s go ahead and make a variable for
each one so we already know what public means um anywhere inside this class has access to
this stuff and also outside this class can also access this stuff whenever you write
something in public it pretty much means anywhere in the program you can access this crap so
we’ll just name it public v4 public variable so now let’s go ahead and talk about protected
well before we talk about protected let’s talk about private because I mean might as
well get out of the way the things you know already we all do you know that whenever we
make something like a private variable that this means that only inside this class has
access to this variable for example if we want to go in another class or in this main.cpp
class this class would not have access to these private variables and that’s why we
need to build methods inside this class that are public to access those private variables
so basically what we know so far whenever we make something public any other file has
access to it whenever we make something private only this file or only this class has access
to it so saying all right this is a this is B so what the heck is this one is kind of
like sitting in the middle is it like combination of both well yes it is listen very closely
whenever you make something protected here is what it means anything inside the class
has access to it just like public and just like private any friend has an access to it
and remember we talked about friends earlier pretty much just saw out things outside the
class that has access to stuff inside your class and also your base classes remember
a derived class inherits from the base class right so any class that is inheriting from
this class has access to it so if we were to make a private variable in this mother
class and then the daughter class inherited everything from mother well it wouldn’t inherit
the private variables these private variables are still private to mother even though daughter
inherited from mother daughter does not get these private variables daughter would in
fact get public and protected so it would inherit these two but not private so that
way you can kind of see it’s a mix between public and private um it doesn’t let anything
inherit it like public and it doesn’t just stay in this class like private it’s kind
of like it in between so I’ll go ahead and make a protected variable protected V and
I’ll go ahead and show you guys an example so let’s go ahead and build a function inside
daughter and we’ll just name this go ahead and make a public function of course public
and we’ll just name it like do something nice about that right and close enough so now let’s
go ahead and build this method right here and I keep thinking like I’m forgetting something
but I guess it’s just because I haven’t programmed in a while so let’s go ahead and put void
daughter hopefully I’m doing this right all right and what was that oh do something and
got it right there for me how nice so now we’ll just go ahead and do this Oh actually
I might as well do this one at a time this will probably show it to you very clearly
so let’s go ahead and before we even start coding this function right here let’s go ahead
and build a daughter object so daughter I think her name should be Tina that seems appropriate
for this tutorial and Tina dots do something right like that so now hopefully when you
run this nothing happens because nothing’s in this function right now so let’s go ahead
and see just whether you have any errors right now nothing happens good so let’s go ahead
and do this let’s say we want to access that public variable and which is like set equal
to a number or something we just want to try and access it so let’s go ahead and try to
access that public variable and do something to like set equal to one or something stupid
well we go ahead and run this and no errors so we know whenever we do that that we do
indeed have access to the public variable and just a reminder this daughter class is
inheriting everything from this mother class and that includes a public variable a protected
variable and a private variable now we already know that daughter can access that public
variable in this line of code right here so now let’s try and step it up a notch now let’s
go ahead and try to access that protected variable protected V right there well if we
go ahead and try and set this equal to you know two or something like that and we build
and run it it works perfectly so therefore we can see that that excuse me the derived
class can indeed access the protected members of the base class and remember mother is the
base class daughter is the derived class whatever does the inheriting aka the daughter is the
derived class the core class that has the crop we want to inherit from that’s called
the base class so now we know that we can indeed access public and protected members
from the base class now let’s go ahead and do something that we know we’re not allowed
to do and try to access a private variable and let’s go ahead and try to set this equal
to three and now we’ll go ahead and try and build and run this and it says I’m Bucky I
don’t think so you know the rules you are not allowed to inherit private members so
that’s why we’re getting an error whenever we try to inherit for a private variable so
the concept behind this story is not to build a stupid program that does nothing but it’s
this whenever you’re inheriting from a class you can inherit all the public stuff you can
inherit all the protected stuff but you cannot inherit the private stuff so remember whenever
you have a base class you’re going to give this crap and you’re going to give this crap
but on you ank in this class biatch I met a crap that’s embarrassing but I’m not going
to redo the Storrow just because I mess up that one sentence okay got it all right so
let’s um you know that’s enough of me talking for this story I think that’s a I think you
guys understand the concept by now basically at the heart of the story you can inherit
everything except private variables I couldn’t make this tutorial real short but I didn’t
so thank you guys for watching I don’t forget to subscribe and by the way if you guys want
a nice organized playlist um I did I don’t really like doing them on YouTube because
it’s not really easy to organize that way but if you go to my website I have all my
videos laid out can watch one right after the other from clicking a button everything
is nice laid out in a nice playlist for you guys so thank you guys for watching don’t
forget subscribe and I’ll see you guys in the next video
alright guys welcome to your 54th tutorial and in this tutorial I want to talk to you
guys about the derived class constructors and deconstructors basically I want to figure
out this riddle whenever you have a constructor in addy constructor in the mother class does
the daughter class also inherit that constructor well it inherits everything else so is that
constructor and Deconstructor included well your short answer is no it does not inherit
the constructor and D constructor from the mother class or does it I guess you’re going
to have to watch and find out so let’s go ahead and first make a constructor and D constructor
in the mother class so let’s go ahead and remember in order to do that just create a
function with the same name mother and tilde mother for the D constructor now let’s go
ahead and set this up and I guess we’ll build the constructor first mother mother and all
we’re going to have this constructor do is like C out I am the mother constructor explanation
point emphasis on that crap and now let’s go ahead and do the same thing let’s copy
this because I’m lazy and remember dadier tilde so that’s the D constructor and just
write mother let’s get rid of the IMD it’s tacky she’s a mom mother D constructor so
now we have a mother class that has a successful constructor and D constructor so now whenever
we create a mother object like mother MA it should build the object so and I messed up
something right here some mother mother let’s see what I messed up oh here it is right here
and L you need that because end is not keyword so let’s go ahead and run the skin and now
whenever I create a mother object you can see that the object gets created I am the
mother constructor and whenever the program comes to an end the object gets destroyed
mother Deconstructor so our mother object works as expected I guess you could say whenever
the object gets created this runs and whenever it gets destroyed this runs we already know
what a constructor and B constructor does so now let’s go ahead and build a daughter
object now remember at the beginning of this tutorial I promised you guys that the mother
constructor and the constructor even though the daughter is inheriting all the stuff from
the mother I promised you guys that it is not inheriting the constructor and D constructor
so let’s go ahead and build the daughter object if I can type daughter and we’ll just name
this like Deena because that’s a nice name so let’s go ahead and build and run this and
see what we get whoa whoa whoa Bucky Bucky I think you lied to me it said I’m the mother
constructor I am the mother constructor Bucky you mofo you son of B you told me that the
daughter is not inheriting the constructor from the mother class so why on earth would
it run this constructor twice well let’s go ahead and actually build a daughter constructor
and we’ll see exactly what’s going on we’ll see if I actually did lie or if I didn’t so
in order to build daughter constructor let’s go ahead and build daughter parentheses and
also tilde daughter for the whoa what the heck is that escape the heck is ahead I do
not know how that got there anyways let’s go ahead and do that and actually instead
of having to copy this I’m just going to you guys should not do this by the way just close
your eyes for this part and just going to change this to daughter and copy this always
bad programming practice because you always usually mess up something but hopefully that
should run just like that and now the mother class has its own unique constructor and D
constructor and the daughter class has its own constrictor and D constructor so now whenever
I run this you’re saying alright so you lied me obviously the daughter did in fact inherit
the moms constructor so now that it has its own construction and Deconstructor I should
see just two constructors and 2d constructors run from the mom one from the kid so let’s
go ahead and build this in what the heck is this crap six things popping up all right
Bucky you need to take a timeout and explain to me what’s going on well in order to do
that I’ll make it real simple for you guys just comment this out right here and run this
program again and now we see once we just built this one daughter object actually let
me go ahead and take this out completely and now XS out and build it and run it again now
we see that even we built even though we built one daughter object it’s still running that
mother constructor and the daughter constructor so what the heck is going on Bucky alright
now let me take a timeout take a deep breath because I got some explaining to do when I
told you at the beginning of this story that the daughter does not inherit the mother constructor
I wasn’t lying I was telling the truth even though the daughter is not inheriting the
mother constructor the mother constructor still runs so basically here is how your program
works whenever you start this program it says alright I have one daughter object that I
need to make so the very first thing I’m going to do before I do any of my own crap in the
starter class the very first thing this program is going to do is it’s going to invoke the
base classes constructor so it says alright daughter you have one base class called mother
so let’s go up to mother and we’re going to end and we’re going to invoke the mother constructor
before we even do anything with the daughter so that’s why the very first thing we did
was invoke the mother’s constructor and then after that we were able to invoke the daughters
constructor so basically whenever you use a class it sets up all its base classes before
it does anything on its own class so that’s why whenever running the starter object it
sets everything up on top of it before it you know did the daughter stuff and another
thing I want to point out say you weren’t only inheriting from a mother but say there
was a grandmother and a great-grandmother and there was a huge hierarchy well it’s still
the same thing if that was the case then the great-grandmother would get done first and
then the grandmother and then the mother and then the daughter basically the very top class
is going to get done um the constructor is going to get finished executing first on the
top most class and this is because like I said whenever you have a class that inherits
from another class it needs to set up the class above it in order to make sure everything
is going to work inside it so the daughter is going to say RA I need the mother in order
to work and the mother is going to say alright I need the grandma in order to work and the
grandmas going to say RA need the great-grandma in order to work and that’s the very top so
that’s why the great-grandmother it would be set up first but in this case we only have
a mother and a daughter a very simple explanation so that’s why the mother gets set up first
because in order for this daughter class to work the mother class needs to be working
first so that is why the mother constructor gets called first like I said the daughter
class isn’t inheriting it’s just setting it up so that’s why this mother constructor gets
ran simple enough so that’s why the mother constructor gets ran and then the daughter
constructor gets ran but then the daughter D constructor gets called first and then the
mother D constructor that’s kind of weird you’re saying alright if we’re going to set
this class up first then you might as well destroy it first right but that’s not the
way it works it goes mother daughter daughter mother it’s kind of gets sandwiched in there
so here’s what’s happening with the rest of the program whenever an object gets destroyed
and remember we only had one daughter object in this program then here’s what happens whenever
an object gets destroyed its Deconstructor gets called so Deena’s Deconstructor got called
first and whenever your program saw that this D constructor get called it sets off a chain
of deconstructors the kid called after it so if there’s were you know a huge family
the daughter would get the daughter would die first and then the mother and then the
grandma would die and then the great-grandmother so it gets kind of built in a reverse order
then dies but that’s how it works so basically one last time whenever you’re first say you
had an object like the daughter all of these spaced classes the topmost base class gets
instantiated first it takes care of the uppermost class first because it needs every class needs
class on top of it to work in order for that object work so that’s why mother got called
even though we weren’t inheriting it we needed to set it up before a daughter could get set
up now whenever this program ends the first thing to die is this daughter object and once
this daughter dies is going to set off a chain of death throughout the family the mother
is going to die then the gram is going to die then greg grandma’s going to die then
Osama bin Laden’s going to die because that’s what happened yesterday and that’s what day
it is in case you guys are watching this like three years in feature that’s what date is
today uh May third Osama bin Laden was declared dead yesterday or two days ago whenever they
announced that so anyways I think I’ll just in on that note because I’m getting way off
topic now and I probably confuse the guys enough so I thank you guys for watching don’t
forget subscribe and again if you want to check out my tutorials and organized fashion
check them out on my website they’re all nice and laid out for you guys so thank you guys
for watching don’t forget subscribe now see eyes later
what’s going on guys it’s Bucky and welcome to your 55th tutorial and in this story I’m
going to be beginning to talk about something called polymorphism now polymorphism is a
subject that gives a lot of students and a lot of people problems because whenever someone
tries to explain it they make it really confusing so let me give you guys just a basic example
um you know not through code or anything I just want to talk to you guys for a second
so say you wanted to make a computer game and you know that your computer game you wanted
to include a bunch of enemies like monsters and ninjas and you know just a bunch of different
types of enemies well all of these enemies share the common function say they all had
an attack function to try to attack you however they all attacked you in kind of different
ways maybe the ninja threw a sigh at you and a monster tried to scare you from under your
bed and you know all these different enemies even though they had the same function you
know they worked a little bit different well will polymorphism allows you to do is it allows
you to call that same attack function on many different objects and since all of these objects
like ninjas and monsters since they’re all enemies this is why it’s valid so since they
all inherit from the same class you can call attack on each different object and even though
ninja has an attack and Monster has an attack even though you’re calling the same function
on different objects they all would know what to do they all know that they’re supposed
to attack you in some way so polymorphism basically allows you to do is use the same
function and have many different outcomes so that’s the basic idea behind polymorphism
but there’s actually a lot to understand so let’s go ahead and get to it so let’s go ahead
and start making that computer game now so go ahead and make class and we’ll name an
enemy and don’t forget put your semicolon at the end and now let’s go ahead and let’s
say that we’re going to have a ninja in a monster and they’re each going to have an
attack power how much you’re going to hurt you so let’s go ahead and well since we learn
what protected is we can go ahead and you that and we’ll put int attack power so now
every enemy has an attack power so now let’s of course make a function in order to set
that attack power so we’ll make it public of course and we’ll just make it void set
attack power and this is going to take one parameter of course some number and using
that number we’re going to set the attack power and again this attack power doesn’t
really mean anything it’s just a made-up number so attack power equals a so we’re going to
pass it in like a value of ten and you know the ninjas attack power is 10 whatever the
heck attacked by our means right so now let’s go ahead and make another class let’s go ahead
and make class ninja and this ninja class is going to inherit from Public Enemy so all
ninjas are enemies we already know that so now let’s go ahead and add some things that
are specific to only ninjas well in each of these classes let’s go ahead and make an attack
function so public void attack there we go and now what we can do is I guess I’ll just
throw everything on one line um this is the part that’s going to be specific to the ninja
class so right here we’re saying all right we’re going to have some ninjas but a ninja
is still going to be enemy I mean the enemy’s an enemy so whenever a ninja attack what I
want them to do just see out on the screen um I am a ninja ninja chop and it’ll be like
– not ninja cob come on ninja chop and it will be like a – and then you write the attack
power because remember since it’s the enemy it has that attack power attribute that it
inherited hopefully you guys watched my last couple tutorials and aren’t like what the
heck does inheritance mean and then just go ahead and that line right there so now we
have a ninja class that has all this crap from enemy since it inherit from enemy and
it also has its own specific attack function so whenever a ninja attacks is going to say
I am a ninja ninja chop minus 10 like it did 10 damage to you or something so let’s go
ahead and copy this and we’ll make another class right here we’ll call this class and
actually I’m about to scroll off the screen so let me just bump this up and we’ll make
you know this a monster class now a monster is also an enemy so you have two enemies you
have a ninja and a monster however when a monster attack it says a monster must eat
you explanation for explanation point it’ll be like minus 25 or something like eating
you doesn’t kill you it only does 25 damage so basically what we have here is three classes
we have an enemy class which is common to all enemies so all enemies are going to have
an attack power however each specific enemy attacks in a specific way a ninja is going
to chop you and a monster is going to eat you so let’s go ahead and start coding and
making all these objects so in our main right here let’s go ahead and make a ninja object
ninja and I’ll just name this n and a monster object and I’ll just name this M so you got
a ninja and a monster and now what we need to do is we need to make two enemy objects
so let me go ahead enemy and make a pointer and we’ll just name this enemy one and we’re
going to set this equal to the address of our ninja object now before I go on let me
say this because this ninja object is of type enemy remember all ninjas are indeed enemies
all monsters are indeed enemies that’s why we can do this that’s why we can say the address
equal to the pointer so anything that an enemy can do a ninja can do does that make sense
they should and also anything an enemy can do a monster can do so let’s go ahead and
just I shouldn’t do this you should never copy and paste because you always mess up
stuff enemy 2 we’ll set this equal to monster address so basically what we can do now is
this we can use that enemy 1 object and remember whenever you’re working with pointer functions
you need that little arrow and you see it already pops up attack power so our program
already knows that and any enemy object you can call attack power because even if it’s
a ninja a monster they all have an attack power so that’s why this is valid so whenever
you set your attack power to you know a ninja can do like 20 damage or 29 or whatever it
already knows that it’s supposed to apply this to the ninja object so even though we’re
setting it enemy attack power 29 it knows that indeed since it’s pointing to the ninja
object that means the ninjas attack power is 29 so basically before I go on because
a ninja is a type of enemy this is valid this line of code right here anything that ninjas
can do excuse me anything that enemy can do a ninja can do because a ninja is just a more
specific type of enemy so every how should I says every enemy has an attack power so
that’s why we’re able to do this every enemy has an attack power and the reason knows whose
attack power to set is because it’s a ninja object right here enemy 1 equals a ninja object
so let’s go ahead and just copy this again you should never copy you ladies and gentleman
and we’ll say enemy 2 which is the monster let’s set this attack power equal to you know
99 or something stupid so now we can go ahead and with each of these objects we can call
attack and it will give it their own custom text so and attack already filled in first
and M attack so we kind of used a combination of you know two different classes using the
general enemy class we set the attack power and then whenever attacks it’s going to use
that attack power like I’m a ninja ninja chop 29 so let’s go ahead and run this and see
if I forgot nice semicolons or anything and I did so hold on one second all I see what
I did wrong what I did wrong is I actually just try setting the attack power and the
method was called set attack power so let’s go ahead and copy this and we’ll set attack
power to 29 and set attack power to 99 now if we go ahead and build around that there
we go it says I am a ninja ninja chop – 29 and the reason at NU – 29 was be kind because
of this line right here and whenever we set the enemy – which was an enemy object but
it knows that it’s supposed to be working with that monster object whenever he set that
equal to set that tech power equal to 99 right there had to find it it knows that whenever
the monster attacked it was supposed to have an attack power of 99 so that is basically
how you can do that and I guess I’ll see you guys later
alright guys welcome back to your 56th C++ tutorial and in this tutorial we’re going
to be talking more about polymorphism because I know I kind of cut off the last tutorial
abruptly and that’s because I understood that I wasn’t making a couple things clear so I
wrote some comments here and read these line by line and hopefully they can make you understand
the basics behind you know inheriting and using these pointers with different types
of objects easier and also I wanted to stress one thing that you guys may not quite have
understood now if we go up to you know the ninja class in the monster class you saw that
each of these specific classes had a function called attack so a ninja could attack and
a monster could attack however if we strode up to the enemy class where they both inherited
from there was no attack function right in here so that is why we had to switch back
to using the ninja in the monster attack after we set the attack power so that’s why the
enemy object see let me go ahead and like if enemy object was to try to attack you would
get an error message because an enemy object can’t attack because this enemy class has
no attack only these specific classes do you see what I’m saying so that’s why we could
use the enemy object to set the attack power because every class inherited this attack
power but we couldn’t use those enemy objects to attack because only these specific classes
can attack so saying alright Bucky that’s uh I understand it now but it’s still kind
of you know a dumb way to program because first you make a ninja object and an enemy
object then you use the enemy object here then you have to switch back to the ninja
object right here well wouldn’t it be nice if we could just have these enemy objects
come on I keep it in that dumb plus sign what it’d be nice if we could just have these enemy
objects attack as well that would make our lives a whole lot easier if we didn’t have
to switch back to this ninja in monster class if these enemy classes excuse me these enemies
objects could just set the attack power attack move around to whatever an enemy needed to
do we’ll check it out guys that is the core concept behind polymorphism and it’s going
to make your programming life a whole lot easier so let me go ahead and I’m going to
go ahead and delete all this crap right here and I’m going to clean out all my classes
so I just have a blank monster class a blank a ninja class and a blank enemy class now
I’m going to teach you guys how to do what I was just talking how to call the attack
function on every single enemy object and have it work beautifully now before you’re
saying oMG you deleted everything I just follow the tutorial and now I’m going to have to
retype all that code why would you delete everything well I deleted everything because
the code for this is actually a whole lot easier than what would be doing before so
before I wanted to stress you guys you know how you inherit and indeed you can you know
use different addresses with different pointers and once you understood the last tutorial
come to this tutorial and look how easy I’m going to make this for you guys so we want
every enemy object to be able to attack so let’s go ahead and make a public function
and we’re going to name this void attack so now every enemy has the ability to attack
so now what we want to do is before we start coding the rest of the stuff we already know
that whenever we build a specific class like ninja and monster that they’re going to have
their own specific attack functions an engine is going to be like karate chop and a monster
is going to like roar I’m coming out from under your bed well we already know that each
of these classes that is going to pretty much overwrite this attack function so what we
want to do is we want to make this function virtual whenever you make a function virtual
it means that you’re going to be using it polymorphically and that way whenever later
on we’re using like enemy 1 attack enemy to attack it knows that it’s supposed to look
in the specific class for that function instead of using this funk right here so now when
we go ahead and you know public make our attack function using void attack and we’ll just
whoa easy back slashes now whenever we write something like void and just right you know
ninja attack and line and let me just go ahead and code this real quick copy this paste it
right there and the monster attack can be like monster attack so now whenever we go
ahead and use well I might as well just code the rest of this program because you know
instead of telling you what I’m going to code is we’re just shut up and code it so we have
a ninja object and we have a monster object now we know from the last tutorial that what
we can do is sense a ninja is just a specific type of enemy we can go ahead and make an
enemy object like enemy object and make a pointer to enemy one get my cursor out of
the way and we can set this equal to the address of ninja whom can do the same thing with monster
and this is valid again because monsters and ninjas are just more specific types of enemies
so anything that you can do to an enemy including attack you can do to a ninja and a monster
if an enemy can move around a ninja in a monster can move around if an enemy could stab you
with sword a ninja in a monster could stab you with a sword so that’s why we were able
to do this right here so now what we can do is since we made this virtual function we
can now go ahead and do stuff like this enemy one attack and let’s just go ahead and copy
this right here and now we can go ahead and write enemy to attack and if you run this
it works perfectly fine ninja attack and monster attack so basically whenever we use virtual
functions were telling C++ this our a so C++ let’s sit down and have a conversation we’re
going to be building a computer game and we’re going to be putting enemies in it now each
of these enemies is going to have its own specific attack function now whenever I call
an enemy object I’m going to have an attack I don’t want you to use this attack function
right here this is just a template telling you that each class that inherits from enemy
is going to have a tech function whenever I use an enemy object that has an attack function
I want you to find out which address aka which type of enemy has that attack function and
call that attack so basically it said our right enemy 1 attack ok let’s go ahead and
I’m going to call attack from enemy hold on enemy 1 is indeed a ninja object so I’m going
to ninja attack and then set re and me to attack well I know that enemy 2 is an enemy
object so I’m going to go ahead and call it tak but hold on this is a virtual function
so I’m going to go ahead and see oh that is a monster object so I’m going to go ahead
and monster attack you so virtual functions are basically template functions and any class
that inherits virtual members whether it’s variable or function or anything ritual is
a polymorphic class because well not because that is the idea behind polymorphism that
you can call this same function I mean check it out we’re using an enemy object and we’re
calling this Sam attack function but it has different results so that is the beauty behind
polymorphism that you can use the same function and produce different results so hopefully
I didn’t confuse you guys too much I think this story is a lot more clear than the other
one that’s you know what basically what polymorphism is that well first of all in order to have
polymorphism you need this virtual stuff right here and also the idea behind polymorphism
is that you can have different functions from the same type and I and I’m doing those little
quotation signs with my fingers you can have the same function with the same type of object
but you produce different results based on what address it’s pointing to so you know
that’s it I don’t want to confuse the eyes anymore anything I have left to talk about
I’ll talk about next story so hopefully you guys understand this a little better than
last tutorial if you don’t don’t worry we’re going to be covering it more in the future
but uh thank you guys for watching don’t forget to subscribe and I guess I’ll see you guys
in the next video alright guys welcome to your 57 C++ tutorial
and in this story I’m going to be going over a couple things I’m not going to be doing
a whole lot of coding so you guys just might want to put away your keyboard for this tutorial
and just you know sit back and watch and learn I’m going to be talking about something called
a pure virtual function and also abstract classes and actually they both go hand in
hand so you know you aren’t really learning you know two totally new concepts but let
me go ahead and before I talk about a pure virtual function let me explain you guys what
a regular virtual function knows so let’s go ahead and before we even start let’s notice
that this enemy class has an attack and this ninja class has an attack and this monster
class has an attack so let’s go ahead and actually code something in this enemy class
attack and let’s code something like C out I am the enemy class now I don’t know that’s
good enough and let’s go ahead and end that line so let’s go ahead and run this program
and see what happens it says ninja attack and monster attack it works as we expected
so basically what a virtual function does is this whenever we’re programming in using
polymorphism for example if we made it if we made an object from the enemy class it
would go up let me explain this one more time if we made an object from the enemy class
and tried to use that attack function right here what did it do is it would go up to the
enemy class and call this attack function but since this attack function is virtual
it would go back down and say alright it was a virtual function which means I need to check
what I’m pointing to and if one of these derived class overrides this attack function then
use that one instead so that’s why even though we inherited this attack function that says
I am the enemy class we overrode it in these derived class one bump says ninja Tech and
one of them says monster attack so since we overrode it right here and since we’re using
polymorphism with virtual functions that virtual function checked inside that we were indeed
overwriting it and used those more specific functions so now let me talk to you guys about
what a your virtual function is so let me go ahead and maybe this will be easier if
I saw if I go ahead and take out this attack actually let me just go ahead and cut this
from the monster class well this monster class is already inheriting an attack function from
the enemy class if it doesn’t implement its own specific attack function it inherits the
default general I am the enemy class so let’s go ahead and run this and it says ninja attack
had its own specific attack function but the monster attack did not so that’s why I inherited
the more general enemy class one simple enough so basically whenever you have a virtual function
you have the option of overriding it but if you don’t it’s no big deal because we already
have a function right here that we inherited but with a pure virtual function in before
I start talking about that let me go ahead and paste this code right back in here a pure
virtual function doesn’t give you the option of inheriting from the base class in order
to make a pure virtual function you don’t have a body at all in instead of a body right
here what you have is equals zero so basically you’re saying this virtual function which
we see equals zero instead of a body is now a pure virtual function and as you can see
a pure virtual function has no implementation whatsoever and by implementation I mean body
my body it means I mean it doesn’t do anything so a pure virtual function doesn’t do anything
at all in one rule that you have any derived class that inherits from this from a class
that has a pure virtual function in it it must must it needs to no exceptions it needs
to overrate that function so remember last I mean yeah the last example I told you we
had the option of taking this out and if we took it out it was no big deal we’ll just
use this stack function well now in every run it says uh-uh you get an error message
right here and the reason is because this you made a pure virtual function in C++ in
the programmer made a deal he said alright programmer whenever you make a pure virtual
function you need to override it that’s our deal so in this class alright you did a good
job but in this class you didn’t overwrite it at all so whenever I try to call this attack
function right here nothing’s happening equals zero so that’s why whenever we use pure virtual
functions we need to overwrite it so that can be good and bad depending on what you’re
going to try to program if you want to make a game or something that you know if you don’t
want a monster to have a specific attack it can just inherit the enemy attack that’s fine
then you don’t want to use this but if you want to make a game with a bunch of different
monsters or something and you want each of those monsters to have a specific way of attacking
then you would want to use a pure virtual function because whenever you use a pure virtual
function it’s going to make sure that you overrode it so now that we overrode in both
the ninja class and monster class we’re going to go ahead and run this and it’s going to
work just fine and you’re saying all right so I understand what a pure virtual function
is but what is an abstract class well this is like the easiest thing you ever learned
an abstract class is just a class with a pure virtual function in it pretty easy huh so
it took me like five minutes to describe what pure virtual function was and then you know
like five seconds to describe what an abstract class was so let’s go ahead and recap one
last clock one last climb one last claim no one last time all right abstract classes are
just classes with pure virtual functions in them now whenever you make a pure virtual
function you don’t have a body you don’t have curly braces you don’t have any implementation
whatsoever it’s just a function that says any derived class needs to overwrite this
function it’s just kind of checking to make sure you overwrite this function now regular
virtual functions have the option of being overridden in the drive class so if this was
just a regular virtual function then we can go ahead and take this attack out if we already
can go ahead and include it but a pure virtual function we need to overrate this attack function
and give it you know some code to run we just can’t you know run this equals zero or it’s
a computer is going to flip out so basically before you could make an enemy object that
use attack wait let me think how I’m going to say this well basically I just said all
I needed to say I’m just going to you know tell you guys how it works in my program but
I think I just said everything I need to say so basically well you know I’m not even going
to do that because I did explained it like five times so anyways that is what pure virtual
function is in all abstract classes is a class that has a pure virtual function in it so
I don’t know if in the next lesson I’m going to be showing you guys how to make a you know
how to put polymorphism to use or we’re going to be moving on to another subject but either
way it’s going to be amazing so stick with me and uh yeah we’re almost done with polymorphism
either way so if you know stressing you out just take a deep breath and you know the end
is near so thank you guys for watching don’t forget subscribe and I’ll see you guys later
alright guys welcome back to your 58 C++ tutorial and in this tutorial I’m going to be talking
to you guys about something called a function template now before I actually you know just
give you guys a definition and show you guys an example let me go ahead and throw a quick
scenario at you guys so say you’re working for Microsoft and your boss Bill Gates comes
up to you and he’s like Bucky I need you to create the world’s perfect calculator so say
alright bill let me be I will do this so the first thing you probably do is this you’re
going to create a function maybe to add two numbers together add two integers together
so you’re going to write in is your return type name the function add crap because you
know it’s the most professional thing I could think of and then you’d write like int a and
int B for your two numbers and then all you need to do in the body is you know add them
together and return them so like return return a plus B so there’s your function right there
add crap it adds two numbers together so whenever you want to use it in your main you need you
know two integer variables like int X equal 7 y equals 43 and we’ll sort the answer and
variable called Z so now we would put Z equal to what I named it add crap and we would just
throw our two variables in here by the way I got a new keyboard the other day and it’s
really different so I keep like hitting you know the wrong keys that are only one off
so you know just deal with me if I have a lot of typos in this story so anyways what
we do is go ahead and throw our two variables in there and then you know just print it out
and screen make sure I didn’t mess anything up so see out a Z and lime so let’s go ahead
and build and run this and make sure we didn’t get any errors and we get 50 and upon checking
we find out that forty-three plus seven is indeed 50 so that is our function add two
integers together but he didn’t say I want program to add two integers together he wants
a calculator so we’re saying our a so we have a bunch of different you know types of numbers
besides integers we have a flow along double um you know every you know sum with decimal
points I’m just with basic integers so we’re going to need to take this function and you
know copy it and you know basically make the same exact function but make this one using
doubles make this one using floats make this one using long and as you can see already
this program is going to get huge and it’s going to get messy and if we ever decided
that we messed up something like we put minus here and set a plus we’re going to need to
go back on every single function and fix them all and it’s just going to be a big pain in
the butt sir saying all right since this function pretty much is doing the exact same thing
wouldn’t it be nifty if we could just instead of this in trite here just replace it with
like a generic datatype and throw in any information we want well that would be a little bit too
easy or what it is there a way that we can indeed build functions that could handle multiple
types of data well you guys are in luck because that’s exactly what I’m going to be teaching
you in this tutorial instead of building a function that can only handle one type of
data if you know we’re building a bunch of different functions that did the exact same
thing why not code this to handle multiple types of data well in order to do that we
first need to build something called a template definition and what a template definition
is going to do is we’re going to be building a generic type of data basically all we’re
going to be doing in this line of code so let’s go ahead and in order to do that you
first write the name template and now inside angle brackets right after it you write class
then you give your class a name like Bucky now Bucky is pretty much a template it’s not
you know an integer it’s not a flow it’s not a character it’s just a generic type of data
so then we can use that generic type of data let’s go ahead and copy Bucky and instead
of you know having int or double or long we can just go ahead and type in our generic
type of data anywhere we would use you know a specific type of data so now your return
value is Bucky instead of passing it in an integer called a you’re passing it in a Bucky
called a and instead of passing it in an integer or float or double called B we’re just passing
it in a Bucky called B whatever the heck of Bucky is so now let me go ahead and run this
and I’ll show you guys that this works beautiful beautifully just like that so what’s happening
is this instead of building functions that take you know that take two integers and return
an integer we can just throw in generic data types instead using this template definition
right here so your template definition is pretty much building a generic type of data
and then instead of you know taking a function that you know works with one type of data
you can just use that generic type of data to work with any different type of data so
you know we use the integer right here but say we wanted to use double and remember a
double is just something with a decimal point like seven point six five or forty 3.54 and
now whenever you go ahead and yeah I just accidentally press something let’s go ahead
and yeah now my computer is freezing that is yep and that’s just great not responding
that out now just go ahead I’m not in the middle of its oral or anything I can wait
for you guys not minimize you you can stay there if you want let’s go ahead and build
and run this oMG are you flipping kidding me must be stopped to do a rebuild yes I want
to stop it see that’s what you guys get when you buy a new keyboard and all the keys are
like all over the place I saw her this is like a keyboard oh wait a minute oh it was
upside down okay it’s fine now okay so as you can see instead of integer if we throw
a double in here it causes the exact same function and it works fine so basically whenever
women we make function templates we’re doing this we’re making first a generic type of
data and instead of making a function specific to one type of data we’re going to use that
generic type of data and now whenever we call that function C++ is are going to automatically
see that X is a double and Y is a double so you must be working with doubles here so instead
of Buckey it’s going to substitute the word double that’s literally what it does it puts
double right there and double right there and double right there and it runs it just
like that and now if we go ahead and we had an integer like seven and 43 it will take
anywhere where it said Bucky and it will change it into int int int int so as long as we have
that generic type of data like that it’s going to go ahead and it’s going to work with any
data we have the one thing that you can’t do is you can’t have an integer X and a double
y you can’t have two different types of data um well actually talking about that there
is a way that you can make a template functions that take different types of data but for
now I just want to cover whenever you have a function that does the exact same thing
and you want it to be able to work with multiple types of data you can build a template function
and what that means is you’re pretty much building a generic type of data and you’re
going to be using that generic type of data inside your function instead of you know integer
years or doubles or anything like that so then whenever you use your function later
on you can use it with integers doubles or you can even build it to work with like strings
and characters if you want but you know you can’t really do that using addition so so
anyways in this tutorial we learn how to work with one type of data using a function template
in the next story I’m going to show you guys how to work with multiple types of data like
how to add an integer and along together or a double and a float and all that good stuff
so thank you guys for watching and I’m gonna go take a keyboarding class for this keyboard
and awfully when you come back I’ll be a little bit better so one thing you guys for watching
and I don’t forget to subscribe and I’ll see you next video
what’s going on guys it’s Bucky and welcome to your 59th C++ tutorial and in this tutorial
I’m going to be teaching you guys how to use function templates but instead of just a single
parameter like last time we’re going to be using a multiple parameter so let me go ahead
and explain you guys what the heck I’m talking about first you know in the last story I taught
you guys how to build basically a generic function to add like two integers together
or to add two doubles together well in this tutorial we’re going to be able to work with
two different data types for example we’re going to be able to take an integer and double
and add those together or maybe you know a long and an integer and add those together
so they don’t have to be the same data type all the parameters we pass in we can work
with two different types of data in the same function so we’re going to set this up a little
bit different then we have before so let’s go ahead and first get two different types
of data to work with so let’s go ahead and make an integer variable X and set this equal
to 89 or any integer you want and go ahead and make another you know double variable
it really doesn’t matter the data type as long as they’re different so pass it in a
double Y and set this equal to you know some fifty six point seven eight or something like
that so now what we’re going to be doing is we’re going to be building a function to compare
these two numbers and just return whatever number is smaller so in this case it would
return fifty six point seven eight so let’s go ahead and pass in those two numbers X and
Y and hopefully if we build our function correctly it will print out the smaller number right
on the screen so let’s go ahead and set up this function template right now so like before
whenever we’re building a function template we always need the keyword template but we
need to spell it correctly template just like that and now unlike before where we just had
one generic class and we named it Buckey or something we’re going to be working with more
than one generic class so make sure you name your classes something that you know you can
work with later on I like to just name mine first and second and by the way I might as
well mention this to you guys you guys are going to see this a lot like cap t in capital
u it’s like kind of a programming standard to use one capital letter whenever you’re
making a template class but I really don’t like doing that I like you know something
a little more descriptive like first and class second um you know if you’re in like a programming
class or you’re watching this because you forgot how to do this for your job or something
you typically want to use one capital letter but whenever I’m programming you’re just gonna
have to deal with it so now that we have a template this line is pretty much saying this
we’re going to be working with two data types but we don’t know what those data types are
yet so we’re going to be giving them generic names so first and second so whenever we’re
building our function you first write the return type which is first and now our function
name was smaller and it took two parameters one number and we’ll call this one a and another
number and recall this one B in all our function itself does is hello and all our function
does is it takes two numbers and returns whatever one’s smaller and we can do that by writing
one nifty line of code if a is less than B then return a if not return B so it’s going
to be saying all right is a less than B if it is return a if it’s not return B so it’s
basically going to return to you the smaller number so now let’s go ahead and now that
our function is working properly let’s go ahead and test this and make sure it works
as you can see we compared eighty nine and fifty six point seven eight and it turned
that double 56.7 a into an integer and you’re saying all right we just compared two numbers
so shouldn’t be giving us um just 56 point seven eight why did it chop off to seven eight
that’s because of this whenever we built our function template it pretty much saw what
type of data we were passing in so we’re saying alright we want to get the smaller of two
numbers we want to first pass you in an integer 89 and then a double 56 point seven eight
so I said all right make first in anywhere you see first change it to an integer so it
said re I’m going to change this to int this to double I’m going to return an int because
that was the first thing we passed then so since this return type was the first type
of data we passed in it converted this double to an integer because remember anytime you
saw first it converted it to an int we could have change this around we could have you
know passed this one in first so it was Y X and whenever we did that we would get 56
point seven eight in that way we could have more precise because in this case scenario
the first thing we’d pass then is a double and the second number we pass then was an
int so whenever it’s looking at your template wherever it sees first it’s going to replace
it with double and that includes the return type right here in the first parameter and
the second parameter would of course be an integer so it will take a double and an integer
and since it returned a double that’s why we got back the double so basically what I’m
saying is this whenever we’re working with two different types of data you need more
than one generic classes or pretty much placeholders basically a generic or excuse me basically
um a generic class is just a variable for the type of data instead of the value itself
does that make sense to you guys so instead of having a variable for this you have a variable
for this simple enough and these variables can only be types of data like int double
long yada yada yada so basically people usually build these templates to work with um you
usually don’t see more than three or four it’s usually one or two but yeah that’s basically
how it works anytime you want to build a function template that takes more than one type of
data you need to separate them into more than one generic classes and then you can use those
generic classes just like you would any other type of data so hopefully you guys understand
what that means arm again if this is just too confusing and you don’t want to keep track
you can go ahead and build a lot of overloaded functions and it’s going to work you know
just fine but if you want to use this it’s going to save you a little bit of coding and
you know it’s a little more confusing to look at but once you got it down you know it makes
your life a whole lot easier so thank you guys for watching I don’t forget to subscribe
if you have any questions just ask me on my forum and I’ll try to answer them for you
guys sometimes I’m busy but I can usually answer them for you guys so thank you guys
for watching and we can subscribe and I’ll see you guys in the next video
alright guys welcome back to your 60th C++ tutorial and in this tutorial I want to talk
to you guys about class templates so before we learned about function templates but in
this tutorial I want to tell you guys aside from just functions being able to use templates
entire classes can use templates as well and again by template all I mean is a generic
data type so let’s go ahead and build it right now so just like before the way you make a
template definition doesn’t change you go ahead and write template and then inside your
angle brackets you write class and I might as well you know start good programming practice
and use what people typically use and that’s just one capital letter instead of you know
a word you can use a word I like using word but since I’m teaching you guys might as well
get into a good habit so now after this now we basically made a generic data type we can
begin making our class so go ahead and name a class I’m going to name mine Bucky and let’s
say that well let me add my semicolon before I got before I forget Bucky is going to have
two variables now we don’t know what those variables are yet whether they’re in stubble
so I’m just going to name them T my generic data type one is going to be name first and
one’s going to be name second so now let’s throw a couple functions inside Bucky so they’re
both going to be public might as well go ahead and make our constructor Bucky and in our
constructor let’s just go ahead and for two numbers set first and second equal to whatever
numbers we pass in so T a and T B basically we’re passing in two numbers which we don’t
know what type of numbers they are yet so now let’s go ahead and set first equal to
a and second equal to B so now these two numbers and Bucky are going to have a value so that’s
what’s going to happen whenever we first build a Bucky object so now after this we can go
ahead and you know just add another method like t bigger and all this is going to do
is it’s going to find out which number is bigger will actually keep this constructor
definition right in here well we’ll take this bigger function and we’ll build outside because
there’s a couple different rules I want that you guys whenever you build a function template
what I say function template what the heck is that whenever you build a function member
outside the Declaration of class template so let’s go ahead and do that right now and
make sure I have all my semicolons in the right place I looks good that’s good one down
there looks good so far I guess we’ll find out later on when I run it but okay now the
first thing I want to know is that even though we made a generic type of data right here
whenever you have a function member outside the Declaration of the class and remember
we said we’re going to be building a function called bigger that belonged to the class Bucky
but we’re going to be doing it outside the class we always need to have that function
definition again so go ahead and copy this and paste it I don’t know why they made it
where you need it again but they did so before every single function you need to have that
now here is how you start out building your function members outside the Declaration of
the class template you first write your return type and our return type is of course T just
our generic return type and then just like before you write what class you’re going to
be working with so I’m going to be working with Bucky and I’m going to be building a
function called bigger but we just can’t go ahead and type you know the function body
right here we need to do something first right after your class name you need to add angle
brackets and add your T right here too and this is because we need to tell C++ at the
functions template parameter is the same one that we’re going to be using for the class
so in order to do that we need to specify that T inside angle brackets right after we
class name everything that’s kind of weird well I agree with it it is kind of weird and
it’s kind of stupid too but if you think this is stupid and complicated you’re going to
have to take it up with the people who made C++ I just teach it for the record I don’t
think it should be made this way but you know after the class name you need to include that
function template parameter again I think it’s a stupid way of doing it but hey I didn’t
make C++ I just teach it so now we can go ahead and you know what well of course this
function called bigger is just going to take the two numbers that are in the Bucky class
first and second and it’s going to find which one is bigger so it’s basically going to be
the opposite of what we did last time so just go ahead and return and we’ll compare if first
is bigger than second then return first else return second so this looks good go right
now now well I think that’s all we have to do for a class so basically we have a class
called Bucky now Bucky has two variables in it a first and a second variable now whenever
we make a Bucky object that’s when we give the first and second variable their values
so first is going to be like 10 and second is going to be like 20 and now we have another
function just to find out which number is bigger so whenever we call the bigger function
it’s going to take the first number and the second number which would be like 10 and 20
if 10 is bigger it’s going to return 10 if 20 is bigger it’s going to return 20 so now
let’s go ahead and make our Bucky object and run this and let me show you guys how it works
so if you think you guys know how to do this you don’t because there’s one other little
weird thing that you have to do you go ahead and make your Bucky object that just as you
think you would making Bucky Bucky object and since this is a constructor that takes
two parameters first and second we need to give it its two numbers like 69 and 105 and
now after this we can go ahead and you know see out bo the bigger and remember this bigger
doesn’t take those two numbers as parameters since this class already has two variables
in it it’s just going to go ahead and grab those variables so as you can see in this
bigger function we don’t need to pass it in those two numbers it already has it so let’s
go ahead and build an around this and say all right what the heck is this why are we
getting an error message that’s because whenever well let’s read the error message I’ll show
you guys what it says missing template arguments before Bo whenever we’re working with the
class template we need to explicitly tell it what type of data we want to substitute
for T and we’re going to be working with integers because 69 and 105 are integers so between
your class name and your object you need to pass in I and T in angle brackets so like
I said before before we go ahead and start making these objects if you think you know
where you’re doing you don’t this is what people typically forget so in the last couple
tutorials we could have done this it was optional I decided to exclude it well in this tutorial
we absolutely need to have it and whenever we do this we’re basically saying all right
this is what we’re going to be substituting for T so if this was double and we made our
class object anywhere this class saw T it would plug in double right here these two
numbers are doubles when our constructor is made we’re going to passing in two doubles
whenever you run the function it’s going to be a double instead of a T well since we’re
going to be working with integers we’re basically telling our class anywhere you see a T substitute
integer for it so that way we can work with you know different types of objects and you
know maybe we’ll make one class an integer and one plus a double and well let me go ahead
and first run this and show you guys that it indeed works 105 is indeed the bigger number
and if you don’t believe me I’ll just change this you know 258 and run it again and show
you guys that our program is working correctly so basically there are a couple weird rules
the first rule I want to tell you guys is every single function you build you need to
precede it with this function template it’s kind of weird but it’s what you need to do
also you need to have the blank generic data type after your class name and this pretty
much tells your function that we are indeed working with the classes template parameter
which is this one and also the last thing you guys got to watch out for is whenever
you make your objects you need to explicitly tell your object what type of data to substitute
for T and you do that between your class and your object declaration right here so there
are a couple of weird rules again I want to say this if you guys are stuck on templates
and this is just way too confusing for you guys all the substitution forget about them
you don’t need to build it it’s just if you want to build a program that takes less code
and makes it a little bit easier then you have the option doing this but it isn’t a
requirement so if you don’t completely understand it don’t worry about it you can move on and
you know you’re not going to be lost in C++ so anyways thank you guys for watching don’t
forget to subscribe don’t forget check out my website and I will see you later
what’s going on guys welcome to your 61st tutorial and in this sorrow I’m going to be
talking about something called template specialization specialization there we go might take me a
few times to get it but basically what template specialization is is it’s a way to make a
class that has a different implementation when a specific type is passed into it for
example if we made a regular old template like before we could pass any data type into
it like integers doubles characters and it’s going to be able to handle any of that data
well in this tutorial I’m going to be showing you guys say for example you want to build
a template to handle all numbers but you want it to do something a little different when
it handled characters because you know let’s say you wanted to build a class that handle
numbers and added them together well you can’t use all these same functions whenever you’re
adding letters together or else you’re going to get a bunch of error so it’s basically
a way of working with a specific type of data in a different way than you would regular
so let’s go ahead and the first thing I’m going to do is build a regular template and
I’m going to compare it to this new specialized template so our boss came up to us and he’s
like Bucky why is he talking to us and only using my name I’m not sure but he’s a little
weird so he’s like Bucky I wanted to build a template to handle all types of data however
if the user passes in a character which is this one specific type of data I want you
to work with it in a different kind of way so one class or one template is going to take
integers doubles floats any type of data we pass in to it but whenever we pass it in the
character it’s going to hand it a little bit differently so after me talking for two minutes
it’s finally time to start coding so let’s go ahead and build a regular template and
this is named class T so remember T can be any type of data and I’ll just call this class
spunky because you know kind of in the spunky kind of mood did anyone ever used to watch
Rocko’s Modern Life what was that dogs am I’m pretty sure it was spunky but let’s go
ahead and make a public and the only thing I’m going to be making in these classes is
just a simple constructor because I don’t need to demonstrate a whole lot I just need
to demonstrate the concept and you know not build a huge functional class so in order
to make a constructor you of course write the class name which is spunky and pass in
any type of data and we’ll name a variable X whenever we first create an object so all
we’re going to do is we’re going to create a class pass it an object and print it out
on the screen so see out X and then go ahead and write something like is not a character
because we’re going to be passing in any type of data like a number and it’s going to make
five is not in a character or 6.17 is not a character so this class is going to handle
everything besides characters basically so let me go ahead and make sure I got all my
semicolons in the right place and let’s go ahead and see a quick example right now so
actually I’ll wait to show you guys an example not yet I’ll make you guys wait so this is
how we build a basic template it can type take any type of data we wanted to integers
characters floats doubles and it could say whatever thing we passed in like J is not
character for is not a character eight point seven six is not a character and it’s going
to work fine with any type of data that’s the beauty of templates so say whenever the
user passes in a character we want it to be able to do something differently well that’s
where template specialization comes in and in order to do this go ahead and type template
with empty parameters right there or empty angle brackets this is the line where we tell
C++ we’re going to be using template specialization and in the next line we’re going to tell it
what type of data this class should handle so this again we can name it the same class
as before because the main implementation doesn’t really change it’s only the data type
in concept and the class or excuse me and you write your class name and in between the
ankle brackets after your name you write what type of data should this class handle all
right this is a special class that I want to handle characters characters only nothing
to do with numbers or any other data type anything like that this class is only going
to be four characters so again one last time the only thing that’s really going to change
is this line which tells C++ we’re going to be using template specialization and this
lat class that says all right since you’re using template special it specialization what
type of data should that specialize in and this class is going to specialize in characters
so let’s go ahead and make that constructor public and of course it’s the same spunky
however in your parameters after you don’t write T X because remember T is a generic
data type since this class is only four characters go ahead and write character right there and
now the body is just like before let’s go ahead and copy this copy right there and paste
it right there except um we can’t have X is not a character we can write X is indeed oops
no more explanation points indeed is indeed a character so basically how our program is
going to work is this we’re going to be creating objects and we’re going to be creating objects
of a bunch of different types integers doubles characters whenever our computer program comes
across the character it’s going to handle it with this specialized template any other
type of data besides character it’s going to go to this generic template or generic
class so you know any numeric data such as integers doubles floats yadda yadda yadda
this is going to handle it but once it sees it’s working with the character it’s going
to handle this class right here so it’s going to be like 4 is not a character 8.0 is not
a character but if we throw in something like J or Z it’s going to say Z is indeed a character
so don’t believe me well let me go ahead and prove it to you guys so the first thing I
want to mention is I know I didn’t tell you guys this but you can explicitly well let
me say this whenever we like past in a data type before C++ already automatically recognized
what type of data type that was but you can also do something like this um you could throw
in your angle brackets data type right after the class name and this helps C++ out and
oh great there’s a motorcycle driving buddy right now and this helps C++ out by it basically
changes this to T or it substitutes T for the data type it’s kind of just uh you know
a nice little hint for Objective C so let’s go ahead and make object one and since we’re
going to be working with integers we’ll go ahead and we pass in seven so now let’s go
ahead and copy this save us some typing and now let’s go create two more objects so two
and three and our next object work a double and remember a double is basically something
that you can use decimal points like three point one five four and now for our third
one let’s go ahead and use that character and remember a character is just a letter
so arm Q or something like that so basically what’s going to happen is this it’s going
to come across this ceases working with integers and use this generic template right here when
it gets the here is going to be working with doubles and it’s going to use this generic
template right here when it comes across this is going to be working with characters and
it’s going to say hold on a second you have a special class right here just made for working
with characters so on this one I’m going to print queue is indeed a character so let’s
go ahead and build to run this if I forgotten I semicolons and it looks good so seven is
not a character seven our excuse me three point one five four is not a character Q is
indeed a character so as you can see these first two lines right here usually use the
generic template class and the last one whenever we had a character in there it used that specialized
character template class so there you can see the beauty of specialized excuse me they’re
called template specialization so there you go it’s basically a specialization with one
data type in other word you have a different implementation when a specific data type is
passed in in this case character so there you go I hope you understand it if you don’t
just go ahead and ask me on my forum or leave a comment below and I’ll try to answer it
for you guys so thank you guys for watching don’t forget subscribe to my channel and I’ll
see you guys in the next video what is going on guys welcome to your 60 second
tutorial and in this tutorial I’m going to begin talking about something called exception
handling now an exception is a way of handling an error that might occur when your program
is running for example if you built a calculator and the user is trying to divide by 0 that
may be a error that occurs when the program is running or if you know you have you made
a game and you need a user to register if they try to use like 999 as their age that’s
unacceptable so that may cause an error to occur so let’s go ahead and take a look at
the syntax for this now the first thing we need is something called a try block now in
this try block this is where we write the code that we want to try aka the code that
we think error might occur so let’s go ahead and make something like I’m going to be making
a simple program that takes two ages the mother’s age and the son’s age now we all know that
a son cannot be older than its mom so whenever the sons age is greater than the mom’s age
this would of course cause an error so let’s go ahead and make two variable int mom’s H
and int sons actually might as well summon equal to values right now and mom’s age equals
like 30 and its sons age we’ll set this equal to 34 now we all know this is impossible so
let’s go ahead and test it right now so if the son’s age is greater than the mom’s age
then we want to go ahead and we want to do something called throwing an exception now
it’s basically this whenever you have something that occurs that shouldn’t occur we need to
tell the computer that an error did occur and we do this by throwing an exception and
in order to throw an exception exception go ahead and type throw and then type an error
number and you can make this any number you want you can actually do other things besides
numbers but just throw a number because later on say we made a program that check for you
know 20 different types of errors we want to know what air ha so let’s go ahead and
we already know that in our little manual 99 means that the mom is younger than the
Sun so you know it’s pretty much just a reference number so anytime we get an exception and
remember anytime this line of code is going to run it basically means that an error occurred
so now that we know an error occurred we need a way to handle that error because we just
don’t want our program to freak out and shut down so in order to handle the air we do something
called catch the air catching the air allows you to pass in that number which is 99 so
now X is equal to 99 and we can handle it a certain way for example we can just output
to the user on something like this son can not be older than mom um and then for reference
or a err number and this is where we write that’s X right there in line and just scroll
head so you guys can see what I’m typing so now if you go ahead and run this we can see
all right whenever this program runs and the kid is older than the mom then we have error
message that pops up that says son cannot be older than mom err number 99 so you know
this is pretty much just for our reference this error number but let’s go ahead and say
that the mom was indeed older than a son the mom was like 50 and the son was 34 well this
is going to work fine and this is never going to happen so no error is going to be thrown
no exceptions are going to be thrown so this catch block is just going to be skipped so
let’s go ahead and run this and we don’t get any error messages this program works perfectly
so basically the concept of this is or oh is this an exception is a way of handling
errors that may occur when your program is running for example if the user tries the
divide by zero or pretty much if the user does something they aren’t supposed to do
now in the try block right here this is where you write the code where the air might occur
and right after it you need a catch block now the catch block is basically what what
to do if there is indeed in air and if there is no air in this try block then the catch
block is skipped all together and just like a function a catch block takes parameters
so it can take an integer in this case such as 99 and that’s pretty much just like an
error number you know whenever you’re just playing on your computer and windows messes
up you know how like the error message pops up well that’s pretty much what we’re doing
right here it’s like error message to the yada yada yada do you want to send to Microsoft
or do you don’t want to send by the way does anyone ever send those do they really get
those error messages I don’t think so but anyways now you guys know the basics of exception
handling and by the way if you guys are saying you know what Bucky I could just have used
an if statement and you know made a separate function for this well I know that we can
do this a different way but um exception handling is useful whenever you’re trying to you know
do computer stuff like allocate memory I just want to show you guys a real easy example
for this case but I know that you could have probably made this program just by using a
basic if statement and you know maybe a simple function or something but I wanted to demonstrate
the try and catch block in how to handle an error message so these are useful for a lot
more than you know comparing two variables and there are some instances where we can’t
just make a simple program that we need to use this exception handling so for now I just
want to tell you guys that so I guess in the next tutorial I’m probably going to show you
guys another example and show you guys how you can use a different parameter for this
catch block right here but for now that’s all you guys get so thank you guys for watching
don’t forget subscribe and I’ll see you guys in the next tutorial
what’s up guys welcome to your 63rd C++ tutorial and in this story I’m going to be showing
you guys another example of exception handling so let me go ahead and I want to cover you
know one other little inky dinky thing it isn’t a main concept but if I don’t cover
this I’m going to you know it’s like OCD I need to cover or else it’s going to bug me
for the rest of my life so let me go ahead and first what I like to do is this whenever
I’m working with exception handling I like to set up the framework first so I like to
go ahead and put try right there and after this put catch with my parameters right like
that and that way whenever we get you know type in a bunch of code in here I don’t have
to go search and be like alright what’s this curly brace go to with these parentheses go
to it’s really easy if you set up the outside first and work your way in then that way you
don’t get confused so let’s go ahead and in this program I just want to build a really
simple calculator I’m going to have the user input two numbers and I’m going to make sure
that the bottom number is not zero because remember you’re not allowed to divide by zero
or else the you know your computer is going to blow up so what I like to do first is a
well before I even do that let’s go ahead and just start with the program so let’s go
ahead and make two variables in num1 and i’ll save in um two for later so let’s go ahead
and give the user prompt like see outs and we’ll just say something like enter first
number and line and now let’s go ahead and give them a way to enter that number into
number one and by the way I read this a couple days ago and maybe this will help you guys
understand this arrow on this arrow the arrow shows the direction that information is flowing
now whenever you output something it goes to your computer screen so this text is going
to your computer screen now whenever you input something it gets inputted and stored as a
variable on your computer so the text that you enter is going to be stored in a variable
in your computer you see where the informations phone to your computer screen to your computer
basically the information is flowing to the user and back into the computer pretty cool
huh so I probably should have said that in Lycia my second tutorial but you know 63 tutorials
in I guess is better late than never so now the user entered a number and it’s stored
in the variable number one so now let’s well I might as well just copy this right here
and copy this and paste it right over here and we’ll change this to two so enter two
it’s going to be sorted two and enter second number pretty good so now after this what
we want to do is it really doesn’t matter what number they answer for you know number
one as long as it’s a number but number two it does matter because the second number cannot
be equal to zero because if we remember from you know fifth grade math class or whatever
you can’t divide by zero it’s illegal the math gods will not allow it so let’s go ahead
and have an error message pop up whenever you try to divide by zero and we can just
go ahead and put a simple if statement if num 2 is equal to zero let’s go ahead and
throw an exception so we’ll just throw the number zero and now what we want to do is
remember if number two is not equal to zero that means that the numbers that they entered
were valid so we’ll just go ahead and print out the answer on the screen num 1 divided
by num and in that line so basically if they enter zero is the second number we’re going
to get a error if they enter two valid numbers is just going to print out the answer by dividing
the top number by the bottom number so if we do indeed get a error remember in the last
tutorial I tell you guys that you can pass in a number two uses like an error reference
number and you can just erase something see out you can’t divide by X and that way whenever
we throw zero it’s going to pass zero and as X and it’s going to say you can’t divide
by zero so let’s go ahead and run this and see what happens enter the first I’m going
to first run a program that works perfectly and our 32 buffer my first one enter 16 for
my second one and 32 divided by 16 is indeed 2 as you can see here so my program is working
beautifully but let’s go sit ahead and say that we have an idiot that’s gonna you know
never knows how to use calculator and is going to try to divide by 0 okay 32 okay everything
is going good so far but then I’m going to try and enter 0 is my second number and whenever
I try to hit this and solve this riddle we can see you can’t divide by 0 so as you notice
as soon as you get a error message it basically just jumps right down to this catch block
right here and it says hold on you did something wrong here’s your error message you can’t
divide by 0 so now the little itty-bitty thing I want to cover in this story is this you
can have multiple catch blocks to catch different types of error messages right now the only
type of error message I showed you guys is the integer error where you can catch a double
arm you can actually catch a character if you want you catch any different type you
want now aside from covering that I don’t need to show you guys you know the different
types of error messages you can catch but what I do need to show you guys is this little
thing right here dot dot dot you’re saying what the heck type is this a dot dot ok I
understand that I can catch you know intz and characters whenever you just want any
error message to be caught for example you don’t know what error message is going to
happen in this try block at any time any error message occurs you just want it to be caught
right here then you can use the da-da-da-da-da is pretty much the all encompassing the default
catch so basically if you have in so basically anytime an error message is thrown it’s going
to be caught by the dot dot no matter if it’s sent in character yadda yadda yadda so with
that being said this is the catch that you see most often I just wanted to throw you
guys the piece of information that you can indeed catch specific types of error messages
but this is the one you’re going to see more often so let’s go ahead and try and run this
and it says alright and once again I did something wrong oh I see what I did I actually tried
to pass an X and X is no longer existing so I’m going to go ahead and try to run this
one more time and you see it says enter first number 32 and then we try to divide by 0 get
error message and we get that default you can’t divide by error message so typically
you would want to write something like this it would probably make more sense but anyways
I just want to stress you guys that aside from catching different types of error messages
you can also catch general error messages and that’s what we’re going to be doing most
of the time so now that you understand that you understand most of everything there is
that know about exception handling there are a couple other things that uh I don’t even
know if I want to cover or not there’s like passing in objects that you can catch and
also um you know inheriting from exception classes and default messages and stuff but
I really don’t know if I want to get in that I mean we understand the basics so in the
next tutorial don’t know if I’m going to be covering more about exception handling or
you know jumping into the next but either way it’s going to be amazing so if you have
any questions check out my form the new boston.com slash forum and yeah I guess I’ll see you
guys in the next video what’s going on guys welcome to your 64th
C++ tutorial and in this tutorial I’m going to be beginning to talk about files now we
need to be able to work with files because files are what you use to save data on your
computer you’re saying hold on Bucky I know what’s used to save data we can just you know
store the data and variables or something like that well we can’t exactly do that for
permanent data for example whenever we store data and variable and we close out of the
program or maybe we want to shut down our computer that data is going to get wiped out
now in order to save data permanently what we need to do is we need to store it on file
on our computer and what this does is whenever we’re working with files it stores it to your
hard drive it permanently edges it in there so then whenever we shut down our computer
we can go ahead and pull it back up and everything is going to be working fine for example if
you were playing a computer game or if you had to write an essay for school you wouldn’t
want to just store all that information and variable because then the user would have
no way of saving that data so what we’re going to be doing is we’re going to be working with
files storing them on the computer’s hard drives and that gives our user a permanent
solution to save data very useful and well you’re probably going to be working with files
in every single program you’re using unless you know you’re just making a cool animation
or something so let’s go ahead and the first thing I need to show you guys is we need to
include an additional header file so let’s go ahead and copy this save us a little bit
of typing and instead of iostream we’re going to put F stream right here now this is the
header file that we need for file input and output without this header file we won’t be
able to work with files at all basically so that’s your main file file I guess you could
say so let’s go ahead and now that we got everything included we need again this is
to work with files this is the input and output data or basically how to add data to the file
and read data from the file so there you go and let’s go ahead and start coding now so
here is how you work with files and c++ the very first thing you need to do is you need
to build an object since c++ can’t work directly with the file itself and needs to be able
to go through objects just because you know whoever made c++ that’s how they made it so
the very first thing we do is we need to write o f stream right like that and this allows
us well there are different types of file streams basically this is how you create a
file object and right after this you give your file object a name any name you want
I’m just going to name mine Bucky’s file now we have a file object aka access to a file
so now the first thing that we wanted to do after this is we need to associate this object
with a file itself because if we’re just working with this object it’s like our a you have
like a billion files on your computer how am I supposed to know what object this one
or excuse me what file this object is supposed to you know represent well that’s what we
do in the next line of code and in order to do that we go ahead and type the name of our
object and we open a file and we just do that using the open function without the star right
there and without this right there we use the open function and inside is a string we
pass in what file do we want to open now I’m going to go ahead and open tuna dot txt and
you’re saying all right Bucky I don’t have a tuna txt file on my website orcses sorry
I’ve been making a web tutorials way too often but ever seen all right Bucky I don’t have
a tuna txt file on my computer so is it going to crash whenever it tries to open it won’t
know whenever you’re working with files in C++ and it opens a file that doesn’t exist
it’s going to see that it doesn’t exist and it’s going to automatically create one for
you for example my file is going to get created right here now as you can see in this directory
there is no tuna txt file so whenever I try to open it it’s going to look for a tuna txt
file and it’s going to say doesn’t exist I’ll create one for you so it’s going to get created
automatically and it’s pretty cool because it saves a you know a line of code from how
to create it so basically what we’re going to be doing so far is creating a file object
that way we have access to some file and in this line of code right here we’re going to
say what file do we want to be associated with this object well now tuna text file is
associated with this Bucky’s file object so now basically any input and output that we
make to this object is going to be input and output in this file we just can’t you know
input and output directly to the file because C++ won’t let us basically Bucky’s file is
the exact same thing it’s tuna text file from now on so now we can go ahead and you know
output characters that if I will read every one output for example you’re saying all right
how do you output to a file what are you talking about well just go ahead and instead of outputting
things to your computer screen like we have before we use that operator right there those
two little less than signs and we just write some text like oh I love tuna and tuna loves
me and we won’t in that line actually go ahead and put a new line just like that and now
you’re saying all right before I had C out right there and output it to my computer screen
well now I’m going to go ahead and have Bucky’s file right there and all this text is going
to output to a text file instead of the computer screen so nothing is going to show up on the
computer screen what this means this little operator is output this data to this location
so before we were outputting the data to the computer screen and now we’re just outputting
it all to a file simple enough I mean the operator is pretty cool it works in that way
so now like I said we basically associated a file with an object in these two lines and
now anytime we use this object name it pretty much knows we’re going to be it represents
this file so it knows whenever we want output this text it’s really outputting it to this
tuna txt file right here so that’s going to run perfectly but at the end of all your programs
when you’re done working the files you always want to close that object and what that does
is any you know little memory or buffers or any resources that the computer is using when
it was working with this file right here it’s going to close them out and it’s going to
free your computer’s memory so it’s just good programming practice to do this so let’s go
ahead and type our file object buckey’s file and just go ahead and type closed with no
parameters so let’s go ahead and run this program and as you can see nothing happens
right here because we didn’t output anything to the computer screen but if we go ahead
and take a look at that directory we now have a tuna txt document that got created so if
we go ahead and double click that the content of this is I love tuna and tuna loves me pretty
cool huh so as you can see this would be useful whenever you’re you know creating a computer
game and you have a character with a bunch of statistics like health and you know happiness
and hunger they can go ahead and play the game for you know an hour store all their
data shut off their computer go to school go to work whatever come back and all their
data is going to be right there for them it’s not a new computer game every single time
you start because that would be kind of waste time so that is how files are useful in computer
programming and how to work with a simple text file so if you think you’re done with
files yet you got a long ways to go so in the next story we’re going to be covering
more about files and trust me there’s a lot more information that we need to learn but
for now thank you guys for watching don’t forget to subscribe and I’ll see you guys
in the next video alright guys welcome to 65th C++ tutorial
and in this lesson I’m just going to be giving you guys a couple tips when working with files
and this is you know something that can save you time as well as some good programming
practice so it’s going to save you a lot of headaches in the future so let me go ahead
and get started so in the last tutorial I told you guys that in order to work with files
you first need to do two things you need to create a file object and then you need to
associate that object with a file itself on the computer so let’s go ahead and instead
of doing that in two lines of code let me show you guys how to do it in one line of
code so of course you need your of’ stream because that’s what object we’re going to
be making in order to work with files and then you give your object a name like buckey’s
file and then you might be thinking all right just go ahead put a semicolon go the next
line of code put bucky’s file you know open the file you want to work with but there’s
the easier way to associate this object with a file now whenever you create a file object
it takes an optional parameter in the constructor actually the entire constructor is optional
but if you want you can go ahead and do this all in one line of code by associating a file
by passing into the constructor and then whenever you create your object it’s going to associate
that file with this object so for example let’s go ahead and make a file name beef jerky
text and now instead of creating your object on one line of code and then associating the
file with this object on another line of code we can just go ahead and pass the file name
into the constructor and now this file is associated with this object so it’s probably
better to get used to this syntax unless you’re making a program where you don’t know what
file is going to be associated with this object whenever you’re running it so usually I like
to do it this way just because the save is a line of code and again this constructor
is optional but trust me it’s going to save you a whole bunch of time in the future so
now you can do things like um Bucky’s file and then just go ahead and output something
stupid like I love the beef and it says oh I love the beef but you know good enough and
then go ahead and might as well add a new line and then get my cursor out of the way
and then of course it’s good program practice to have Bucky’s file close to free up any
of our computer’s resources so let’s go ahead and actually watch this file get made so I’m
going to go ahead and press run and oh man and close it out but anyways you can see now
that we have a beef jerky txt file so let’s go ahead and click that and it says ooh I
love beef and i do love beef jerky can’t lie about that so let’s go ahead and before I
let you guys go I want to tell you guys one more thing a function that you’re going to
be seeing a lot if you’re working with the team of developers or you know if your teacher
gave you a assignment working on the files is some function called is open now is open
is a function that you can use on a file object and it’s going to return true if the object
is currently associated with a file and this is a way that you can check basically if a
file is open and you know it’s ready to be worked with so in order to do this go ahead
and write if and then you do this write your file object which is in this case the buckey’s
file and then write your function right after it is underscore open now this isn’t going
to take any parameters and just go ahead and write something if the file is open then we’ll
just output on the screen like um okay the file is open good enough and then might as
well go ahead and in that line and we’ll say else if the file is not open then let’s just
go ahead and output like Bucky you messed up and will spell everything wrong and looks
good go so go ahead and in that line so now what’s going to happen is this all right we
coded this line correctly and it did indeed open this beef jerky text file so whenever
we check if the file is open this this is open really isn’t a good name it really should
be like if object is associated with a file that might be a better name for this but you
know that would be a lot a lot of text so what this line is going to do is it’s going
to check if this object is associated with a file if it is which it is it’s going to
output ok the file is open if it’s not and we did something wrong it’s going to say Bucky
you messed up so let’s go ahead and run this and indeed we get ok the file is open so saying
all right so when would be the case scenario when the file wouldn’t open well for example
if you go ahead and you made you know your file object correctly but you forgot to associate
it with a file right here then whenever we try to run it it’s going to say hold on Bucky
you messed up you forgot to associate your object with a file so that is case scenario
where we would want to check if the file is open or not and basically in programming well
in the real world how it works is this you make sure it’s open first and then you write
all your code right here so it’s a nice little check before you know you just start writing
code like this this is probably would be better if we threw inside here you know so if you
wanna you know impress your boss or your teacher just make sure your files are open before
you start doing a bunch of crap to them or else you know it’s going to save you a lot
of headaches so with that being said and this is where I just want to stress you guys that
there is indeed an optional constructor where you can associate a file with the object instead
of putting on two lines of codes and also what you can do is it’s a better programming
practice to make sure your file is open and by that I mean make sure your file is associated
with the object before you start doing a whole bunch of crap to it it’s going to save you
some time and save your computer a whole lot of headaches so with that being said that’s
all I have for you guys for the story so thank you guys for watching I don’t forget subscribing
if they have any questions so check out my forum the New Boston calm slash forum and
yeah I’ll see you there so thank you guys and I’ll see you next video
alright guys welcome to your 66th C++ tutorial and in this tutorial I’m going to be teaching
you guys how to make a custom file structure to work with your program because whenever
you build your own custom program you just don’t want to use any file format you know
playing off-the-shelf format you want to build a custom file structure so that it makes it
easier for your program to work with your files and I don’t know what I’m talking about
now but trust me you will buy that in this tutorial so the first thing we need is an
idea actually before we get started let’s go ahead and say that we wanted to make a
game like Sim City or whatever where we had a bunch of different players or a bunch of
different characters and we wanted to store all the character information and the text
file well the first thing we need is of course a text file so go ahead and put of’ stream
and we’ll just name it like the file good enough I guess and we’ll just go ahead and
create a file called players dot text now remember if this file already exists then
C++ is going to look at it’s going to open that one but if it doesn’t exist yet which
it doesn’t let me go ahead and pull up my directory see no players that text since it
doesn’t exist already C++ is going to automatically create one once it sees that indeed does not
exist so let’s go ahead and the first thing I want to give is we’re going to be making
a program basically to enter the character information you know say we wanted to build
a bunch of characters ourselves well let’s go ahead and give the user an ISO prompt about
what we want them to do so see out not mmm close but you know not close we’ll just go
ahead and put enters enter players ID and then you know maybe they have a name for each
player and maybe they have like a bank account money or something simple enough and then
go ahead and end that line so this is just going to print some text out on the screen
telling them what to do so we’re going to enter a number a name and another number the
first one’s going to be the ID number the second one is going to be their name like
Bucky and third one’s going to be like 48 dollars in their bank account something stupid
like that and now what we want to do is we’re going to be working with something called
an end-of-file marker now before I go on let me say this every file on your computer when
C++ opens it’s going to look for the end of file marker this is pretty much the end of
your file it’s I mean no weird complicated explanation it’s just a little symbol that
goes at the end of your file to tell C++ you know how long your file is whenever it’s reading
your file and it gets that end of file marker that’s where it knows where to stop it’s pretty
much telling C++ where the end of your file is simple as that now what we can do is we
can explicitly enter and end a file indicator and I’ll show you guys how to do this later
on but basically whenever you hit control hold down control in his e on your keyboard
if you’re working with Windows this inputs an end-of-file marker and I know it’s kind
of a weird way to do things because if there was an end-of-file marker button on your keyboard
it would be a lot easier to see but if you’re working with Windows then hold down control
in his E and this puts the end of file marker and so we’ll just go ahead and they probably
don’t know that so we’ll just go ahead and put press ctrl + Z to quit the program and
you know stop um inserting characters or whatever so go ahead and in that line I know that’s
a little weird but we need it or this program would just keep running forever so now we
need three variables that are temporarily going to store this information just go ahead
we’ll make the ID number an integer and we’ll make the name our string of course and we’ll
put the money since it has decimal points like 48 dollars and 87 cents we’ll go ahead
and make a double so we have three variables ID number name and money now what we need
to do is we the user want to be allowed to enter this information whenever this program
is running so let’s go ahead and make a while loop because we really don’t know how many
characters the user is going to enter and here’s what we do remember anytime you want
to get information from the keyboard aka from the user use CIN and then the first thing
they type in is going to be stored in the variable ID number the second thing they type
in is going to be the character’s name and the third thing they type in is how much money
the character has so the user is going to be able to type characters information you
know maybe five different character informations and then we want to be able to store in a
file so this basically the user is going to be able to type in as many characters as they
want so this loop is going to be true as long as they don’t get to the end of the file and
how do you know you get to the end of file because of this weird control-z thing so basically
whenever the user enters information we now have an ID number a name in some amount of
money so now the only thing we want to do is we want to insert it into the file so of
course remember just like before in the last Oriels we write the file object and this is
pretty much a synonym for players text so they can be used interchangeable they can’t
be used interchangeably but whenever you’re looking at it just assume that this means
player is a text so anything we insert it just gets directly inserted into the file
players out text because that’s the file associated with object right here so the first thing
we want to insert is the ID number and then just so it looks nice and pretty just go ahead
and insert a little space and then the next thing what do we have a name and then a little
space after that and then the last thing is money and line and let me make sure I got
number space namespace money and line okay so it’s good go so basically well let’s go
ahead and run this program and see what we get it says all right enter players ID name
and money so let’s go ahead and make a couple players for this you know Sim City social
community game whatever that game were making so the players ID is going to be like 1 and
the players name is going to be like Bucky and the money I have is like $65 and 47 cents
go ahead and press Enter add another character on the ID is 2 the name is Sara and she has
43 dollars and 21 cents and let’s make one more 3 is Sofie and she has two dollars and
54 cents now let’s go ahead and press well we can just go ahead and press ctrl-z right
now and sometimes you have to press ENTER and then your program is going to stop sometimes
you can just hit ctrl Z and it send a file sometimes you need hit ctrl Z enter so anyways
I hit ctrl Z that signified the end of the file and of course when I got to the end of
my file I couldn’t insert anything else so my program quit as you can see right here
so now if you go up to your documents you can see that you have a txt file created called
players text so let’s go ahead and click that and you can see at the well basically it inputted
exactly as we input it does that make sense no well too bad you can see that it wrote
to the file exactly what we wanted to exactly in the format that we wanted to so basically
what was going on is this we first created a file that we open and whenever we open it
we have access to that file we then gave the user a nice little prompt to show them how
to input the data and then made three variables so we could temporarily store the data now
the user was allowed to fill in the variables ID number name and money and whenever we had
those variables populated with values we just stored them in a file using whatever format
we wanted to in the format we wanted to store min was the ID number space name space money
and then once you got one character or one player inserted just go to the next line and
do it again so that way on the next line it was ID number name money on the next line
ID number name money so that is how you can have you know a way of storing data into a
file formatted how you want it so you’re saying all right why would I want to format it like
this well maybe you wouldn’t but whenever you know the structure of how your file is
formatted that way whenever we open the file to read all the information in we know what
format it’s going to be and so that’s why you want to create custom file structures
because custom file structures make it easier for your program to work with files if they
know where the data is going to be and also what type of data is going to be there for
example in our game we knew that that first row was always ID numbers that second row
was always names in that third row was always and by the way row I mean column that third
column was always the amount of money so I mean it just makes it really easy to program
this way so that’s all I have for you guys for this tutorial I know this was kind of
weird with this control-z thing I probably should have made it without it but hey you
guys learned something new I guess so thank you guys for watching don’t forget to subscribe
and I’ll see you guys in the next video what is going on guys welcome to your let’s
see sixty seventh C++ tutorial and in this tutorial I’m going to be teaching guys how
to read custom data so in the last story what we did is let me scroll to my desktop watermelon
we created this players text file and what it did is basically allowed us to create our
own custom file structure and what we did is we were making our computer game with a
bunch of different players we gave them an integer as the ID number a name which was
a string and their bank account which was of course a double so now we have all our
data right here and it’s laid out in a very specific way no other program could really
understand what this data is used for except our program so now that we have that data
we need to be able to read it specifically in our own custom way so it’s a little bit
different than writing it for a couple of reasons which I’m going to be talking about
so the first thing I want to stress to you guys is you can’t use o F stream Oh F is for
outputting data to a string for example when you have text and you want to output it to
a file or write it to a file whenever you want to read data in from a file you need
if’ stream this allows you to take data from a file and read it into you know the computer’s
memory or whatever so it’s the opposite of Oh F stream Oh F is for writing that a to
a file i F is for reading data from a file so then you go ahead and create your file
object I’m just going to mine the file and then of course is a constructor optional optional
constructor you can take the name of that file so players dot text was the name of that
file you can either create the text file document or excuse me what am I going to say the file
object on one line of code or break it up into I always like to put it on one line of
code just like when we were writing to a file so aside from that what we’re going to be
doing is we’re going to be reading the data from that document so remember that document
have three pieces information in it on each row it had an ID number a name in the amount
of money so let’s go ahead and make variables to hold that data into ID and then we’ll make
string name to store their name and double money to store their you know bank account
information so basically how we’re going to do that is this we’re going to create a while
loop in the wallet you can loop through each piece of information whether it’s their ID
number their name or their money and it’s going to store it in a variable and whenever
we have that information stored in the variable is we can do you know whatever you want that
we can load it into our computer game we can just go ahead and print it out on the screen
we can go ahead and send it to database on the internet but basically what we have to
do first is take that data from the file and we need to store it on variables in our program
so I want to talk to you guys about how we’re going to do this remember I said in the last
tutorial that every file has end a file marker and C++ pretty much is going to run this program
until it gets to the end of file marker and then when it gets to the end it’s going to
return something called a null pointer so let me go ahead and actually code this and
then I can explain what I’m talking about so just like before whenever we were you know
whenever you use like cin to get information from the keyboard and then you could store
it in variables like this well now we’re getting information from the file so we go ahead and
type the file and that’s where the information is coming from and we’re going to store an
ID the first piece of information and the second piece of information we’re going to
store name and the third piece of information we’re going to store in money so it’s basically
going to loop through this while loop and look at the file each time and every time
it gets the new piece of information it’s going to store an ID name and money now let
me say this whenever you’re working with files there’s something called a file pointer let
me go ahead and open my document again and I could probably show it to you guys really
easily or it’s a watermelon players so whenever you’re working with files you have something
called a file pointer now the job of this file pointer is to keep track of where in
the file C++ is working with so what it’s going to do is this file pointer let me go
ahead and skinny up my program a little bit is going to start at the very beginning and
by the way the end of file pointer is what right there where my cursor is but anyways
your file pointer always starts at the very beginning what this loop is going to do right
now is it’s going to read the first piece of information so it’s going to see right
there that’s the first piece of information that number one right there is what I want
to store an ID so next it says alright this next piece of information store a name so
it’s going to scroll the file pointer is going to go to this next piece of information and
it’s going to see this one right here Bucky should be stored in name and then it’s going
to say all right now the next piece of information which is this right here I’m supposed to store
this in the variable money so that’s what’s going to happen the first time this program
loops through so the next time it loops through the file pointer is going to be right here
so that’s why we didn’t have to change any line of code right here because we’re working
with the same object yet it knows the next piece of information is this too because the
file pointer the next time this loop runs is going to be at the beginning of the next
line or at the end of this line pretty much the same spot so that is how the file pointer
is going to be working and by the way it actually is called the file pointer so you know I’m
not making that up so that’s why this loop is going to run fine each time so now that
we have the correct pieces of information stored an ID name and money we can just go
ahead and if we were making a game or something we would want to load them into the game but
for now let’s just go ahead and print them out on the screen so see out / now ID and
then go ahead and I’m going to make a comma-separated list instead of you know spaces because you
know if you’re working with Excel well nevermind oh they use comma separated list but anyways
I just like how it looks so the next piece of information is name and I’m just going
to add a comma right there and lastly what is it money so after this I’m just going to
go ahead and end that line and whenever you run this program it should work just fine
so what it did is it read that file from the txt file one Bucky sixty sixty five point
four seven and it did what we wanted with it in in this case what we wanted to do with
it is just output it on the screen so we set alright now that we have the information output
the ID and then a comma and then Bucky and then the comma and then money and then end
of the line so it goes to a new line so then where this is working in the file is it’s
at the end of the file so whenever you run this loop again the next idea jumps down to
is number two it runs the entire loop it’s at the end of that line and then whenever
you run this loop again it jumps down to the ID number three and it runs that line of code
and how this does this while loop know when to stop because whenever the end of file marker
has been reached C++ knows already without us having to type any extra code that a null
pointer is returned basically it changes this boolean to false so whenever this is working
this is true whenever it gets to the end of file C++ already knows that this loop should
be false we don’t have to explicitly you know write the if statement in here to check it
or anything it’s one of the cool features of C++ it automatically knows when to end
so that’s one cool thing that we don’t have to worry about another cool thing that we
don’t have to worry about is whenever we’re working with this if’ stream or OS stream
object whenever the file reaches the end of the file and this goes to false this object
is going to get deleted automatically in the Deconstructor remember when we talked about
constructors and deconstructors the D constructor is going to call the closed file method so
remember like in the first file tutorial I told you that told you guys that you need
to close your files in order to free up resources well with this I have stream constructor or
excuse me D constructor is going to do is as soon as this file object gets deleted this
file is going to close automatically so that’s two things we don’t have to worry about whenever
we get to the end of file this is going to go to false in the loop is going to end automatically
and also whenever it gets to the end the file the D constructor is going to close the file
automatically so I don’t have to explicitly write the file dot closed so pretty much everything
is handed to you you just got to remember how to use it C++ made it really really simple
to work with files if you really think about it you don’t have to do all the background
stuff so there you go that’s how you write and read a custom file and again your file
structure is going to be different depending on type of application you make but that’s
the basics of it so thank you guys for watching don’t forget subscribe I know you guys probably
have a lot of questions just ask my on my forum the New Boston comp forum and I’m going
to go to McDonald’s get a extra large milkshake and you well check out my next video so think
guys watching and I’ll see you then alright guys welcome to your 68th c++ tutorial
and these next couple tutorials are going to be a chain of tutorials I can’t teach you
guys what I’m going to teach you in one tutorial so it’s probably going to be like a three
or four part series but anyways what I’m going to be teaching you guys is this I’m going
to be building a really cool program showing you how to work with files and by the end
of these tutorials you guys are going to be a pro on how to work with files so basically
what we’re going to be doing is we’re going to be pretending we’re making a computer game
now in this computer game we have a character and this character in his own little world
is going to come across a bunch of different objects some objects have no effect on them
whatsoever zero effect on his health or is you know happiness whatever you want to call
it some objects like ninjas meth and dirty needles they harm our character so they’re
going to you know have negative 54 energy or what to say it’s like energy or something
now some objects like a fruit a soda and candy help our character out so they’re going to
give them positive energy so as you can see we have a file with a name and the effect
that it has on our character’s energy so we’re going to be building a program just to print
out these different types of items and groups for example we’re going to have the user press
one if they want to print out all the plain items like a shoe a pencil and chair press
two if they want to print out the helpful items like fruit and soda and candy and press
three if they want to print out the harmful items like a ninja meth and a dirty needle
now we’ll have them press four if they just want to quit the program altogether and you
know go watch YouTube videos so that’s the basics of what we’re going to be doing we’re
going to be incorporating a program with a file and it’s going to work beautifully together
but like I said it’s going to take more than one video so just be prepared for that so
let’s go ahead and start coding this baby right meow so the first thing we want to do
is we want to give them a menu to choose from now this menu is going to be one two three
four depending on what they press we’re going to output the appropriate items so we’re going
to want to store this variable either 1 2 3 or 4 in a variable so just go ahead and
make will store this number and variable named what they want now this will be equal to one
two three or four so now what we can do is we can just take that variable what they want
and we’re going to set it equal to a function in order you know to get the number from them
so we’ll just name this function get what they want now we didn’t build this function
yet and this obviously isn’t a built in function on C++ so we need to go ahead and build it
right now so what I like to do actually is whenever I’m building a big program that’s
going to be working with a lot of functions above each function well I’ll just do with
main first I like to leave a comment just the function name like a main function and
you guys may be like alright why the heck do you want to do that well if you notice
the comments are a different color and it makes it really easy to find functions whenever
you’re scrolling through like three hundred three thousand lines of code so you guys might
be like alright that’s kind of dumb but trust me do it it’s going to save you a whole bunch
of headaches in the future so we’re going to be building this get what they want function
so the first thing I’m going to do is get what they want function and I probably spelled
it wrong yep but you know good enough so now remember this function has to return an integer
that’s going to be either 1 2 3 or 4 so our turn type is going to be integer now the name
is get what they want so just go ahead and copy that and paste that and it’s not going
to take any parameters we don’t have to give any information into it we just want to get
information back from it so that’s why it’s not going to take any parameters so now what
we want to do is we need a variable to temporarily store 1 2 3 or 4 so we got to make an int
variable and we’ll just name this choice name or whatever you can name it X if you want
really doesn’t matter so now what we want to do is we to output the their choices for
them so you know they know whether it enter one two or three or four so just go ahead
and make four different see out statements we can make it all in one statement but it
would probably look cleaner if it’s just four different statements so in the first one we’ll
just put enter one for just plain items and you probably want to put each of these on
a new line so just copy this three more times copy and we’ll go ahead and write enter 2
4 2 can be let’s say helpful items and this will be the you know candy the soda is that
really helpful for a kid I don’t think so but in this game it is and three will be for
the harmful items and this will be like the the ninja their dirty needles and the website
put oh yeah meth definitely harmful for a kid and four will just be like quit program
so whenever they press for the program is going to quit so now what we want to do is
we want to allow them to input of course one of those numbers so CIN that’s how they get
information from the keyboard and whatever number they enter it’s going to be stored
in the variable choice now all we need to do is take that variable choice and return
it in our function so whenever we return choice it’s going to store that number and what they
want so now this could what they want function basically gets a number from from the keyboard
and it stores it in a variable what they want so now we have a variable either 1 2 3 or
4 but we didn’t do anything with that variable yet and what we want to do is since this program
is going to keep running until they hit 4 we want to throw pretty much our entire program
inside a while loop and this while loop is basically going to say ok keep running this
program until they hit the number 4 so we’re going to say all right while that what they
want variable is not equal to the number 4 keep running this program and of course at
the end of this at the end of this while loop well let me think actually no okay I’ll save
that for later on but basically what I’m going to be doing is this I guess I can throw it
in right now at the end of this while loop we want to give them another opportunity to
you know get what they want to enter another number so go ahead and copy this and well
I guess I can show you guys what’s going on right now basically this program is going
to run and I didn’t prototype it always remember to prototype your functions ladies and gentlemen
and whenever your prototype something just copy the header and paste it at the very top
and that way whenever your program comes across it like it did right there um it’s going to
know what it is so let me show you guys what’s going on so far well the very first thing
that we did is we ran this program and it called up this function right here now once
it got that function it pretty much took a number from one to three or four and stored
it in the variable what they want and we said all right keep calling this function in making
equal to store to what they want until they enter four so whenever they run this program
they’re going to be able to enter one it’s going to do something too it’s going to do
something three it’s going to do something they can enter three 100 times if they want
but as soon as they enter for the while loop is going to end and the program is going to
quit just like that so we have the basic shell of the program right now we basically are
getting a number from them and we’re going to keep getting a number from until they enter
four so now with the basic shell done the only thing that we need to do now is whenever
they enter one handle that one appropriately and show all the plain objects whenever they
enter to handle that appropriately and show all the helpful items whenever it ends will
enter three show all the items that are harmful to the character in now that we now have the
basic shell we can move on to that but we can in this story because if I do I’m going
to run out of time so copy all this code down make sure you know how this menu function
works and also make sure you understand why how we keep looping through this function
until they enter the number four and by the way as soon as they enter number four this
while loop is going to be false and it quits and as you can see whenever it quits there’s
no more quote there is no more code after it so the main function ends in our program
stops so once you guys understand that you’re ready to move on to the next tutorial so thank
you guys for watching don’t forget subscribe and I’ll see you guys in the next video
alright guys so hopefully you watch my last tutorial number 68 and now you’re watching
the 69 tutorial and in this tutorial we’re going to be finishing up the program that
well I don’t know if we’re going to be finishing it but we’re going to be continuing the program
that we started in the last oriole so in the last tutorial what we did is we basically
built the main functionality for our program the only thing that we didn’t do is handle
the other items except for four so I already told you guys that what this loop does is
it tests this variable what they want and if it’s equal to one it should print out the
plain items if it’s equal to two it should print out the helpful items if they enter
to the number three then they should print out the items that are harmful to the character
so what we want to do is we want to test this what they want variable for one two or three
now what we can do is make a bunch of different if statements but remember I told you guys
whenever you’re testing a single variable for multiple values it’s better to use a switch
statement now a switch statement takes one parameter the variable what they want and
it tests it a bunch of against a bunch of different cases so let’s go ahead and finish
this by we need the curly braces right here and inside the curly braces of the switch
statement we write all those different cases so we go ahead and we first write in the case
of one what do we want to do and we’re going to be wanting to display these items and we
can write all the code right here but since we’re going to be displaying the items multiple
times it’s better to build a function later on so let’s go ahead and just again I didn’t
build this function yet we’re going to be building it later on but let’s go ahead and
build a function called display items and whenever we enter one we’re going to pass
in the parameter one now display items like I said is going to be a function that takes
one parameter depending on what parameter it takes it knows what items the print out
so whenever we display items with the parameter one it’s going to print out the plain items
so of course remember we add break after every case so it doesn’t go to the rest of the cases
and let me just go ahead and copy this two more times so in the case of two and in the
case of three so if what they want is equal to two then we want to display items with
the parameter two if they enter three that means that they want to print out the harmful
items so we’ll go ahead and write a function display items that takes the parameter three
now check it out this main function is done completely we don’t have to do anything else
to this main function the only thing we have to do is build this display items function
pretty cool huh so let me go ahead and come down here and the first thing like I said
that I like to do whenever I’m building a function is add a comment now I’ll just go
ahead and put display items function now this function it’s only going to print out stuff
on the screen that’s his only job to display items so let’s go ahead and make a void as
a return type we’re not getting any information back it’s just going to print out something
it’s just going to you know do the dirty work for us so it’s void we’re not asking for anything
back in return we’re just asking it to do something so now let’s go ahead and write
display items and of course remember it needs to know what items you want one two or three
it basically needs to know what choice did you enter one two or three so we’re going
to be passing it in an integer parameter just like that now well the first thing I want
to do before I continue is go ahead and prototype my function and remember to prototype your
function one last time go ahead and copy the header and paste it right above your main
and the reason we prototype functions one more time is because whenever we come across
it right here in our main program it knows that it’s a function that we made later on
right down there hopefully you guys watch my series before and you should know that
but anyways let’s go ahead and build this display items function so the very first thing
that we need to do is we need to have access to that file where all the items are and remember
this is the file right here with all the items in in you know whatever this is the effect
of energy on our character or whatever so remember we already know how to open a file
to gain access to it and that’s through an if stream object and by the way before I even
finish typing that make sure you have included your F stream this is pretty much the header
file that allows you have access to the files so in order to read information from a file
we need to make an if stream object and just go ahead and name your object you know objects
file by the way I named this file objects so that’s why I’m in this object file and
of course we can put this on two different lines but it takes the optional constructor
of what file do you want to associate this object to and we want to associate this object
with a objects dot txt file that’s the text file that I just showed you guys now after
this we need to make two variables the first variable is going to store the name of the
object and the second variable is going to store you know the power or the energy whatever
you want to call this thing I’ll call it power so the first variable is going to be a string
variable and it’s going to store the name of the object the next variable is going to
be a double variable and this is going to store the power of the object so now what
we can do is the first thing we want to test is what choice did they enter so we’re going
to be making a bunch of different if statements we can make a switch statement but I think
if will look a little bit neater so we’ll say all right if the choice they entered which
is X is equal to 1 then check it out that means that they wanted to display the plain
items in other words they want to display the items that has zero effect on our character
so if the choice they entered is equal to 1 what do we want to do well we want to go
ahead and first here’s what we’re going to do we’re going to loop through each object
we’re going to loop through this one this one then this one then this one then this
one each time we loop through we’re going to want to check if the power is equal to
zero if the power is equal to zero then we’re going to want to go ahead and print it out
so it’s going to say all right loop through this one print it out loop through this one
don’t print it out don’t print it out print it out print it out um so that’s what it’s
basically going to do so the first thing we need to do is loop through each object and
we in order to do that if you remember from let’s see I want to say two tutorials ago
we can just go ahead and make a while loop and in this while loop just go ahead and write
come on there we go I was waiting for it to pop up go ahead and write your object file
and then go ahead and we’re going to store the first thing and name and we’re going to
store the second thing in power and this is going to loop through as long as your file
is open so it’s going to say all right loop through the first thing stored a name and
store it in power so now what we want to do is we want to check all right whenever you
loop through an item and the power is equal to just go ahead I don’t want to add that
space if the power is equal to zero then go ahead and print it out so see out name of
the object and then we’ll just go ahead and add a little space and then the power and
then we’ll go ahead and end that line so basically we’re going to be looping through this whole
file whenever it gets to item where the power is equal to zero print it out on the screen
now let’s go ahead and copy this two more times you never say all right remember X is
the choice that they entered so if they enter the choice of number two then they want to
print out the helpful items just like that so whenever they want to print out the helpful
items we know that the power must be greater than zero because remember a helpful item
is like a candy or a soda where the power is greater than zero so what this is going
to do is whenever they enter the choice to print out the items that have a power greater
than zero and whenever they enter the choice 3 print out the items that have power of less
than zero and these items are you know a ninja meth and dirty needle all those items that
are harmful items have a power of less than zero right like that so basically our program
is complete this is ready to run and I know you guys are just itching for me to press
this build and run button and see this program go to work but I’m actually going to be doing
that in the next tutorial because first I want you guys to watch one of my other videos
kind of sucker you into that one and also I’m going to be showing you guys that it does
indeed run perfectly I actually in between these videos I’m going to check for errors
and make sure I didn’t mess up anything and I’m also going to be talking through you guys
one more time to clear up any you know confusing things that or any questions any guys might
have so thank you guys for watching I cannot wait to press this pun so I guess you guys
are going to have to see me press it in the next tutorial so thank you guys don’t forget
subscribe and I’ll see you guys next tutorial alright guys welcome back to your 70th tutorial
and this story really isn’t tutorial at all we’re just going to be running the program
that we built in the last two tutorials and I actually did check for errors in the last
video and this program is built perfectly who woulda guessed and so it’s ready to run
so let me go ahead and press this build and run and show you guys how it works so the
very first thing it does is pop up a menu and it gives us a choice press one to print
out plain items to four helpful items press three to print out harmful items and whenever
you want to create the program press four so let’s go ahead and print out one and this
is going to print out the items that have no effect on our character so press one go
ahead and hit enter and it’s going to print out chair a shoe and a pencil all with zero
effect on our character so now let’s go ahead and let’s get down you know three or something
hit three enter and it’s going to print out ninja meth and a dirty needle so now let’s
go ahead and print two and whenever we hit two to print out the helpful items is going
to print out fruit candy and soda and now let’s go ahead and press four to quit the
program and our program is going to end with execution time of 49 seconds how awesome is
that so one thing that I want to do because it’s just going to annoy me is I want to add
a new line right there and right there so now whenever you run the program it’s going
to you know it just makes everything not bunched up in one line so if you go ahead and hit
two you can see that you know it makes it a little bit better so again there’s a lot
of different formatting things that we can do but you know I just want to make sure it
wasn’t bunched together so to print out all the helpful items um if I go ahead and you
know hit three it will print out are the harmful items yada yada yada so basically I wanted
to show you guys that this program does indeed work I wasn’t biessing you and this is how
you can build a program that works awesomely with a file in this case the object file or
the objects text file so let me guys let me talk you through how this program works one
last time the very first thing I want to mention is whenever you’re building a program takes
a bunch of different functions make sure to prototype your functions first and foremost
if you don’t prototype your functions like this and try to run it you’re going to get
an error message because it’s going to come across this function and it’s not going to
understand what it means so that’s why you need to prototype your function so once you
have your functions prototype let me run this get that error message out of the way now
that you have your function prototype I’ll tell you guys how this program works the very
first thing we wanted to do was display a menu to the user and we wanted them to enter
a number 1 2 3 or 4 depending on their menu choice so what we did is we built a function
to display that menu name get what they want and we took whatever they entered and we stored
it in variable called choice and what this did function did is it returned that variable
choice so it basically stored that number either 1 2 3 or 4 in the variable what they
want because this number tells us what they want so after that we took that number and
we first made sure it wasn’t equal to 4 because remember 4 means they want to quit the program
so whenever they entered for this while loop ended in the program quit so what this wallet
did is said alright if they enter any number besides 4 go ahead and run this code right
here because that means that they want something if they enter 1 it means they want plain items
2 helpful items 3 harmful items so I said alright test this variable if they enter 1
2 or 3 if they enter 1 call this function in display items with the parameter 1 if they
enter 2 it means they want helpful items so we’re going to call display helpful items
and if they enter 3 we’re going to pass in 3 on this display items function so you’re
saying all right so the display item function obviously displays the proper items to them
so say we passed it the variable 2 whoa it calls the display items function with a parameter
to it basically Scrolls down here and cause display items now remember display items it
just prints out something on the screen it doesn’t give us any integers or strings back
so we made that void now whenever we pass then 1 2 or 3 it treated that variable as
X so X was pretty much your choice 1 2 or 3 so the very first thing that we did in this
function is we open that file because I mean all the crap that we needed was inside this
file so we open that file and then we made two variables we made a name variable to store
the name of the object and we made a power variable to store how much power that object
had whether it’s 0 negative or positive we then tested what choice they wanted remember
if X which is now the choice is equal to 1 then it means they want to display the items
with no power the plain items whenever they entered the number 2 it means they wanted
to display the helpful items and helpful items just had a Power greater than 0 so that’s
why we printed out the objects with the power greater than 0 whenever they entered the number
3 it means they wanted to display the harmful items the items that hurt our character so
said all right we’re going to test this power variable and any object with a power variable
that’s less than 0 go ahead and print it out so that is how our program works and here’s
what I’m going to ask one of you guys to do you guys probably want all the source code
for this so actually what I’m going to do is I’m going to go ahead and copy this and
post it in my forum under the c++ section so if you guys want just go ahead and sign
up actually don’t even need to sign up for my form but I would prefer you did sign up
for my form go look in the C++ section and I’ll post all the source code here so if you
guys you know are getting an error in your program whenever you’re working along you
can just go ahead and copy my source code and it’s all there for you guys so thank you
guys for watching so much hopefully you understand how this program works if you don’t understand
then don’t flip out all you need to do is go ahead look at the source code and once
you see it all together um you guys can understand how it’s all pieced together so again one
last time this is how you build a program that works perfectly with a file just like
this and with that being said I want to say this a lot of well in this tutorial I showed
you guys how to have the user you know input a number and print out all the helpful items
or harmful items but in whenever you’re developing a game instead of just printing it out on
the screen you would want you know whenever your user comes across a helpful item do you
know make them get that much energy or whenever your character comes across a harmful item
have them you know take that much energy away from your character or whenever your character
comes across than item like a building or a chair or a shoe it has no effect on the
character so aside from printing it out we would do something different in this line
of code right here but that’s basically um your very first glance at how games and programs
work of files in a useful program so I’m glad I could teach this tutorial finally make a
program that’s useful instead of teaching guys just the core concepts I know a lot of
you guys were complaining that I was just teaching the concepts and you guys never really
got to see how a program was put together so hopefully here’s a nice little look at
how it works so thank you guys for watching one last time thank you guys so much for subscribing
and I’m excited to move on to the next topic so hopefully we’re done working on files at
least for now so thank you guys for watching and I’ll see you guys in next video
what is going on guys welcome to your let’s see seventy first C++ tutorial and in these
next I want to say three or four tutorials I’m going to be covering more about strings
now I know that I touched a little bit about strings in we’ve actually been using strings
this whole series but I want to talk to you guys specifically about the string class now
the cool thing about these next couple tutorials is there aren’t a lot of confusing concepts
that you guys need to understand but the string class gives us a bunch of functions that well
let me tell you guys this whenever you’re building a program and you don’t know how
to do something that you usually want to look it up in the documentation and if there’s
you know an advanced function in order to do something it will tell you how to do it
but some functions like how to print stuff out on the screen are essential to have in
your memory and a lot of those functions that are essential to have in your memory are a
lot of the string functions so this is stuff that whenever your program you shouldn’t have
to look up so in these next couple tutorials I’m going to be teaching guys the core string
functions so the good thing is they aren’t confusing they take like a minute each to
learn the sucky thing is that we do in fact need to learn these because if you don’t know
them if you do know them it’s just going to make program a whole lot easier so the first
thing I want to clear up is probably something I should have touched on a long time ago and
that’s also something that kind of confuses you guys whenever I was working with files
you guys didn’t really understand that whenever I came to you know a white space that whenever
I was like reading data in from a file then and automatically quit reading it in and let
me explain you guys what I’m talking about sit whoa Oh easy calf socks all I got cough
oh there I go got some milkshake from McDonald’s in my throat so say we have a string called
Bucky and we want to you know fill in the string with the users data whatever the user
enters from the keyboard we want to store in this variable called Bucky so we use the
CIN and whatever the enter is going to be stored in variable called Bucky so now we’ll
go ahead and just print out something on the screen like the string I entered is and then
we’ll make a little space right there and then we’ll just go ahead and write the variable
name which was Bucky and in that line well let’s go ahead and run this program and I
want to clear up a couple things so now we see a blank cursor telling us to input something
and we already know that if we input something like tuna it says the string I entered is
tuna we understand that much but what I didn’t tell you guys and I probably should have told
you guys this in like the tenth tutorial but you know better late than never is whenever
you enter something like tuna sandwiches are good and you say all right well that’s my
string of text and you press Enter it only says the string I entered is tuna and that
brings me to a lot of a point that I should have covered in either the 10th tutorial in
the last tutorial when I was teaching you guys how data is read in from a file whenever
you using cin to read data in whether it’s from the keyboard the user or a file when
you’re using c i end the end of the input is determined by the first whitespace character
it comes across so for example whenever you run this program and you see tuna sandwiches
it doesn’t read that whole tuna sandwiches it reads it until it gets to it first whitespace
so that’s why in the last tutorial whenever we came across you know a newline character
or a whitespace that’s when it stopped and that’s when it finished reading in the data
so hopefully you guys understand cin only reads one small piece of data at a time so
now that you guys understand that concept let me talk to you guys about another concept
and that is well what happens when you do want to read an entire line as one string
well say we go ahead and lesson name our string something else besides Buckey let’s name it
string X whenever you want to read an entire line of data here’s what you do you go ahead
and write the function get line and go ahead and write CIN and just go ahead and write
X and now let’s just go ahead and you know print this out on screen real quick and line
and now what get line does is instead of the end of input being determined by a whitespace
character like a space the end of the input is determined by a new line character so get
line is going to wait till we have a new line in other words when you’re working with cin
which means input from keyboard it’s going to wait until we press Enter or basically
when we’re done typing text on the line so now when we put tuna sandwiches are good and
press Enter it’s going to output tuna sandwiches are good and this entire third thing is the
string that’s going to be stored in X so again what I want to stress is whenever you’re working
explicitly with CIN it reads one small piece of data whenever you want to read an entire
line you want to use get line sorry I still got that milkshake stuck in my throat so that
is the difference between CIN and get line and how the end of the input is determined
by a white space or a newline character so that’s not only going to help you a lot when
you know getting information from the user but also whenever you’re working with files
that’s how it read the information I probably should have told you guys that before but
hey like I said better late than other never there you go okay let’s slow down for a second
Bucky and I gather your thoughts and all right I’m gonna go now so now I want to talk to
you guys about additional ways to create and copy strings so aside from having you know
string Bucky equals Yaya oops yada yada yada and while this is a valid then this is the
way we created strings before I want to tell you guys about an additional way to create
strings from the string class say we want to create a string named s1 we can go ahead
and pass it in a constructor of a string like hampster just like that and now hamster is
going to be equal to s1 again like I said just two different ways of doing the same
thing but why do we need to know this because whenever we’re working at a company and someone
creates a string this way we don’t you know think it’s something totally different or
some class that they made up we know that you know don’t freak out it’s just the same
way of creating a string so if we want to create a blank string we can do something
like this and let me go ahead and create another blank string because I’m going to be showing
you guys an example later on so anytime you want to copy a string you can do so in one
of two ways the first way which you guys probably could guess is you can set string two equal
to string 1 which is hamster now this is a valid way of copying the string but some people
also want to use a function from the string class and that’s called assign now assign
isn’t really a good word they probably should just named it copy but it does the exact same
thing and that say you want to have s3 and you want to have this you know actually we
have it right there it already pops up for us assign string one these two go with you
know with exception of being string two and shrink three these do the identical thing
each of these functions copy this string to this variable right here so again just to
prove it to you guys let me just print out all these on the screen Oh print s1 s2 and
s3 and just go ahead and end that line and whenever we print it out we can see that all
these things hamster hamster hamster do the exact same thing so again what this line of
code does is it’s just an additional way where we can go through the string class and we
can create a string using the constructor and what this does right here this assign
is basically an alternative to copying strings so with that being said um I want to talk
about one last thing and hey I guess again okay the last thing i want to talk about in
this tutorial and i’m probably going to go over 10 minutes and i hate making videos over
10 minutes what i do is strings are arrays of characters they’re not like arrays of characters
that are not similar similar to arrays of characters strings are indeed arrays of characters
so let me go ahead and make a string called s1 and i’ll set it equal to oh em gee WTF
BBQ good enough so now i want to stress that anything you can do to an array you can do
to a string so let’s go ahead and see out something like s1 and say we wanted to output
one of these characters and these characters are elements in the string 0 1 2 3 so this
w is s1 3 well in addition to you know having the explicit index the string class also has
an ant method to say alright if we put 3 right there it’s going to look in the third element
in the array and it’s going to find what character is at that location so it’s you know a little
easier to tell to someone who’s like just learning programming using the app function
is a little easier to see then you know using the array indexes right there so let’s just
go ahead and end that line and print out and whenever we print it out we can see that we
indeed get a W so the point of this lesson is I want to stress you guys that strings
are arrays of characters and one other cool thing I might as well teach you guys while
I have the time is just like an array where we can loop through it and print out each
one of its indexes we can also loop through a string so let me go ahead and put int x
equals 0 and in order to get the length of a string it has built-in length function so
put X is less than oh what’s my street name s1 dot length with no parameters and we’ll
just loop through them one at a time using X plus plus so now what we need to do is just
print out on the screen s1 dot at and as a parameter we can just go ahead and type in
X because what X is going to do is it’s going to say all right it’s going to start at 0
remember this is the 0th element in the array and is going to count the length of the string
and it’s going to loop basically from the beginning till the end and the X is going
to go 0 1 2 3 4 all the way until it reaches this as last element so that way whenever
we pass X into the parameter for at instead of printing just one character it’s going
to print the entire string so let’s go ahead and build and run this and check it out here’s
what we printed from before from the last lesson and now here’s the entire string getting
printed out OMG WTF BBQ so those are some simple string functions like I said before
none of these are going to you know twist your brain like polymorphism or maybe working
with files are but there’s some core functions that we need to learn especially the at the
length those are two string functions that you’re going to see a lot whenever you’re
working for a company hopefully you’re watching this tutorials in the hopes of you know getting
a job or at least working with other people on a game project or something like that you’re
going to see these functions a lot so you need to know what they are how they work and
also some additional ways of creating copying strings so now that you understand these concepts
hopefully you memorize them embed them in your brain I want to tell you guys that there
are a lot of different things that the string class has to offer aside from you know finding
the length of a string so we’re going to be covering all those coarse string concepts
in the next couple tutorials so have no fear no really confusing topics for now and I want
to tell you guys that you’re almost to the end of learning beginning C++ we probably
only got like 20 more videos to go so with that looking forward to I want to say guys
thank you for subscribing thank you for watching my videos and I will see you guys in the next
video alright guys welcome to 72nd well we’ve come
a long way c++ tutorial and in this tutorial i’m going to be talking to you guys about
some more very useful string functions so the very first string function i want to talk
to you guys about is called the substring function now what the substring function does
is it takes a big string and it creates a substring out of you know a small piece of
it so let me go ahead and show you guys an example it’s going to be a lot easier to show
you example rather than try and explain it so go ahead and make a string i’m cuming call
actually i’m probably going to call all my strings s1 in this story and just go ahead
and write something like OMG i think i am Craigie or something stupid like that i don’t
know create a new string you want and go ahead and print out s1 dot s ub STR this is a sub
string function and go ahead and outline and L now the substring function takes two parameters
the first parameter it takes is the beginning index or the beginning character remember
strings are arrays is pretty much an array of characters so this is 0 1 2 3 4 so on and
so forth so let me go ahead and put the beginning character which is 17 and the next parameter
it takes is how many characters do you want to go on for so if we put 1 5 it would start
at 1 and it would go 5 characters in so let’s go ahead and put 17 7 go ahead and run this
and we get the word pretty with explanation point 1 2 3 4 5 6 7 actually 0 1 2 3 4 5 6
I don’t know it’s one or the other but anyways the substring method is basically what character
do you want to begin in and this is the seventeenth character and if we count seven characters
one two three four five six seven is how many characters is included in the substring so
basically the substring function is how to create a smaller string from a bigger string
definitely useful whenever well whenever you want to create a smaller string from a bigger
string now after this I want to teach you guys about another function and that’s called
the swap function now this is might be if you’re working a lot with sorting lists or
how to sort arrays you’re going to be using this function a ton what it basically does
is it takes one string and swaps it with another string so let’s go ahead and put string one
and we’ll make this one make it something that begins with a a like apples and put string
two and what this one does is we’ll make this one like beans good enough and what we can
do first is just print out the strings to verify that they are indeed working correctly
so one and then we’ll print out two and then go ahead and in that line so what this line
is going to print out apples beans and I’ll add a space after this and now what we can
do is I want to show you guys what the swap function is and how it works the first thing
you need is a string to apply it to and I’m going to apply it to shrink one now go ahead
and call swap on this now it always needs to take a parameter and that’s what string
do you want to swap with it and we want to swap shrink two with it so basically saying
are a swap beans with apples and now string 1 contains beans and string 2 contains apples
so if we go ahead and print this out again let me just go ahead and copy it and print
it out again in build and run the original strings or apples and beans but now it took
the contents or values of them and flip-flop them so now string 1 contains beans and string
2 contains apples again this is very useful whenever you’re sorting usually you’re going
to run through a huge while loop and you’re going to back all right if this value is greater
than this value then swap them if not leave them the same and by the end by the time you
get to the end of the loop everything’s in a nice neat order so that’s your basic of
you know how to sort things but again let’s go ahead and delete this and again one last
time none of these things are really confusing but they’re definite functions that we need
to know that’s why I’m going to be covering this explicitly because these are functions
that you should definitely shouldn’t have to look up in a you know resource or reference
guide these should be function that should be stuck in your brain so the last function
I want to cover the in this tutorial is the find function now like I said I want to repeat
this one last time strings are just arrays of characters so whenever we’re finding a
substring what it’s going to do is it’s going to give us the location or in other words
the index of where in the string that is so let me go ahead and make a string again most
of these it’s going to be a lot easier just to see an example so let me go ahead and make
something like Hamm is spam oh yes I am and I actually made that string for a very specific
reason what you will see you later on so go ahead and see out s1 dot find a m and now
let me go ahead and in that line and you’re saying all right so I kind of know what this
is going to do already it’s going to go and look through your string and find where a.m.
is but check it out we have this one right here and this one right here and this one
right here so let’s go ahead and build and run this and see what when it returns and
I forgot to do something and add a lesson and sign right there so let’s go ahead and
build around this again and it’s going to say one so say an R right so obviously it
gave me the location of this first one right here because this is the 0th index of this
array and this is one so what it basically does is it gives you the starting location
of the first found arm first found a.m. I guess that’s all I need to say the first found
occurrence that’s what I was looking for so even though there are three occurrences in
this string it’s going to loop through and it’s only return to you the first occurrence
of this so remember that whenever using fine it doesn’t give you know 1 and 10 and 19 or
whatever the heck these are so saying all right well what if I want the occurrence of
the last a.m. well there’s another function for that and that’s called our find this stands
for reverse find you know how whenever you’re finding you know a substring such as a.m.
and a bigger string is going to start at the beginning and it’s going to work a left to
right well what reverse fine does is it works right to left just like that and it’s going
to go right to left and it’s going to look for a.m. so it’s basically going to give you
the last occurrence of a.m. which let’s go ahead and print this out and that is 21 so
if we can see this is 1 2 3 4 5 6 7 8 9 10 11 12 3456 10 21 right like that as you can
see this 21st index in other words this one right here is the first location of the last
a.m. so that’s the difference between fine and our fine one works left to right and one
works right the left so there are some more string functions right there again these string
functions our function is that you should not have to look up these are some functions
that should be embedded in your programming brain so that’s why I want to give them to
you guys and hopefully me showing you an example of how they work help you know make them stick
in your brain a little bit easier so thank you guys for watching these stories we only
have a few more string functions to cover but don’t skip over these tutorials because
later on when I’m developing games and showing you guys how to work with 3d and cool stuff
like that I’m not going to be taking time to stop and pause and cover these functions
so if you know you decide to skip these next tutorials and just go straight to game development
or whatever you’re going to be crap out of luck so there you go thank you guys for watching
don’t forget subscribe don’t forget to check out all my other videos and yeah I guess I’ll
see you guys later what’s up guys welcome to your 73rd C++ tutorial
and in this tutorial you will be happy to know we are covering the last string functions
so after this tutorial no more string functions for a while so I bet you guys are glad about
that so the first thing I’m going to be covering is the string erase function now what this
function does it it erases everything from and including whatever element you tell it
to so let’s go ahead and string I’m just going to make a shrink out string one and all right
something like hi my name is Bucky and I love bacon and ham something stupid like that and
why not explanation point you know spice things up a bit so let’s go ahead and first just
print this out on the screen so we have something to compare it to after we change it later
on so now what I want to do is I want to let’s say I want to erase let’s see I want to erase
everything from the xx character on so it’s going to be like somewhere around here and
say this is the xx character right here I want to erase everything from here till the
end of the string well we need to use something called the erase function and it’s probably
already in there right there erase and it takes one parameter what character do you
want to begin erasing that erasing it and remember it includes the xx character as well
so it doesn’t start at 21 and actually erases xx character as well so now let’s go ahead
and print out the new-and-improved erased string and we’ll go ahead and run it and see
what it looks like so the original string is hi my name is Bucky and I’ll a bacon in
ham and the new string is hi my name is Bucky and then erased everything from here on out
so that’s all the erase function does so now aside from the erase function I guess we can
go ahead and keep this and this and this the only line of code that’s going to change in
this story is this so now that we know what the erase function is I want to talk you guys
about another function and that’s the replace function so press s1 replace functions already
filling in for me all right the replace function takes three parameters the first parameter
it takes is what character do you want to begin replacing it and Bucky I already know
is the 14th character since this was the 20th character right here so this B is the 14th
index the next parameter so let’s go ahead and put 14 in there right now before I forget
the next parameter it takes is how many characters do you want to replace so let’s count one
two three four five Bucky is five characters long so we want if we want to erase this entire
thing or in other words replace it we’re going to start at the 14th character and replace
five characters so that’s going to replace the entire word Bucky so the third parameter
it takes is what do you want to replace it with so now that we have Bucky replaced I
think it would be only appropriate to you know add a new name like arm I was just going
to enter right there that’d be way too easy like Samuel Jackson and I spelled that wrong
but it doesn’t really matter so you don’t have to replace it with the same number of
characters you’re pretty much saying all right what do you want to replace and what do you
want to replace it with uh so if we go ahead and run this we can see that the original
string was hi my name is Bucky and I love bacon and ham and now the new string took
Bucky out and added Samuel Jackson so it says hi my name is Samuel Jackson I love bacon
in him and he probably does so what replace does is basically takes a certain bit out
of the string and replaces it with something else easy enough so now I want to talk to
you guys about one last string function and that’s insert now what insert does is it inserts
a substring into the string but it doesn’t take anything out like it did Bucky before
so if we wanted to change the strength – hi my name is lucky Bucky and I’ll a bacon ham
we don’t want to take anything out we just want to insert something right after is so
what will you do is put s1 as string we’re working with the function is called insert
right like that and it takes two parameters the first parameter it takes is where do you
want to begin inserting it well we already know 14 is the beginning of Bucky and the
next parameter it takes is what string do you want to put right there what substring
do you want to add into the main string and just go ahead and press lucky and we’ll add
a space after lucky so it would be lucky space Bucky if we didn’t add this space it would
be like lucky Bucky one big word but I want my name is lucky Bucky so I’m going to begin
at index number four and I want to insert the word lucky with a space after it so don’t
forget your semicolon and whenever you run this you can see the original string hi my
name is Bucky and now we inserted lucky so the new string is hi my name is lucky Bucky
and I love bacon and ham so that is how you insert words you know into a string and one
other thing I forgot to mention let’s see if I can go ahead and ZZ replace just like
that usually people use replace whenever they they usually use it in conjunction with fine
so remember in the lesser tutorial i told you about find how you can find like all the
AMS in a string I think that was an example I showed you guys people typically loop through
your entire document and find all instances of something and use this replace function
to replace it with this for example here’s a great example if you ever use Microsoft
Word and it saw that you know you did your whole homework assignment and you saw you
spelled the word dictionary with a k’ and you typed it like 18 times well whenever you
right click it and spell check it it says replace all this is how they made it it just
loops through finds all the time you take dictionary weather okay and it replaces it
with a proper spelling of dictionary which of course doesn’t have a K in it so if you’re
ever wondering how Microsoft coded that fine and replace these are the methods they used
to do it so if you think you know you know Microsoft is a bunch of genius programmers
in a program in ways that you can never even learn well that’s one of the techniques we
already learned that we just you know learn the concept of it instead of applying it in
the main program ball they did is they use find if they loop through your entire document
found all the instances and each time they found it they called this replace and replace
it with a proper string so there you go there’s a you know nice low example of when this would
be useful in real life so with that being said that’s all this well I don’t want to
say that’s all the shrink functions are probably like 200 string functions but those are all
the main ones that I want to cover now that we covered all the main string functions whenever
your program in games and documents and applications later on and we come across you know a string
function we don’t have to look up what it means again there are a bunch of different
other string functions but these are the main ones that are going to come across in your
programming so that being said take take a deep breath and we only got a couple more
topics to cover so I am ready for it and to be honest I’m getting kind of tired of learning
C++ I’m ready to move on to game development with C++ and 3d modeling we C++ in the fun
stuff but hey we got to cover the born stuff before we get the fun stuff those are the
rules of life so thank you guys so much for watching and thank you guys for sticking with
me for what was it like 73 tutorials already I had super fun I had super you know I’m just
going to quit talking right now before I mess up anything else but anyways thank you guys
for watching and I’ll see you guys in the next video

100 thoughts on “C++ Tutorial From Basic to Advance”

  1. So u know how in games they give a default name and u can change it if u want is that what constructors do?

  2. 7:37:12 for beginners, Bucky said he should have mentioned this in his second tutorial, so check it out! He talks about the << and >> operators.

  3. Tutorial timestamp (slightly modified):
    0:02 Installing CodeBlocks
    7:56 Simple C++ Program
    17:45 Printing Text
    23:35 Variables
    29:03 Basic Calculator
    36:32 variable memory
    40:12 Basic Arithmetic
    46:10 If
    53:25 Function
    1:02:29 Creating functions with parameters
    1:07:55 Functions with multiple parameters
    1:14:17 Classes and Objects
    1:22:43 Variables in Classes
    1:31:56 Constructor
    1:40:04 placing classes in diff. files
    1:49:26 If statement
    1:57:26 If else statement
    2:04:28 while loops
    2:10:38 program using loops
    2:18:20 Sentinel controlled loops
    2:28:01 Assignment & Increment operator
    2:33:59 For loops
    2:38:58 stock market simulator
    2:45:41 Do- while loop
    2:49:45 switch
    2:57:09 Logical Operator
    3:04:30 Random Number Generator
    3:14:23 Default arguments
    3:19:59 Scope resolution operator
    3:26:53 Function Overloading
    3:33:14 Recursion
    3:41:33 Arrays
    3:48:14 Arrays with Loops
    3:54:35 Arrays Calculation
    3:59:31 Array as argument
    4:07:30 MD Array
    4:19:59 Pointers
    4:26:07 Pass by Reference
    4:35:46 size of
    4:40:54 pointer math’s
    4:46:59 Indirection Operator
    4:55:01 Deconstructor
    5:01:01 Constant Object
    5:08:01 Member Initialization list
    5:16:01 Composition
    5:25:01 Composition continue
    5:34:01 Friend Function
    5:40:01 This pointer
    5:50:01 Operator Overloading
    5:55:01 Operator
    6:01:01 Inheritance
    6:09:01 Protected Members
    6:17:01 Constructor & Deconstructor Derive C
    6:27:01 Polymorphism
    6:37:01 virtual Function
    6:47:01 Abstract & pure virtual
    6:54:01 Function Template
    7:03:01 Template with parameters
    7:10:01 Class template
    7:20:01 Template specialization
    7:29:01 Exception handling
    7:36:01 Exception
    7:44:01 Files
    7:52:01 files
    7:59:01 writing Custom file structure
    8:08:01 reading Custom file structure
    8:18:01 file program
    8:27:01 file program 2
    8:37:01 file program 3
    8:45:01 String Class
    8:57:01 String Class 1
    9:05:01 String Class 2

  4. I started watching now and its been 30 min.For this 30 min i have learned one thing and that is Bucky loves bacon <3

  5. #include <iostream>
    using namespace std ;
    int main() {
    char P;
    char r;
    char e;
    char t;
    char t;
    char y;
    char C;
    char o;
    char o;
    char l;
    char H;
    char u;
    char H;
    char !;

  6. Thanks for this course, but it would be good to clarify something … c is not a totally different language from C++ …. C++ was originally called c with classes … check this interview out where the creator of C++, Stroustrup (you should mention it) explains where C++ came from … very interesting … https://youtu.be/JBjjnqG0BP8

  7. Amazing , I can't believe it I could finish 5 hours of this video in just 13 days . Alhamdulilah .
    Thank you too Bucky ! the pretty cool guy 😄

  8. 1:31:56 constructor
    2:33:59 for loop
    2:49:45 switch case
    3:59:39 array as argument
    constsant object
    member initialization list
    operator overloading 5:50:01
    operator 5:55:01
    6:09:01 protected members

  9. I was going to watch this but it took the first 5 minutes to say download codeblocks. 5 seconds would have been enough time. Didnt give me much hope for the rest of the video. Bye

  10. 6:33:55 absolutely true, i dont know how often i have searched for a bug caused by copy paste already. Too many hours wasted.

  11. Thanks a bunch for doing all this work, and making it available free of charge. You got yourself a subscriber 😛

  12. If Youtube had exist earlier. I programmed from late 80`s to beginning 00`s and wanted to change to C++. Had only one gigantic book and my interest sank immediately to zero. 🙂

  13. I wasn't going to watch this video – and i didn't but when i saw 1 ad in 9 hours, i closed my adblocker and watched your ad and continued

  14. Is there a way to run a C++ file from the windows shell? I want to know how to do this so I can write C++ programs in Vim.

  15. Thanks for teaching me c++ and that too in a funny way. Great video, lengthy but informative and worth time investing in this video.

  16. Hey wassup guys ! I have four questions!

    1. Is he really teaching everything there is about C++ language ?
    2. Where can I find C++ exercises and projects to practice (beginner to advanced) ?
    3. Is C++ only for computer app and computer games or it's also use in video games (ps4 and xbox one)
    4. After putting time in studying this video, other videos and practicing, how can I prove my skills to those compagnies ?

  17. i want to understand about composition, association and aggregation please make video on it and make it more easy

  18. So if we are outside of main() and we do something simple like
    #include <iostream>
    using namespace std;

    int addmeup()

    {

    int a = 10;
    int b = 10;

    int sum = a + b;
    return sum;

    }
    or is it still a return 0; and you print it out using cout << ?

  19. Well I know some code from your other amazing videos in the past but now I'm just refreshing the faded neurons with programming and your voice your amazing programing voice lol I sound like a suck up🤣

  20. Is this right, I cheated and looked it up and yeah I know I'm late and someone else may have already answered but I was curious.
    Sir William Wallace was a Scottish knight who became one of the main leaders during the First War of Scottish Independence. Along with Andrew Moray, Wallace defeated an English army at the Battle of Stirling Bridge in September 1297.

  21. Disclaimer: ANY and ALL individuals who went over 12 minutes and 59 seconds on this video are irrevocably disqualified from bidding in the "First to Beat This Guy Up" Auction.

  22. LLVM ERROR: ERROR: Constant unimplemented for type: [1 x i32]
    exit(code = 1)
    This error is coming whenever I am using the cin code

  23. #include <iostream>

    using namespace std;

    int main()
    {
    int a;
    int b;
    int add;

    cout <<"Enter a number bro n";
    cin >> a;

    cout <<"Enter another number bro n";
    cin >> b;

    add = a + b;
    cout <<"The sum is " << add << endl;
    return 0;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *