Overloading Functions in C++ Programming | Lecture # 15

Learn C++ Language
Lecture # 15 – Overloading Functions in C++  

Learn C++ Language

Please wait a little bit to Load




Function overloading is an interesting topic and I
find it pretty easy to understand when you get your
head wrapped around it’s not too difficult.
And even though we probably won’t be using it in
our final hangman project it’s still something I think
you should know as a beginner so that when you see
it you understand what is happening.
So what we’re going to do is we’re going to make a
function that says you know void print salary OK.
And this function is going to take in an integer
value and we’ll make that function down here.
Void print salary and X will basically just say see
out in Dyllon X in the line.
And if we run that get just what we expect we’ll
say print salary will say 20000.
When we run it it will just print out 20000 which
is exactly what we want.
But what happens if we want to use this same
function but we want multiple ways to do it.
For instance we’ll say something like C-L enter
your salary and we’ll say no scene salary and salary
can be for instance we don’t know if they are going
to enter a shoestring salary or we’ll say right
now we’ll say it’s an int salary and we’ll pass in
salary here. So whenever they intreated it will get put into the
function. So I’ll say enter your salary will say 23 times
read male pronoun 23:00 But what if you know we’re having you know in this function it won’t really make
sense because you have to declare your data type meaning whatever you declare
they’re going to have to intervene anyways.

But when if you have whenever you start programming
and object oriented design and different types of programming architecture
you’re going to see that sometimes you don’t know what kind of data is coming in.



And even though that won’t be the case here we’re going to pretend like
it is so that you can go ahead
and get your head wrapped around the whole concept. And so let’s assume
that someone enters in a double salary all of a sudden. And when we run it and
we into the salary you know they enter in something like that it’s only
printing out this because it was taking an integer value or what if we really
wanted to print this value.
But we can’t with this print salary function because it’s only asking
for an integer value. Or better yet what if we went into say double salary and
in a string. And when it says into your salary now it doesn’t even run because
you can’t pass in a string at all but what if we didn’t want to pass in a string
and have them type out $2300 as a word.
Well to do that we we can solve this problem by something called a
function overloading. And to do that you simply make multiple prototypes of the
same function but with different parameters. For instance we’ll say void print
salary and we’ll say string We’ll make two will make double.
And then we’ll say boy and print salary sharing and then we’ll come
down here we’ll copy this and we’re going to make we’re going to reprint them
and we’re just going to change these.
So this one will take a string value. And this one will take a double
value.
Well now we have the same function but we have it overloaded with
different parameters.

So we can expect pretty much anything to be included.






So now before when we go in air when we ran this we can run it again
and print salary we can actually enter in twenty three a hundred dollars and
I’ll say 23 and I know I didn’t say hundred dollars because technically we
didn’t do get line we’re going to go ahead and fix that real quick just so I
can show you.
And I know you’ve seen this before in the previous tutorial but
probably not with CNN.
So I’ll say season with good line instead of before we did something
like an output file or input file and then Will story and salary. And now we
run this on we can say you know Twenty three hundred dollars I’ll say twenty
three hundred dollars. But that same token you know we can change this to a
double and it’ll use that same overloaded function. And before you’ll use this
function in before whenever we used.
So let’s see what’s going on here.

Says seek out double salary Prince salary function call to double and I
believe let’s just try to do is I believe get Lon’s only going to work with
string values anyways.
So say salary and when we write it we will say 23:00 hundred point 0 2
4 6 or whatever. OT 4. 6 When you run it it will actually get the get the whole
value window in a new for 6. Because right now it’s on the Default Value of
double precision. Point is Point 2 after the decimal.
That’s where the double comes from.
If we actually changed double to flow which is the same thing as in
doubles. Basically they’re both decimal point values just with they take them
different bytes in memory. When we run it.
See here to make sure we have. We have a prototype around it here. Change
that flow.




I’m just kind of showing you how to overload different tabs and data
when you yourself are now 23:00 point 0 2 3 4 5. Again we get the same thing
and I’m thinking that maybe it’s because it’s defaulting to a floating
precision point of point 0 2 and let’s just check that out OK. Let’s say you
know Seow set precision to 4 fixed and let’s see if that fixes it. And we don’t
have IO manip involved so let’s go ahead and include that. Let’s run that and
there we go.
So now we’re getting the four position points so we had to specify to
set a precision point for after the decimal. Because it’s defaulting to no
matter whether it’s a flow or a double data type. But needless to say you know
this tutorial was more about overloading functions and that’s basically what we
did.
We can intervene now when we run it.




I know that you see when we’re running it we know that it’s a flow
coming in because we had to declare it here. But while I want you to understand
is that in future classes we get the object oriented programming. You may not
know what kind of data is coming in and that’s where function overloading is
important because often times you don’t know if a string or a float is going to
be passed into a function and so you have to prepare for all scenarios.


Check Also

ACCA F2 Management Accounting Lecture 86 – Performance Measurement – Introduction

ACCA F2 Management Accounting  Lecture # 86 – Performance Measurement – Introduction Please wait to …