Function Overloading

Umar Lone
A free video tutorial from Umar Lone
Trainer, developer & founder at Poash Technologies
4.5 instructor rating • 7 courses • 49,066 students

Lecture description

Explains function overloading and how to use it 

Learn more from the full course

Complete Modern C++ (C++11/14/17)

The most comprehensive C++ tutorial with C++11/14/17 features

18:47:59 of on-demand video • Updated April 2021

  • Use C++ as an object-oriented language
  • Demystify function & class templates
  • Use STL components in your applications
  • Write real-world applications in C++
  • Apply Modern C++ (C++11/14/17) in programs
English [Auto] Hi, welcome back. In this video, I'll explain functional loading, so we'll start by creating a function that adds to numbers so the function adds to numbers and returns the result. So let's use this function. And then Brindusa. What if further down in the program, we may want to add to double's. So. Here we have some double values. And let's build. So the Gore campaign is fine, but it gives a warning converging from double to impossible loss of data. The reason for this morning is that you are invoking a function with double arguments, but the function accepts only integer arguments. An integer requires four bytes for storage, while a double requires it. So consequently, there will be a data loss when the value from the double goes into an integer. So how do we take care of this? So obviously we'll have to create a separate function for adding two doubles. So all this are double's. And then use it. Here. And let's build. And it builds without any errors and warnings, so you see that we have two implementations for the same behavior, but the behavior appears as function and both the functions have different names. So when the programmer uses these behaviors, that is the functions he or she will have to remember the function needs. So this will cause the code to become slightly more complex while invoking the functions. So ideally, if you have multiple implementations of the same behavior, then the functions implementing the behavior should have the same name so that it is easy for the programmers to invoke those behaviors. We can achieve this in C++ through functional learning and functional learning to our more functions can have the same function name. So we don't need to give a different name to this function. So let's change the function call. Now, when dysfunction is invoked, it will correctly invoke the function that accepts the arguments and the first function call will correctly invoke the function that accepts integers. So how does Compiler know which function to invoke it examines the arguments at the function call? So these arguments are of type integers and this causes the compiler to match them with the function that accepts integers. And for this function call the compiler knows that the arguments are five double, so it matches this function call with this function. So now we are able to represent different implementations of the same behavior with the same function name. So this will make it convenient for the programmers to invoke these functions. The functions would overload only when the arguments are different. Let's read one more function called Brend, which takes an integer and another function with the same name, and this also accepts an integer. So these functions were not altered because arguments are not different. So if we try to invoke dysfunction, the compiler will flag this as an error. So it says it already has a body. So the arguments have to be different. That is the number of arguments or the type of arguments have to be different. So this can be a float. So these two functions of all. So we can build and check. So this will explain the functions can also overload if one of the function accepts a pointer or a reference and the other accepts a constant pointer or a reference. So this will also overload. So in this case, let's try to invoke this with. This address of ex, so this would invoke the first function. But if X were constant, then this call will invoke the second function. So the qualifier will also participate in function or lorang when it is applied on a pointer or a reference. So functional loading allows us to create two or more functions with the same name, but the arguments of those functions should differ in type and or no. If the argument is appointed a reference, then any qualifiers applied on the point of the reference will also participate in all. The return type is ignored. It is not considered during functional. As applied on member functions and classes also participate in all, we will look at this later with glasses. So functional loading allows us to provide different implementations of the same behavior so the behaviors will have the same name. The correct implementation is automatically chosen based on the type of arguments. So this happens at compile time. The compiler is able to choose the correct function that has to be invoked at compile time. So this is also an example of static polymorphism where the behavior that is to be invoked is known by the compiler at compile time. So what is their advantage of functional learning? It is just a convenience for the caller. The caller doesn't have to remember different function names for the same behavior. So even if the functions have the same name, how are they entirely uniquely identified by the compiler and lingo? So this is achieved by a process called as Nemani. So Neimann generates unique names for the functions. This allows the Linko to link the call with the correct or function. The name mangling algorithm varies from computer compiler, but it will still depend on the type and the number of function arguments because the name matching algorithm is unknown. It may not be possible to call C++ functions from C-code or from other languages. C++ provides a compiler director called His Excellency, which can be applied on global functions and variables. This is used to suppress the name mangling of the function or the variable. In a set of overloaded functions, this can be applied to only one function. Using C on a C++ function allows us to call that function from C or other languages. It is applied just before the declaration of the function of the variable and is applied only during declaration, not in definition. So we can see what are the names that company generates for our functions, to see the mangled names of our global functions, our global variables, we can generate a map, find a map value generated by the Linko and contains information about the program that is being linked. We can generate the map in should you go to project properties and expand Lingga, go to debugging and jus just for generate map file option. So once you choose, yes, rebuild your program. And then open the directory of your programs. All right, click here and then choose Open containing folder. So there'll be a debug folder, so there are two debug folders, so you just have to go to the one that is about the project folder and you will see a file with the extension dot. Maybe so open the map file in a text editor. And if you scroll down, you'll see the mangled names of our functions. Let's apply extensively on one of the functions and see what happens. And we build again. Will reload the map five. And see that the brain function, one of the brain functions, is not mangled. No dysfunction can be invoked from sea or other languages. In this example, I have defined the functions in the DOD Sleepify. But ideally, the functions must be declared in a defined and then implemented in order to be defined. So in that case, how would this excellency behave? Let me do one thing. Let me add. I had to find. And I will move the declarations of all these functions in the head of Fine. Obviously, there won't be any implementation. I will move the implementation and another implementation find. Not to invoke the functions, I will have to include their corresponding head fine. Now, here, you need to understand, if you apply extern C on the declaration, then it must be also applied in the definition of the function. Right now, if I build this, it will be fine and let me remove this qualifier so that. It invokes the first brain function that has the externally specified and just for our understanding, let's come out the other brain function. Now, this print function is not usable, it's built again. And it builds as usual. Now, what if ecstasy is not applied on the implementation? Now, in this case, when the compiler compiles the code, it will see that print function is declared with external so it will not mangle the name while it generates the call to the print function. So the print function name will be simply print. However, this print function will be compiled as a C++ function. That is, its name will be mangled. That is why the linker will not be able to link this print function with the function in this file, because here the name will be different. It will be mangled. So if I build this, it will not link. So there is a or error. It says it's not able to locate this print function. So what to do, ensure that you also use see? In the definition. And now this. What if you want to apply externally on multiple functions? Then you can apply it like this. And obviously in the implementation, we will also have to apply externally on this ad function, but I can. A externally on multiple functions, just like we did, and they had to fight. And it works, and let's examine the map, find. And these are the two functions whose names have not been mangled. Instead of having to manually apply extern C in the definitions of the functions, you can simply include the header file that contains the declarations of these functions. Since the declarations already have externally applied, now you don't have to apply externally again. Let's build. And it works. That's all for now. I'll see you in the next lecture. Until then, good bye.