- [Instructor] Passing functions in C++.
Here's our use case.
We've been studying hash tables,
and soon we're going to implement hash tables in C++.
And for the upcoming project on hash tables,
you'll be asked to experiment with different hash functions
to see how behavior changes
when we change our hash function.
Now, in order to do that,
you could implement two different hash table classes,
hash table A
and hash table B,
each with a different hash function.
But that's gonna lead to a lot of code duplication
because the only thing that's changing
is the hash function itself.
The rest of the code
for your hash table's gonna be the same.
So, instead what we're gonna do
is we're gonna pass a hash function
to the hash table constructor.
And then the hash table object thus instantiated
will include the hash function we passed to it
when we created it.
Now in order to do this,
first we need to learn
how to pass a function in C++.
And C++ allows us to do that.
Just like we can pass a variable to a constructor,
we can pass a value to a constructor,
we can also pass a function.
And then that function will be incorporated
into the object itself when it's instantiated.
So let's first,
like I said,
see how this passing of functions works.
So here we are back in CLion,
and I'm going to create
a new file
and I'm gonna call it passingFunctions.cpp.
And then we're going to use a new include
that we haven't seen before.
Include functional.
And this header gives us the ability
to pass functions around in C++.
Now we're also,
for the purposes of this demo,
gonna use IO stream.
And we're gonna use string.
And now I'm gonna do something that I'll...
It's cheating a little bit.
It's not a good practice
but for the purposes of this very short demo it's okay.
I'm gonna define
and implement a class right here in this file.
Ideally, this should go in a header file.
So do as I say not as I do.
So without further ado,
we're gonna create a class.
We'll call it very creatively, MyClass.
And then we're going to have a private member.
And this is gonna be the function that we pass in.
So how do we do this?
Well, whenever we create a private member
we have to give the type.
And so the type here is standard function.
And that's provided by
the functional library that we just included.
And this function is going to return a string.
And it's gonna take as a parameter an integer.
And we'll just call it func.
So here we have a private member,
it's a function,
it's gonna return a string,
it's gonna take an integer
and it's called func.
And now we need to implement our constructor,
public.
And here's our constructor,
MyClass.
And we're going to pass in a function.
So it's gonna take a function as a parameter.
Again, we have to give the type.
It's a function,
that takes a string
or that returns a string I should say.
It takes an int
as a parameter itself, func.
And then a very simple constructor indeed
this func = func.
So here we've defined this private member.
It's a function.
When we construct our object,
we're going to pass in a function
and then it's gonna be assigned to this private member func.
And then we need some way to exercise that code.
So I'm gonna create another function here.
A member function that we can call standard string.
This is gonna return a string demo,
gonna pass in an int,
call it X.
And then what we're gonna do is we're gonna return
X applied to that function.
So when we call this function,
when we call demo in our driver code,
we're gonna pass in an integer
and then it's going to pass that integer
to whatever function it is that we passed in
and is stored in this member.
So now that's our class.
Now let's create a function,
standard string.
Because remember our function returns a string
and it's gonna take an integer
and we'll just call it a function.
again very creative.
Int X, so it's passing in an integer X,
and then let's make it very very simple function.
If X modular two,
then return, and it's gotta return a string,
"Number is odd."
Otherwise, return
"Number is even."
I gotta put semi-colons there.
So there's our function.
Takes in an integer.
If the integer is odd,
it returns the string "Number is odd."
If the input integer is even
it returns the string "Number is even."
And that's it.
So that's the function that we're going to pass
to our constructor when we instantiate
an object of my class type.
Now the last thing we need to do is
implement our main function int main,
and that's gonna return zero.
And then we're gonna instantiate my class object.
My class, we'll call it MyClassObject.
And we have to pass in the functions.
Now here we go, a function.
So we're not passing in a variable.
We're not passing in an integer
or a string,
we're passing in this function.
And so now,
what did I do here?
I don't need those braces.
Silly me. Sorry.
So now let's exercise our code.
Standard string S equals MyClassObject demo.
I'm gonna call that demo function.
And demo, as you remember,
is just gonna return
the value that we pass in applied to function, so 42.
And that should return,
"Number is even."
So string will become "Number is even."
And then we'll print that out, std cout,
std endl.
And then let's pass it an odd number
and verify that it returns the string "Number is odd."
So just gonna do odd number three.
So when we run this program,
it should print out,
"Number is even,"
"Number is odd."
And now let's run pass function.
Oh, dear.
I have a typo. Sorry.
I see where it is right away.
I need another pointy bit here.
All right. Sorry.
Just run it again.
Oh, dear.
Redefinition of S,
look at me making mistakes all over the place.
Sorry about this.
All right. Finally.
"Number's even," "Number's odd."
So again, we've created a class.
The class has a private member that holds a function.
We pass a function to the constructor
that becomes incorporated into our class object
when we instantiate.
We can use that function anywhere within our class
if we like,
and that's what we're gonna do when we come to hash tables.
We can also call it from the outside to test it.
Here's the function that we passed in
and here's our code to exercise it.
That's all there is to it.
And we'll use this when we implement hash tables.
That's all for now.