Press "Enter" to skip to content

C++ Programming


well hello internet and welcome to my
C++ programming tutorial in this
tutorial I’m going to teach the entire
C++ programming language in one video in
the description underneath the video
you’re going to see links to all the
individual parts so that should help you
get it to exactly what you want and I’m
not going to waste any time covering
installation if you guys want me to
cover that just leave a comment down
below and I’ll do that later so I have a
lot to do so let’s get into it
okay so what I have here on the left
side of the screen is a basic TextEdit
or what I have here on the right side of
screen is a terminal everything is going
to be exactly the same as long as you
use G plus plus to compile your programs
no matter if you’re on Mac Windows or on
a Linux operating system so the very
first thing we’re going to do here is
cover comments this is a comment just
like many other languages you can create
a comment just by putting two dashes and
then whatever you want and if you want a
multi-line comment you can just type in
multi-line and then close that off
though there is commenting and c++ first
thing we’re going to do here is include
some outside libraries I’m going to have
some functions that we’re going to want
to use so this one’s going to allow us
to use function called C out and a whole
bunch of other different things you’re
going to see here in a second that’ll be
used for vectors this will be used for
strings and then this will be used for
file i/o now all of your code is going
to be contained inside of a main
function like this and then you’re going
to have a curly bracket and then you
have a closing curly bracket now inside
of here you could call the function C
out either using C out or STD C out like
this what I want to do is get rid of
this part and how I get rid of that is
right here right after the include
statement so I’m going to type in using
namespace STD keep me from having to
type that out every single time now if
you wanted to do a basic hello world
type of statement here all we’re going
to have to do is type in hello world
using C out which outputs the text that
we have here and a carriage return
whenever we type in the same brackets
you see right there and ndele that’s for
the carriage return and this right here
saying we went out to put this on our
screen the other thing we need to do
here is call return zero you’re always
going to do this and zero stands for the
execution went through
perfectly fine so just to keep
everything simple jump over here in the
terminal and this is the command I’m
going to use to run this it’s called see
Tut and make sure with your C++ programs
you always end them with CP pxz
extension then right here I’m saying
that I want to use the version 11 of C++
then I’m going to compile that and then
to run it I’m going to put period for it
slash a dot out if you’re on Windows you
would just type in a and you can see
hello world prints out red like that so
the next thing we want to discuss here
are variables and datatypes
now variables start with a letter and
can contain of course additional letters
numbers or underscores but they must
start off with the letter and here we’re
going to actually create a constant
variable which is going to be of type
datatype double which means that it
contains floating-point numbers or
decimal places and here we’re going to
give it the value of 3.14159265 35 and
once again constant being here just
means that this value cannot be changed
and normally whenever you define
constant variables you make their names
in all upper cases other data types that
are available to you are characters
which contain just one character and
they are going to be surrounded by
single quotes so let’s say I have a
grade equal to and a write like that and
there you go and a character very
specifically is going to take up one
byte inside of memory boolean s– which
normally start with is whenever you’re
defining the variable name can contain
true which is going to be countable to 1
or false which is going to be comparable
to 0 in Saur integers are just whole
numbers without decimal places floats
are floating-point numbers just like
doubles and they normally are accurate
up to 6 decimal places let’s just say
favorite number and just copy this guy
and doubles of course like I said before
are also floating-point numbers but they
tend to be accurate up to around 15
digits in length and then of course if I
want to output one of these variables on
the screen I’m going to use C out again
and then I could say favorite number
inside of double quotes and then to
output it
I just put another bracket inside of
there face
nom and then of course and L for a
character turn in there you can see
favorite number 3.14159 now of course
there are other data types that are
available we have short ends which are
going to be at least 16 bits in size
long int which are going to be at least
32 bits long long int that are going to
be at least 64 unsigned int which are
going to be the same size as the signed
version and long doubles which are not
less in size than regular old doubles if
you want to find out the number of bytes
for a data type we could come in here
and go to C out and say something like
size of int we could then call up size
of and put my age inside of there since
it is an int and inside of C++ all
statements end with a semicolon so we
could just jump down here keep
everything on the screen and go like
that compile and execute you can see
right here size of an int is 4 bytes if
I would then go in do exactly the same
things for characters bullying’s floats
and doubles and execute you can see how
many bytes are in each one of those as
well if you’re wondering how big a
number can be based off of bytes and so
forth and so on let’s go in here and
let’s create the largest int that we can
possibly create and whenever we know the
fact how many bytes an INT can contain
we can then go in and define exactly how
large that number would be and you can
see right here this is the biggest
integer we could possibly have and if we
went down here and said hey let’s go and
up with that like this and you can see
it prints out exactly the way that you
would expect it to however if we go one
over and put an 8 in there and said save
that and recompile you’re going to see
that you get completely different
results see it comes back as a negative
number that’s just the demonstration of
what happens whenever you go outside of
the bounds using your different data
types when it comes to arithmetic you’re
going to be able to use plus and minus
and multiplication and division as well
as modulus which is going to return the
remainder of a division we’re also going
to be able to use shorthand incremental
notation as well as decremental give you
an example of what that looks like
you can see right there on your screen
and exactly how you would perform
addition if you wanted output that on
the screen like this and to demonstrate
the shortcuts for increments and
decrements let’s say that we have an int
and I’m just going to give call it 5 and
give it the value of 5 then output on
our screen 5 plus plus is equal to and
then take the variable 5 5 and increment
it I’ll then increment it from the left
side and then I’ll decrement it from the
right side and then I will decrement it
from the left side and you see what’s
going on right here in this situation
whenever you increment from the right
side it is going to actually perform
that action after it gets the current
value of 5 that’s the reason why you see
5 here then over here whenever you
increment it on the left side it is
going to increment it first it’s
remember it currently has a value of 6
because that’s what happened up here and
that’s the reason why you get a 7 over
here and likewise whenever you have
decrement on the right side it occurs
after the value is taken here and then
here you see that both of them are
decremented or we’re back at 5 again
we’d also be able to come in here and
use another form of shorthand assignment
notation by going 5 plus or equal to and
let’s say 5 in this situation that right
there is going to be equivalent to if
you had 5 is equal to 5 plus 5 right
like that or let’s change this into 6 so
it’s not quite so confusing and yes you
could put multiple statements here on
the same line just as long as you have
semicolons that’s how C++ defines that a
statement has ended another thing that’s
important is to understand order of
operation which basically it states that
if you have some calculation that’s
being performed the multiplication and
division are going to be performed
before any addition or subtraction and
you can see right here exactly what
we’re doing here I have braces around
the additions and subtractions and up
here I do not and here if I execute you
can see I get dramatically different
results why is that well in this
situation we are going to have the
multiplication occur first that’s where
you’re going to get 6 right here then
we’re going to jump in and have this add
up to 3 which comes to negative 3 in
this situation we’re going to perform
addition and subtraction first which is
going to give you a value of 0 so when
that’s multiplied times 2 you’re going
to get 0 so use braces all the time it’s
going to save you a lot of hassle now
let’s say we come in here
and we want to show some division here
on our screen and this is exactly how we
do that 4/5 well if we execute that
might not get the result we were looking
for which into this situation comes out
to zero what happens whenever we want to
actually show a floating-point number
here with decimal places instead of
basically just coming out to zero it’s
actually very simple just come in here
and we’re going to perform what is
called casting to cast from any of the
different data types you’re going to
take the type that you want to cast to
put it between these parentheses and you
can see here we now compile and execute
you’re going to get exactly what you
wanted and of course these can be
performed by putting int in side of here
to cast to an integer even though that’s
exactly what it is
characters as well as doubles right like
that now you can get rid of all this
stuff here so we can focus in on the if
statement now the if statements just
basically going to execute different
code depending upon different conditions
you’re going to have comparison
operators which are going to be equal to
not equal to greater than less than
greater than or equal to or less than an
equal to you’re also going to have
logical operators which are going to be
and or or not and they’re going to come
in the form of and or or not let’s go
and take a look at some examples of how
we could use these let’s say that we
create an age which is equal to 70 years
old and another age at last exam let’s
say we want to set up a little program
here that it’s going to define if a
person is going to be able to drive or
not and then we have is not intoxicated
and we’re going to set that to true
always a good thing if you’re going to
drive now we’re going to come in here
with our if statements we’re going to
say something like if the age is greater
than or equal to one and there’s the
logical operator make sure you close all
the brackets off the right way age is
less than 16 in this situation Oh in
that situation we’re going to say that
they cannot drive now if we want to
check another condition we’re just going
to type in else if in this situation
let’s check to see if they’re
intoxicated in this situation we’re
going to use the not symbol and we’re
going to say is not intoxicated and all
that’s going to do is take this true
right here the not in this situation and
turn it into false and in that situation
where say you can’t try
again I could also come in here and do
an even more convoluted thing if and it
could say something like age is greater
than or equal to 80 and then do another
completely different comparison age is
greater than 100 or more brackets here
age – age at last exam and then close
that off greater than five and then
close these off so what I’m basically
saying here is if the person is over the
age of 100 we’re not going to allow them
to drive if they are going to be over
the age of 80 or equal to we’re
basically going to require them to take
an exam every five years to verify that
they are actually still able to drive
it’s kind of a convoluted example but
I’m just trying to find a reason to be
able to come in here and show you how
all these different things work and in
that situation we’re going to say that
they can’t drive and then finally where
everything else that isn’t checked if we
want them to have the ability to drive
if they make it the hallway down here
we’re going to just type in else and
there you go you’re going to see that
that works for you and here if we
execute it you’re going to see that yes
indeed this person can drive so that’s a
pretty convoluted example of how to use
if statements as well as else and else
if now let’s take a look at switch
statement which is basically going to be
used whenever you have a limited number
of possible options so let’s say we have
int greeting option and let’s say they
chose two then what we’re going to be
able to do is check the versions or the
values of greeting option with our
switch statement put it inside of curly
brackets again we’re going to say case
if the value of greeting option is equal
to 1 well then we’re going to say that
we want to print out on the screen
Bonjour then to jump out of the switch
statement all together we’re going to
type in break and if you didn’t check
that it’s going to continue to check all
of the other possible values like it
will jump down here if you don’t have
break inside and check to see if it has
the value of 2 so you want to make sure
the breaks in there
they could have hola and all these other
different options and then finally with
the switch statement if you want to have
a default thing that it’s going to do if
none of the other things match you just
type in default and then see out and
hello right like that
need break here in this situation and
you can see based off the fact that they
did enter a to hear that Jolla prints
out on the screen that’s basically
everything you need to know about the
switch statement I will take a look at
the ternary operator which is going to
perform an assignment based off of a
condition and its basic formats going to
be variable is equal to there’s going to
be a condition here followed by a
question mark and this is the value it’s
going to be assigned if it is true and
this is going to be the value that’s
assigned if it’s false and to show you a
real example let’s say we have integer
largest num and in this situation I’m
going to use real numbers we’re going to
have our condition is 5 greater than to
put our question mark if it’s true then
we’re going to assign the value of 5 to
largest number and if it’s false we’re
going to assign the value of 2 to that
number and then of course we could print
that out on the screen it’s that simple
ternary operator and now let’s take a
look at arrays now rays are just going
to store multiple values of the same
data type so let’s come in here and
provide our data type for our array just
think of a raise like boxes because
that’s basically what they are in memory
so let’s have an array here and you have
to define inside of arrays how many
boxes you need or how many pieces of
data you want to store in them at the
very beginning and then this cannot be
changed we’ll look at vectors later
whenever we want to look at a way to
change those values could also come in
here and let’s say we want a array full
of bad numbers and it’s also going to
need 5 spaces to hold these numbers this
situation we could come in here and type
in all these numbers we want to store
right like that you’re going to be able
to get the first item in the array with
a label and the labels for all of these
array items are going to start with 0 so
this is going to have an index or label
of 0 this is 1 2 3 4 and if we want the
very first bad number in this array
we’re going to type in 0 just like I
said and change this to bad nums compile
and execute you’re going to see the four
pops up because that has the index of 0
we’re also going to be able to come in
here and create multi-dimensional arrays
which is just going to be boxes of boxes
so let’s say I wanted to have a
character array that’s going to be
multi-dimensional and I’m going to have
5 spaces in the first array that’s
created here
and five in the second read that’s
created here I could then come in and
define that I want to store the letters
for my name we’re going to use two curly
brackets in this situation then we’re
going to close off that first with a
closing curly brace and then open it up
again and here I can put my last name we
jump down to the next line so it’s a
little bit easier to see and we’re using
characters here characters always use
single quotes strings as you’re going to
see later on always use double quotes
and then finally after we have all those
in put two curly braces in there closing
and there you go that’s a
multi-dimensional array if you would
then want to get hold of and print out
the let’s say the second letter in the
second array you see out again second
letter in second array we would go my
name in this situation and we want to
get the second one well in that
situation second is equal to one and
then the second letter in the second
array go right like that and there you
can see right there a pops up which is
exactly the right one
you could also come in and change the
value in the array just by using its
index
so we’ll say my name and let’s say we
want to get the first array which is
zero in this situation and the second
letter inside of it or the third letter
in it in this situation and we want to
change that to e no problem then come in
here and change this to exactly the same
place POW and execute and you can see
here the new value is e just like we set
it right there now I’m going to leave
everything here because I’m going to use
it while I demonstrate exactly how the
for loop works now the for loop just
allows you to continue executing code as
long as the condition is true and you’re
going to define it with for here I’m
going to give the datatype for the value
that I’m going to increment and we give
it the initial value of one I’m going to
say that we’re going to continue
incrementing as long as I is less than
or equal to ten and then we’ll have to
increment it right here curly brackets
and inside of the for loop we could go
see out and just print all of these on
our screen and there you can see it
print it out one through ten
now we could also come in here and use
our multi-dimensional array by stacking
inner for loops and we’ll just go for
and integer J is equal to zero we’re
going to use zero here because arrays
start off with the zero index so we’re
going to say wow J is less than five
we’re going to continue to cycle through
here and then inside of here we’re going
to
another for loop let’s have this BK is
equal to zero okay is less than five
increment the value of K now we can come
in here and print out my whole entire
name by just putting J okay inside of
there and then finally see I didn’t put
the end L in there after it’s done going
through the for loop I could then come
in here and put the carriage return in
and execute and there you can see it
printed out my name remember I changed
the e here and then it went and printed
the last name and that’s what it’s
simply going to do it’s going to go to
the first array cycle through everything
using this for loop right here let’s
come down here and then J is going to
come up here and it’s going to print
everything out so that’s how for loops
work now let’s take a look at while
loops now you’re going to use while
loops instead of for loops when you
don’t know ahead of time when your loop
is going to end so let’s say we have a
random number and this is how you would
generate random numbers inside of C++ we
go Rand and let’s say that I wanted to
get random numbers between 1 and 100
modulus 100 like like that now this guy
alone is going to generate random
numbers between 0 and 99 so if I want 1
to 100 I’m going to put a 1 right here
now with a while loop I can say that I
want to continue cycling through this
loop as long as random number is not
equal to 100 and we could print out on
the screen the random number every
single time and in this situation let’s
say we want to put a comma between there
close that off but of course you’re
going to need a way to get out of this
while loop so the random number is going
to have to change so go and generate
another random number make sure we use
the same thing again and then after we
are done with our while loop put a
carriage return in there but don’t
forget to put the equal sign in there
now let’s go and see how long it takes
us to get to 100 there you can see it
printed all those out right there on our
screen and I don’t know I’m not going to
count how many of those are let then
take that long to come up with a random
number of 100 another thing that’s kind
of interesting is you can basically
simulate exactly what a for loop does
using a while loop the only thing is
we’re going to have to have something
that’s going to increment for us this
index right here will do fine and in
this situation of course keep it outside
of your while loop just like we did with
our random number before we’re going to
say that we want to cycle through row as
long as index is less than or equal to
ten then inside of your output value of
index and then of course increment your
index right here and you can sing do
pretty much exactly the same thing you
can do with a for loop with while loop
and that brings us to do while loops and
they’re going to be used whenever you
want to execute whatever is in the loop
at the very least one time and I’m also
going to talk about strings here for a
second because we’re now going to also
cover how to get user input we create a
string which is just a series of
characters
that’s all string is int int number
guests give this value of 0 start off
and then the do-while loops just going
to start off with do and then in the
very end of it we’re going to have a
while part so you know everything is
going to be executed that is in the loop
at least once even if it number guests
is not equal to 4 so it number guests
it’s not equal to 4 and this is going to
be a basic like guessing game and you
have to put a semicolon here at the end
don’t forget that now inside of this
we’re going to get some user input we’re
going to start off by saying that the
user of our program is to guess a number
between 1 and 10 we want to allow our
user to enter some input and to do that
we’re going to type in get line we’re
going to type in c i n which is going to
be the source of the user input which is
going to be the keyboard in this
situation and then we’re going to have
to say where we want that value to be
stored in this situation we want to
store it in the string number guest if
we want to convert the string into an
integer we can then go int number guest
we need to do this because this guy
right here we’re not going to be able to
compare a string to an integer of 4 it’s
easy enough to change we’re just going
to type in s TOI
which is going to convert a string into
an integer and then we’ll pass in number
guest which is our string and then we
could print int number guest if we would
like and this is going to continue to
cycle until they enter the number of
four and then if they get out of our
do-while loop we could come in and go
see out you win otherwise it’s going to
continue to ask them as you’ll see guess
between 1 and 10 I’ll type in 1 ask me
again
2 now for you can see I want right so
there’s user input converting from
strings into integers do-while and a
whole bunch of other different things
let’s take a deeper look at straw
now in C++ in comparison to C C++ is
going to provide us with string objects
and you’re like I said before they are
always going to be surrounded with
double quotes and just to cover this
real briefly inside of C you would have
previously made strings by saying
something like copy array six like this
is equal to and then created a character
array so I’ll just type it happy and
then you had to always end it with a
backslash and then a zero with closing
curly bracket and that’s how we were
using strings inside of C the C++ way is
much better and easier so we’re going to
say string birthday string is equal to
space in their birthday and there we are
we’re done now we could of course
combine these guys and how we’re going
to combine them is with the
concatenation operator or just the plus
sign or you can just call it combined
you can call it whatever you want
so birthday string and there you can see
it printed happy birthday combine both
of those printing them out the screen
and perform a couple other different
operations let’s say let’s call this
your name always need to define your
variables before you use them and then
I’m going to have it come in and print
out the screen to get you some user
input what is your name get line it’s
going to get the user input get it from
the keyboard store whatever they enter
into your name and then I could go see
out hello put your name inside of there
what’s your name type in Derrick hello
Derrick talks to me could then come in
and create let’s say double in this
situation there’s constant you don’t
know what this is don’t worry about it’s
just a random number of thought of or
Euler thought of actually I could then
go string Euler guess we want to do like
a little quiz application uppercase
letter right there just makes it easier
to be able to read the variable names
and then I’ll go Euler guess double and
now come in here ask them what is those
constants I could then get whatever they
enter inside of here don’t worry about
it I’m going to show you how to convert
from doubles and or from strings to
doubles we need to do to convert from a
string to a double and we’re going to
store it in Euler guess double is type
in instead of sto I sto D
right like that and you’ll learn guess
is the string value they entered and
then I’ll say if you older guess double
is equal to Euler constant which you can
see all those on the screen curly
brackets again see out you are right and
then else you are wrong up make sure you
come in here and put it s inside of
there Euler’s constant Euler’s constant
execute what’s your name Derek what’s
Euler’s constant 0.577 21 you are right
you can see exactly how that works
couple other things we can do with
strings you want to get this size or the
number of characters in a string see out
size of string could you use Euler guess
in this situation and then just call
size so it’s basically exactly what you
thought it would be when to check if a
string is empty Euler guess again and
just type in empty great to use whenever
you want to test input from a user could
use append also to be able to add
strings together
so we could get Euler guess and call
append and then add something like was
your guests bounced out of there there
you go
Turk point five let’s type that in you
were wrong didn’t matter size of string
is equal to three see right there
decimal five five that’s the three is
string empty 0 comes back or false
it’s obviously not empty and it prints
out my guests there on the screen come
in here and create another string let’s
call this dog string give it the value
of dog and cat string give it the value
of cat we can compare numbers so let’s
say when we see out or compare strings
them in with the comparison function and
let’s change this to dog and this the
cat and change this the dog and you can
see in this situation whenever a value
is equal like it is here dog string and
dog string you’re going to get a return
value of 0 you’re going to receive a 1
in this situation if it’s less than and
a negative one if it’s greater than and
this is all based off of alphabetical so
we could also come in here and sign
copies of a value to another string we
go whole name is equal to and go your
name a sign and I could say your name
and there you go
I could then get a substring so let’s
say that I wanted to get the first name
while ago
like that dot a son and I would pass in
the string I want to work with then the
starting index from which I want to
first start pulling characters and then
the number of characters I want which is
five I could then come down here change
this to first name if I typed in my full
name then you’re going to say that it
just grabbed the substring up Derrick I
could then perform a search on a string
so let’s say that I wanted to find the
index of my last name get the last name
index if we did not know it I could type
in your name and then find followed by
the substring that I want to find inside
of there and the index that I want to
start searching from and then I could
say index for last name last name index
is my full name dot data and you can see
the index starts at six and if you count
over you’re going to see that B actually
is six and also insert is going to allow
me to put in a string at the index that
I tell it to put it in so I’ll go insert
five so starting in index five I want to
put in the word Justin change this to
your name just so you can see that here
in a second I could also erase or delete
six characters starting at the index of
seven and I would just go your name
again the string want to work with a
race starting at six
I want to delete six characters starting
at the seventh index and then print it
out there again and then finally I could
also come in and replace let’s say I
want to replace five characters starting
at index six well there’s the index 6 so
I want to replace five characters and
let’s say that I want to change it to
Maximus
so my new name will be Maximus and make
sure that is named your name and here’s
my name you can see all the changes that
are made to it right there I inserted
just and inside of there right there I
got rid of it and then right here I’ve
performed a replace to change my last
name to Maximus so there is a lot of
stuff about strings okay now let’s talk
about vectors now vectors are just like
a raise the only real difference is that
their size can change and how you create
a vector is type in vector and then the
datatype you want to store then then
let’s say that I’m going to store or
lottery numbers in my vector I’m going
to decide that the initial size should
be 10 I could then create an array to
show you how to
put an array in a vector and let’s say
it has a size of five and then we’ll
initialize it just like we did before 14
24 and 34 like that we can then go and
take our vector and insert our array and
how we’re going to do that is go lottery
number
effect and say that we want to start
inserting at the beginning of our vector
and then we’ll say the array that we
want to insert into it and then let’s
say that we only want to take the first
three numbers from our array right like
that we could then go see out and just
print lottery number like that and at
and let’s say we want what’s the third
item which would be second index whoops
like this
we went out put that on the screen
because remember it didn’t take the
whole array just took these first three
numbers and there you can see 14 popped
up now if you would want to also come in
here and let’s say we want to add
another value into the fifth index
lottery number dot insert we could then
go lottery num vector
begin which is the beginning of the
vector plus five to get to the fifth
index and then put 44 inside of there we
could then jump down here to five and
see the 44 pop dot pushback is going to
add a value at the end of our vector so
push back and let’s say we want to put
64 in there we then want to defined as
you can see the vector resizes we then
wanted to find that final value we can
call back and it would get it for us as
you can see right there pop back it’s
going to remove the final value well is
no point in doing that which in this
situation would be 64 and then we could
get the first value in the vector by
calling front step back we could get the
last element by calling back of course
empty is going to return zero if the
vector is not empty and a 1 or true
whenever it is empty and then finally if
we wanted to get the size of our vector
we can do that with sighs there’s a
rundown of all we can do with vectors
now let’s take a look at what we can do
with functions inside a C++ now your
functions are
actually going to appear before your
main function and it basically just has
the return type so let’s say that we
wanted to have a function that added
numbers we would have our return type
for our function if it didn’t have a
return type it would have void instead
of integer and then we’re going to
define the data types for our attributes
that are going to be received by our
function so I can um we could also set
it up so that this function would have a
default value which means actually you
wouldn’t have to send a second attribute
in that situation the only thing that
you have to do is if you do this is all
of your functions that have a default
value or all of your attributes that
have a default value have to come last
so this has to come before that so
that’s the only rule there and this is
also known as a function prototype we
could then go and type in combined value
is equal to first num plus second num
store that in there and then return the
combined value and I did it this way so
that I could demonstrate something else
you could take this right here and put
it right down here in the return part
right there you don’t have to create a
new variable just to perform that
another thing I want to demonstrate
since I’m up here and not inside of main
is that you are able to overload
functions which basically means you can
use exactly the same name but different
attributes so I could come in here even
though I have add numbers already
actually it’s just copy the whole thing
paste it in there so there’s add numbers
twice it’s okay to have add numbers the
only problem is we have to have
different attributes inside of here
so let’s say we have second num and we
also have another integer which is going
to be third num and like I said before
we don’t need all that we could just go
return and perform our addition right
inside of here
second num third num and return that now
let’s jump down inside of main and
actually call these functions if I
didn’t mention before functions
basically allow you to reuse code and
also add a certain level of organization
to your code so here add nums I’m
passing in one I’m going to use the
default value of zero they’re not going
to pass anything and just to demonstrate
it and then I’ll show the other one as
well at numbers one five
five and six and though you can see if I
execute that right there you got one and
twelve pop back on the screen let’s go
and create a little bit more complicated
function here and this is basically what
they call a recursive function and this
is normally demonstrated using the
getting of factorial so that’s what I’ll
do
get factorial int number and this is
going to be a function that calls itself
from inside of itself sounds way more
complicated than this so I’m going to go
and create an integer called sum then
I’m going to say if number which is
going to be passed inside of here is
equal to one then I want to have some
equal to one LS sum is going to be equal
to get factorial save call on itself
from inside of itself and here I’m going
to get a number minus one times number
this is going to get our factorial
because here we’re then going to return
sum so very quick way to get the
factorial of a number and basically what
is going on here is right here we’ll
come down to this part this is the first
time through this function and let’s say
number is equal to three well in that
situation get factorial is called with
the value of three would actually be two
let’s just focus in on this one right
here so three minus one is equal to two
in that situation it’s going to return a
value of two and then we’re going to
multiply that times three so right here
whenever we’re going through your the
second time this time we’re going to be
passing two into the get factorial so
there we are and two minus one is going
to be equal to one and in this situation
this is going to return the value of one
and get factorial in this situation
whenever it comes in here the third time
number is going to be equal to one so
sum is going to be equal to one and
that’s going to end that functions
execution and then soon as sort of
ricochet back up to the beginning so
what we’re going to be left with in this
situation is this is going to return one
which is going to be multiplied times
two that to right here actually goes
right here and then we’ll multiply that
2 times three which is going to give us
our final value of six and let’s go
through here and let’s actually run it
to make sure that that’s what it does
and how to find out is to go factorial
of 3 is and then call the phone
get factorial passing the value of three
and I’ll make sure I come up here and
have the return value be an integer
right there and then also come in here
and remove this little parenthesis I put
in there by accident if we execute it
you can see factorial three is six
exactly like we demonstrated up here
there’s a rundown of functions inside of
C++ now I want to cover file i/o or how
to read and write files using either
text or machine readable binary so I’m
going to create a string here I’m going
to call this Steve quote it’s equal to
and say a day without sunshine is like
you know night now what I’m gonna have
to do is create an output file stream to
create this file if it doesn’t exist or
what it will do is create a function or
create a file if it doesn’t exist I’m
going to call it writer and I’m going to
define the name that I want to save this
text into and I’m going to call it Steve
quote dot txt and then going to verify
that the file stream was created so that
I’ll be able to write to the file if not
I’m going to put out an error message
and then if we want to signal that an
error has occurred we can return a
negative one in this situation else if
the file stream is open that means we
can write to the file and it’s very
simple to write text to the file just go
writer and the string you want to write
and then I’ll throw a new line on there
at the end and then very importantly you
also want to come in and close that file
stream at the end I’m going to continue
going here because we’ve got to read
from it let’s say that you also wanted
to come in here our file stream is
closed we’re going to create another one
and I’ll just call this writer too and
then in this situation what we’re going
to do is we’re going to define that we
want to write to the Steve quote text
file and then here we’re going to say
that we want to append what we’re going
to do next to the end of what’s already
there now there’s a whole bunch of
different symbols we can use use here to
do different things with files like I
just said with iOS app we’re going to be
able to append to the end of the file
with iOS binary we’re going to be able
to treat the file as if it’s binary iOS
in is going to open a file to read input
what you’re going to see here a minute
iOS trunk is the default what happens
whenever you don’t use one of these
symbols and then iOS out is going to
open
off to write output but in this
situation we’re going to be a pending
and once again we’re going to need to
come in here and check that this file
stream is already open and I’m just
going to copy this paste that in there
but make sure this is right or up here
instead of right there you go and this
is going to be writer 2 we’re going to
spit out an error again if the file
stream was not opened and then to append
in this situation we’re going to go
writer and then let’s say that we want
to put on a new line backslash N and
then a dash and then type in Steve
Martin we then could come in and say
write or close and there that is alright
so we wrote and we appended and we
created the file and did all that now
I’m going to create a character that’s
going to hold each of the individual
characters we’re going to read from our
file and we’re going to read these
characters using an input file stream so
input file stream and I’m going to call
this reader and you need to tell it
exactly what file you’re going to be
reading from and then I’m going to check
if that stream is going to be open just
like we did before with the reader
inside of there come in here and print
error again turn Erica negative one else
we know that we got our stream open and
we’ll be able to read from that file and
we’re going to use a for loop in this
situation to read each character from
the stream until we get to the end of
the line so I’m going to go in I is
equal to zero then I’m going to go while
the reader does not get to the end of
the file we’re going to continue reading
characters and then we’ll call reader
get to get the next letter of output
that we want and then in this situation
let’s just say that we want to print
that out to the screen type in letter
then after the for loops all done with
you go see out until and then once again
close the reader if we did that all
right day without sunshine is like you
know night so you can see it worked out
and those are all the different things
you can do and in regards to reading and
writing data using C++ I’m going to
briefly touch on exception handling and
exception handling basically allows you
to avoid potential problems in this
situation I’m just going to focus on one
which is a division by zero error that
could cause all kinds problem now
whenever you want to cover something
that could cause an exception or an
error that you want to be able to catch
you’re going to surround the code that
could potentially cause that air inside
of what’s called a try block and then
what we’re
to do is going to set an if statement
inside here that says if the number is
not equal to zero well in that situation
we can say C out and divide by number
like we have there but as we can see
that is going to cause an error and
we’re never going to get there
else we’re going to say throw number now
this throw is going to look for a catch
and the catch area is where we are going
to catch the number that was entered
don’t have to do this but it helps so
that we have the error message makes
sense and then we could do something
like number is not valid and then maybe
have the mask or ask them for it again
you can see here if we run zero is not
valid so instead of causing an error
right here by putting this inside of a
try block and then throwing if they
would actually enter a 0 which causes
potential errors we’re instead going to
catch it down in this area right here
and solve it and keep our program from
crashing so that’s a brief explanation
of exceptions ok now let’s talk about
pointers now when data is stored it is
stored in an appropriately sized box
that is based off of its data type just
like we covered previously here we have
an integer and here we have a character
now like we did before we’re going to be
able to come in here and use sizeof to
determine how many bytes that data is
going to take up and we can see here
whenever we run the program that the
integer is going to take up 4 bytes and
the character is going to take up 1 byte
now you’re going to be able to reference
this box or this memory address where
all of your data is stored with what is
called the reference operator which is
an M purse and so we could come in here
and go see out and my age is located at
and then use the reference operator
followed by my age and when we execute
that you can see the memory address
right here now what’s really cool about
this is whenever we don’t use pointers
or we don’t use reference operators and
we change a value of a variable in a
regular old function that change of
value does not carry on it is lost and
the reason why is whenever we use
regular functions what we are in essence
doing is passing a value into that
function so even if we had the same and
these and we passed in some as a
variable into a function called add
these just like this what’s going to
happen is the value of sums so let’s say
we have an integer here integer sum is
equal to 5 and we pass some into the add
these function well if we change the
value of sum and we do not pass it back
sum is still going to be equal to 5
because we are passing a value with
reference operator and with pointers in
general what we’re going to be able to
do instead is pass by reference and any
change that is made inside of there is
going to show now a pointer is going to
be able to store a memory address to
recreate my age to demonstrate this and
whenever you’re defining a pointer you
just have to use the same data type
followed by a star and we’ll say age
pointer like this is equal to and then
you’re going to use the reference
operator to pass that over just like
before we’re going to be able to access
the memory address with this pointer of
pointer by just typing in age PTR but
we’ll also be able to dereference to get
the data at that memory address so we’ll
say data at memory address and how you
dereference is to put star in front of
age pointer right like that you’ll see
here with this pointer will now be able
to get the memory address as well as the
data that is stored at that memory
address let’s take a look at arrays as
well let’s say we have our bad numbers
array just like we used previously we
can then go int star to create a pointer
and we’ll call this number array pointer
is equal to bad nums
what’s neat here is we’re going to
actually be able to increment through
our array just using shorthand notation
like plus plus and minus minus so we can
go see out address number array pointer
and value and then to get the actual
value in the array we can go numb array
pointer and then close that off and then
come in and go numb array pointer plus
plus to get the next value in our array
bounce that in there and there you can
see jumped in there grabbed 4 and 13 as
well as the memory addresses and as we
saw previously an integer takes up 4
bytes you can see the difference
the memory addresses right there so
you’re learning about how this stuff is
stored inside of here and something else
that is interesting is an array name is
just a pointer to the array so we could
come in and go address and put the array
name by itself inside of there at gnomes
to get the value that’s stored there and
you can see that it works also see same
memory address same value so something
interesting think about like I said
previously whenever we pass a variable
to function you’re passing by value
however whenever you pass a pointer to a
function you are actually passing a
reference that can be changed so let’s
jump up here and let’s create a function
to demonstrate exactly how this works
this isn’t going to return anything so
I’m going to pull it up void inside of
there and I’m going to say make me young
and this is going to receive a pointer
so I’m going to put int with a star
after it and then age like that and then
let’s pull up out here on the screen the
previous age I used to be like that and
to get the value of age I put a star and
age because otherwise it’s memory
address have to put that store in there
but then I can also go in and change
that value by putting star and age is
equal to 21 don’t return anything don’t
have to do anything otherwise then I can
come down here and call make me young if
I want to pass a reference to that guy
passing by reference that’s all that
means I can do that and then I could put
I’m and then just a check or verify that
it actually changed that age you can do
this and you can see I used to be 39
that came from the function then we
changed it to 21 inside of the function
and then you can see here that it
changed globally can also come in here
and once again this ampersand is going
to denote that age reference will be a
reference to the assigned variable so
there’s reference difference between a
reference and a pointer which I’m going
to get more into when you should use one
when you should use another one I’m
going to be able to go see out you’re
actually going to use references more
than you use pointers like I said that’s
coming in a second another thing that’s
interesting is we could take this
reference and increment it and see that
that also changed my
right like this you can see here’s my
age 21 here’s my age of 22 again
globally we’re changing all those then
let’s create another function this one’s
going to be called act your age just to
pass the reference and show the
difference between passing that
reference and dealing with pointers come
up here and create another function it’s
also not going to return anything just
to prove that nothing’s exchanging hands
act your age except we’re passing a
reference this time so we’re going to
put the ampersand inside of there and
I’m just going to put age there again
doesn’t matter what I put and here I’m
just going to say age is equal to 39
back down here again just demonstrating
a difference between passing by
reference and passing by pointer and we
could go ahead and prove that it’s still
change age is equal to ndele and there
you can see it did indeed change so
basically you might ask yourself well
whenever you’re deciding whether you
want to use pointers or references when
should you use which well basically you
should use pointers if you don’t want to
have to initialize at declaration what
that means is you can see right here I
am defining we’re initializing what I’m
referencing whenever I am using the
reference option whenever I am dealing
with pointers I do not need to
immediately initialize and then on top
of that with a pointer I’m going to be
able to assign another variable to it so
a pointer is going to be able to deal
with more than one while a reference is
going to get this one reference to this
one variable and be stuck with it so if
you don’t need to be able to change
whatever you’re going to be pointing at
use a reference that’s pretty much the
ironclad rule so and that’s the basis of
pointers we’re going to get more into
them here in a second but now I’m going
to jump over and talk about classes now
now in object-oriented programming what
we’re trying to do is model real world
objects like an animal in code and how
you do that is you think about well what
makes a specific object an object well
each object has attributes such as
height and weight and whatever and it
also has capabilities so it can run and
it can eat just to be really simple
there well these attributes are going to
be modeled in the variables and these
capabilities are going to be modeled in
the functions or
the methods same thing just a different
name so what I’m going to do here inside
of my class is I’m going to have some of
these things be private and by private
what I’m saying is that anything that is
marked private in regards to attributes
or variables is only going to be able to
be changed by functions inside of my
class and we do that just to keep them
protected and this is called
encapsulation so I’m going to have my
name height and weight all be private
and while I’m in here I might as well
talk about static variables basically if
you put static in front of something all
that means is that this variables value
is going to be shared by every object of
type animal that is ever created so
let’s say that I want to also keep track
of the number of animals that are
created from my class well it wouldn’t
make sense that an animal would count
that’s another reason why we have static
here because static variables also
normally are attributes that the class
object would not have so we want to be
able to count the number of animals that
are created but we do not want to
actually have the animal type defined to
be able to do it and that brings us to
public well if we have variables that
are private we’re going to have to have
public methods that are going to be able
to access the values inside of there so
we’re going to have something like get
height there we go and I’m actually
going to just create this here on one
line and whenever this is called it is
going to return the value that is stored
for my animal object to whoever calls
for it and we can also do the same thing
with weight and this is also
encapsulation we’re keeping all of our
data protected and this is going to
return a string
whenever the get name function is called
and we can just go return and name and
there we go but what we really want to
do whenever we’re dealing with
encapsulation is protect the values that
are going to be stored so they might
want to come in here and set the height
and let’s say that we decide that we
only want them to be able to enter a
reasonable height we could provide a
check inside here to verify that the
animal height makes sense and otherwise
discard it but in this situation I’ll
just
that’s nice and simple also going to do
the exactly the same thing here for the
weight so weight and let’s have this
video kilograms weight kilograms and of
course it doesn’t matter if you put
centimeters or kilograms or what you put
inside of there and setname say animal
name right now we have to have this be
different from this but I’m going to
show you how to change that here in a
second animal name right there
everything else looks perfectly fine and
then we could also create another
function inside of here let’s call this
at all and here we’re creating a
prototype of a function that we are
going to declare here in a second you’re
going to see it and that’s how we
declare a prototype another thing we
would want to have is a constructor and
what a constructor is first off its name
is going to start with whatever the
class’s name is and then here we’re
going to create a prototype of that and
this is going to be the function that is
going to be called whenever an object is
created and so it’ll pass in the height
the weight and the name and the
constructor is going to handle creating
every object if we have a constructor we
all show should have a destructor and it
just has that little tilde sign there
once again this is going to be a
prototype where I’m going to build the
door declare the actual one down below
and then let’s say that we want to have
another constructor that doesn’t receive
anything and this is an example of
overloading and to overload a function
like I talked about previously the name
can be the same but the attributes need
to be different so here we have two intz
and a string here we are getting past
nothing
there’s also protected members when
basically whenever we’re dealing with
protected what we’re saying is that
anything marked as protect is going to
be available to members of the same
class as well as subclasses but nothing
else and we could also come in here and
define a static method and a static
method is set aside because they are
attached to the class and not to the
object as you’re going to see I’m going
to show you in a second how to actually
call for a static method and in this
situation static methods also can only
access static member variables like this
guy we have up here right there so we
are going to have this method
specifically be able to return the
number of animals
that are created here we go and this
time we’re not going to do a prototype
we’ll just have it actually do what it’s
supposed to do there it is and also
finally we’ll create a two string which
is going to print out all the
information we have on our animals okay
so we went and we declared our class
it’s after this little curly bracket
we’re going to put a semicolon and now
we’re going to declare everything so the
very first thing we’re going to declare
is our static number of animals and how
we refer to static variables is put the
class name followed by these two colons
right here and then whatever the name of
it is say it’s tied to the class it has
absolutely nothing to do with the object
itself we can then define the prototype
method for set all so set all is right
here we’re going to go and define it
there that was just a prototype and
we’re going to do that by putting a void
animal and then set all like that and
then we have to define exactly what’s
being passed in here so we’re going to
have two inch for the height and the
weight as well as the string name this
basically does what the constructor does
but just wanted to show you something
now if you want to refer to an object
specific height and not just a generic
type of height you put this inside of
here and the reason we have to do that
is whenever the class is created there
are no animal objects created so if we
want to refer to the animals you know
the specific animal objects version or
value for height we have to put this in
front of it and there we go and now we
can also do the same thing for weight
and also name just change this to weight
and change this to weight and we could
have also come up here and used this
here see how we had to use centimeters
instead of having height or weight or
whatever inside of there if we would
have used this we would have been able
to get around that just a way to refer
so that the you know the code actually
knows what you’re trying to code or what
you’re trying to do you can also come in
here and change that static variable
remember we are creating or changing the
values here and then once again we’re
basically going to do this with the
constructor as well and remember the
constructor is called every single time
an animal object is created so there we
go and it’s going to be called an
I’m going to get this right here int
wait string name and this is basically
going to do exactly the same thing that
this does let’s just go in there paste
it in there and you can see that
basically said all does exactly the same
thing that constructor does so there’s
no reason to put that in there I just
did it just so I could show you another
function inside of there so let’s just
get rid of this to show you the regular
functions and constructors all are the
same for the D constructor we’re just
going to go animal two colons ampersand
animal doesn’t receive anything and then
we could come in here and just put a
message out animal we get this specific
animal name put this in there again name
and destroyed just so that we have a
message on the screen to see what’s
going on we have our overloaded
constructor that’s going to be called
whenever no attributes are passed in an
animal animal it’s still constructor no
attributes and in this situation we
could also call that we have created
another animal then we have the two
string where’s that at
there it is say just void to string
let’s come down here and declare that
void doesn’t get anything animal class
name to string doesn’t receive
attributes and here we could come in and
just print out all the information we
have on our animal is and get the height
for our specific animal object jump down
in the next slide just keep this neat
centimeters tall and this get the wait
for it kilograms in weight and just
close that off then we’ll be able to go
down in Maine and start creating animal
object so to create this let’s say we
want to create an animal named Fred we
could then go into Fred and set his
height to 33 set his weight to 10 and
set his name of course to Fred I like
that just create a new Fred animal we
could then get the values back from it I
going Fred dot get name is we’re not
using this here just to show you the
difference between trying to get the
data from inside of the class itself and
then trying to get it somewhere else
centimeters tall and Fred get weight
kilograms
in fight there we are and we could also
come in and use our constructor in this
situation here we are using the default
constructor that doesn’t get any
attributes and here we’ll use the
constructor that does get out your bids
so we’ll pass in 3615 and Tom and then
we’re going to be able to also print out
all the differences between Tom and Fred
let’s print that out and then just
change this to Tom there’s Tom there’s
Tom there’s Tom and made little error
here let’s just jump up to set name and
let’s make sure we change this to a
string and on an integer move a little
bit too fast here sometimes make little
mistakes and there’s an animal name
that’s also come down here and change
this name and then change this to name
all right so there you go now you saw
that we created a Fred and a Tom object
let’s go over and execute that and there
you can see fred is 33 centimeters tall
10 kilograms in weight tom is 36
centimeters tall 15 kilograms in weight
and here you can see the destructor was
called to destroy both those objects
since we no longer need them now we can
also come in and inherit all of the
attributes and the methods that are
created from the animal object and
instead use them in our dog class so
there we go just new create a new dog
class and to inherit all the animal
classes we just put a colon and then
public and animal and that’s all we need
to do and we have everything that is
defined in the animal class we could
come in here of course and create some
new ones so let’s say we wanted to have
a sound string that is going to have the
value of wolf and then we could also
come in and create a bunch of public
methods that do some different things
one thing it’s going to have to do is
get the sound and in this situation I’m
just going to have that print out on our
screen so sound there we go since we are
going to have a sound in our dog class
and our dog objects we’re going to have
to declare a new constructor that’s also
going to get the sound so int int string
and string I like that with that
prototype we could also come in here and
declare a default constructor that’s not
to receive anything and then on top of
that call the default superclass
constructor just like going like this
and there we are and we could also
decide to overwrite two strings since we
are also going to be printing out the
dog sound that’s all we’re going to do
there and then put another semicolon
here at the end of course very important
for our class now we have to go in here
and actually define everything that was
is going to change so this is going to
be the constructor for our dog and it’s
going to get passed the in tight and the
integer weights and the string name as
well as the string mark now to reference
the animal class here and put a colon in
an animal and we’re going to say here
that we want the animal constructor to
handle the height and the weight since
it handled it so well previously however
I want in this constructor to be able to
handle the sound part because sound
doesn’t exist in the animal class and
here we’re just going to have a theme
park so that’s a great way to have the
animal constructor handle height weight
name because all those attributes are
going to be shared and at the same time
just change the thing that is different
saves us some space also could come in
here because to string also changed so
let’s go in here and change it and
because the attributes like name and
height and weight were private in animal
I’m going to have to use the get method
to be able to access them so I’m going
to say this get name right like that is
this get height centimeters tall and
this get weight kilograms in weight and
says this is the thing that’s different
jump down here this sound okay now I can
demonstrate actually creating a dog
object so let’s just go dog and let’s
call him spot I’m going to pass in 38 16
his name is spot and the sound is going
to be wolf if I also just want to
demonstrate using static methods here go
see out number of animals that
created animal and to call a static
method you go get numb of animals
believe that’s what they called it find
out in a second and now I’m also going
to be able to go spot dot it sound and
we could also go in and get Tom test to
string which basically does with all
that other stuff did up there spot dot
to string and just to show you one other
thing we could also call the superclass
version of a method by going animal spot
animal and then put in two colons and
string and by the way I keep calling
this two colons this is called the scope
operator if you want to know what that’s
really called and of course the end here
make sure we put our return statement
with zero and if we execute it you can
say there is everything number of
animals that were created you can see
it’s three right there and that includes
spot being the dog as well as Fred and
also Tom so that’s the reason why that
showed up and the reason why is the
animal constructor was called every
single time whether dog was created or
animals were created you can see wolf
right here is this guy right here
calling get sound you can see we’re
calling to string on tom to print out
the information on that we can see here
that spot also says wolf while the
regular animal does not not
automatically works and here we’re just
demonstrating the use of calling to
string and in this situation spot
whether he has a sound or not doesn’t
matter because we are calling the animal
version of two string and the animal
version of two string only prints out
the height and the weight as well as the
name and then finally you can see right
here where the destructors are called
and all of our objects are destroyed
okay now it’s time to talk about virtual
methods and polymorphism I’m gonna hold
off on the definition for polymorphism
here and instead create a new animal
class and it’s going to have some public
stuff that it does and what it’s going
to do here first off is get family which
I’m going to refer to as a family of
objects and there’s just say we are
animals close that off and then this is
where we get two virtual methods I’m
going to mark this as virtual and then
I’m going to change it back
from virtual basically we mark a method
as virtual when we know that an animal
in this situation will be a base class
that may have this method overwritten by
a subclass and you’re going to see it in
action here it’ll make more sense when
you see it in action so in this
situation let’s say get class and then
I’m gonna go I’m and and okay so these
are all in the family of animals but the
specific class for this is animal in
this situation and close that off and
now we’re going to need to create a
subclass called dog of type and more
it’s going to inherit from animal and
it’s going to have public course it
inherits everything get class in this
case I’m a dog is going to get printed
out and then close off that class now
what we’re going to be able to do here
is come down inside of here create an
animal class is equal to new animal and
dog in this situation new dog now in
this situation it’s not going to matter
if this is marked as virtual or not
because these are both references to
animals and dogs as you can see right
here I can just go animal – get class
and dog dot get class right like that
here I got the virtual part off of there
and here you can see if I execute this
I’m a dog and I’m an animal both print
they’re on the screen if I was however
going to try and call get class from a
function you’re going to see that we get
some problems so here we’re going to say
void what class are you and we pass in
an animal object they’re both animal
objects remember well you would think
I’d be able to go animal get class and
get the exactly the same results however
as you’re going to see if I do that by
passing in animal and dog we don’t get
the results we expect instead what
happens is I’m an animal gets passed in
both times if we want to be able to call
the proper get class for the dog and the
proper get class for the animal all we
need to do is come up here and type in
virtual and all that means is it’s
saying hey we expect this to change so
if you get a dog whether it’s an animal
or not why don’t you go take a chance
and take a look at if get class resides
in the dog class or dog
and now you’re going to see everything
prints out exactly the right way now
we’re going to come up and create a
couple more classes let’s say that we
also want to create another class and
let’s call this German Shepherd which is
a little bit more specific and it is
going to be of type dog and then we’ll
go in and type in public void and
overwrite get class with I’m a German
Shepherd ndele closing curly bracket and
void get derived I’m an animal and dog
ndele closing curly bracket and now come
down here and experiment a little bit
more see basically polymorphism all it
means is whenever a animal is passed in
in this situation like it did right here
we passed in an animal object it’s
automatically going to find the right
method to call so that’s that’s
basically it now there’s many ways you
can apply polymorphism but polymorphism
on its own looks like that so let’s
create a dog object and then let’s
create a German Shepherd and call him
max the base class can also call derived
class methods as long as they exist in
the base class so we’re going to come up
here create an animal it’s a pointer to
the dog and to get a reference to spot
we just put that little inverse and in
there create another one and point this
to the German Shepherd get a reference
to that with Max we can now call the
method that was not overwritten by any
of the other classes get family that’s
the same an animal we could also call
get class which was overwritten and then
come in here change this deployed sorry
about that and then of course also close
off our class the curly bracket and
execute it and there you can see we are
dogs we were able to call this guy
properly and then I’m a dog see this is
a reference to dog spot and we were able
to call the proper get family because
this was listed as an animal and also
we’re able to call the very specific
class that resides in dog even though
these were listed as animals another
example of polymorphism another thing
that’s neat is we can actually call the
superclass German Shepherd because
everything is inherited so we can call
get family that resides in the animal
class and of course we can also call the
overwritten German Shepherd version
shepherd and it’s automatically going to
go and get the right thing as you can
see right there we’re animals and I’m a
German Shepherd so there’s an example of
using virtual methods as well as a whole
bunch of different examples on how
polymorphism works and then to finally
get polymorphism hundred percent in your
head let’s go and create an animal
object as well as a dog object and a cat
object that all inherit from the animal
class now the animal is going to be a
virtual method again because we’re going
to override it and we’re going to treat
them as animals because they are curly
bracket and we’ll say the animal says
ger basically going to do exactly make
sure you put that closing curly bracket
on there and then we’re not going to
copy the virtual part in this situation
in the dog here we’re going to say the
dog says woof and then the cat says meow
as homework you could look into how you
would actually set up an animal class as
a capability class that exists only to
be derived from so look into that that
is your homework because you guys asked
for homework all the time and now I’m
going to also jump in here and give you
an example of what an abstract data type
looks like so let’s say we have a car
and this is more for completionists
reasons than anything else we could come
in here and public again virtual int get
numb wheels the value 0 and virtual int
get numb doors and then you’d be able to
derive this by coming in here and
creating a new class called stationwagon
inherit from car even though car doesn’t
do anything closing bracket move that up
and then come in and define everything
that we saw in the generic car or the
generic car abstract data type get numb
wheels as for wheels like that close
that off and then we could go int get
numb doors just like we defined up here
in this abstract data type for car stage
wagon has four doors and then we could
also come in here and go station wagon
create an empty constructor and a
destructor right like that now let’s
jump down in Maine and play around with
these guys so we’re going to create
another animal here for our cat and to
do that we just call cat and animal dog
new dog and we can see that if we call
the cat specifically saying we want it
to make a sound and then we do the same
thing with the dog you can see that the
cat says meow and the dog says wolf even
though they’re both set up as animals
now let’s go in and test our abstract
data type with the car let’s create a
stationwagon
of type car it’s equal to new station
wagon
I like this and even though is a car
object get numb wheels you can see that
station wagon has four wheels shows up
there on the screen there’s a whole
bunch of different examples of
polymorphism and a whole lot of
information on C++ hope you guys enjoyed
this video if you actually watch the
whole entire thing please leave a
comment telling me so I would really
appreciate that and please leave your
questions and comments below otherwise
till next time
Please follow and like us:

Be First to Comment

Leave a Reply