Function Overloading in C++ Programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Applause] [Music] Yoga is done by our first impulse efforts and in this video tutorial we are going to be learning the concept of function overloading and we will see what function overloading means the theoretical aspect as well as we will see a program and how to implement function overloading so before we start off with this topic it is very important that you understand the concept of functions in C++ and for that you can watch the previous two video video tutorials of this playlist and I will put the links in the description so with that being said let's get started so what is function overloading mean in C++ so let me just read out the definition or a basic description of function loading so function overloading is a feature in C++ programming where two or more functions can have the same name but different parameters now you already know what parameters are that is the values that we pass in the opening and closing round braces in the function prototype or function signature now as I mentioned if you don't know what functions are you can check out the previous two video tutorials which are on functions itself from this playlist so function overloading basically can be considered as an example of polymorphism which is a feature of C++ and we will discuss polymorphism in detail in further tutorials so what does different parameters mean now there are certain rules that are all loaded function must follow so these are the different two different parameters or three different conditions so an overloaded function can have different type of parameters different number of parameters and different sequence of parameters so to the right we have six different cases of overloaded function so number one you can see we have void print so print is the function name void is written type which means it is not returning any value and you have the opening and closing round brackets so in this case we do not have any parameter now the second function is again print so the name of the function is same however in this case we are passing an integer value as a parameter the third one we are passing one parameter but this is a different type of parameter so this satisfies the first case that is different type of parameters so the number two and three examples of that overload function is a case of different type of parameters number 1 and 2 of the example is the case of different number of parameters so in the first case we have 0 parameters being passed and the second example we have one integer parameter being pass so this is allowed for the third case different sequence of parameters so you can see we have example number five and six so in both cases we are passing two parameters one is integer and one is double but if you observe five and six the sequence that is the order in which the parameters are passed is swapped what is changed so in number five we have first integer and then double and in number six we have four is double and then integer so again this is a case of correct function overloading or correct overloaded function so this was the theoretical aspect of function overloading but why do we exactly need overloading so now in a case wherein you need to print different values depending upon what user passes say let us take this example itself now if I use your passes nothing you want to print some message now if the user passes an integer value you want to print that integer value so in so a function overloading didn't exist you would have made different functions with different names so you would have wanted to make six different functions which basically are just going to print the message or is basically going to print whatever the users passes so the function ID is same but just because function overloading didn't exist in that case you have to create six different names for the same functionality so this is where function overloading comes into picture using a single name you can like perform same activities or you can implement the same functionality so now let's go ahead and see a practical example so we will try to create a program and overload a function so go ahead and quickly open up your ID and type in the code that I have already written so this is the basic structure of a C++ program topic is function overloading so in this program we will try to perform addition of different data types that is integer float doubles and we will try to perform addition using the same function name but we will try to overload that function so let's just create the function first so the function name is ADD and let's try to create int add we passed into a comma in B so this is the first function the name of the function is add and it is going to return an integer value so I will say return a plus B so whatever parameters are being passed it is going to perform addition and just going to return that value now what is the user passes to float all double values that is the data type being passed as double so we have the symbol point in consideration so now what I'll say is bevels add double a comma BB so in this case the function name is same as you can see we have added over here we have had over here but here the parameters are passed as double and double and then since the addition of the two double variables will again be a double integer or double variable so the return type is going to be double we'll say return D plus B now we can also overload this function to take in three parameters so let's start to create that I will say int add int a comma and V comma in C here the function would return addition of three variables so we have created three different variants of the same function name that is add and essentially it is performing the same logical task that is the operation is addition operation however the number of parameters the type of parameters is different and let's take a case where in the sequence of parameters is also different so I will say double add int a comma BB return a plus B this copy and paste this and just change the sequence that is double a and int B so we will just try to add comments and try to number these functions so this is number one you will say number two is number 3 number 4 and number 5 so the reason I gave this functions number is because you can then see which function is being called in the main function so in the main function let's try to call all these five different functions in fact what we can do is inside each function we can print out its number so as you see out function 1 and then inside the second I will say function to say this L say function 3 here is a function four and last year is a function five so first let's try to pass two integer variables so that function one is going to be called and we can verify this in this statement itself to see out add 1 comma 2 so this is basically the first function going to be called and weensy and this will be confirmed if it prints this message let's try to call the second function which has two double variables so I'll say 3 point 5 comma 4 point 5 so this has to call the second variant for the third one we have 3 different integer variables so say 3 comma 4 comma 5 for the fourth one we have forced integer so it's at 3 and the second one is a double and for the fifth variant we have forced one else double and the second one is integer so is at 3 point 5 comma 4 just save this as function overloading dot CPP let's try to compile and run this first let me just try to compile so that we can check if there is any syntax errors ok so I just missed out a semicolon over here let me just save this and try to compile it again ok again missed out semicolons so this is a very common mistake that most programmers commit and they forget to end the statement with semicolons just save this compile it again and it should compile correctly ok so we got the lump validation results there are zero errors and zero warnings so let's try to run this and click on run and there you go with the message as function and the value 1 then we have actually printed out the addition of two numbers so it's you can see it is not properly structured so let me just add an escape sequence over here or else a addition is let me just save this and compile and run okay so as you can see for the function one was called because you can see this message printed function 1 and the Edition is 3 because you can see over here that is passed 1 & 2 for the next we have function - being called and the addition is 8 because you can see we have passed 3 point 5 and 4 point 5 then the function 3 is called and the Edition is 12 because we have passed 3 4 & 5 for the function 4 vs 7 point 5 and similarly for function 5 we have seven point five because we just swapped the places of integer and double variables over here so this is our function overloading works and depending upon the number of parameters the type of parameters and sequence of parameters the appropriate function is being called so function overloading helps us to all over function with same name and performs similar functionality with the same name so that you don't have to create different names for different functions so that's about it for function overloading and I hope to understood the concept of function overloading why we need function overloading and how to practically implement function overloading in a program so that's it for this video guys if you have any queries you can put them in the comment section and if you like this video give it a thumbs up share it with your friends and don't forget to subscribe to our Channel peace
Info
Channel: Simple Snippets
Views: 120,425
Rating: undefined out of 5
Keywords: c++ function overloading, easy function overloading program in cpp, function overloading c++, example of function overloading in c++, function overloading, function overloading example, overloading functions c++, functions overloading, how to achieve function overloading, function overloading in c++, what is function overloading in c++, function, function overloading in c++ youtube, overloading in c++, overloading, c++, c++ programming, simple snippets, method overloading
Id: dh0mKAPFzlQ
Channel Id: undefined
Length: 10min 2sec (602 seconds)
Published: Fri Jul 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.