Function Overloading C++

Feb 5, 2017 at 5:40am
Source code: Tutorialspoint

Question: I want to know what is function overloading used for? How is it useful?



Google: Method Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include <iostream>
using namespace std;
 
class printData {
   public:
      void print(int i) {
         cout << "Printing int: " << i << endl;
      }

      void print(double  f) {
         cout << "Printing float: " << f << endl;
      }

      void print(char* c) {
         cout << "Printing character: " << c << endl;
      }
};

int main(void) {
   printData pd;
 
   // Call print to print integer
   pd.print(5);
	
   // Call print to print float
   pd.print(500.263);
	
   // Call print to print character
   pd.print("Hello C++");
 
   return 0;
}



Printing int: 5
Printing float: 500.263
Printing character: Hello C++
Last edited on Feb 5, 2017 at 5:41am
Feb 5, 2017 at 5:52am
Feb 5, 2017 at 10:48am
Ok, but how does the program understand/work when you pass a value to the function or call a function in the public section of class. Does it work depending on the order of methods in the class ?
Feb 5, 2017 at 12:03pm
Does it work depending on the order of methods in the class ?

No, the program works by matching the signature from the available overloads. The reply in the following link describes what exactly a function signature comprises of:
http://www.geekinterview.com/talk/19553-what-exactly-function-signature-c-contain.html

Consider the overloaded ctor for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

struct Person
{
    std::string m_name;
    int m_age;

    Person() {}
    Person(const std::string& name, const int& age)
    : m_name(name), m_age(age){}
};

int main()
{
   Person a(std::string("John Smith"), 42);//calls second ctor
   Person b;//calls first ctor
}

Feb 6, 2017 at 1:24am
Ok, I read the information in the link you provided, so briefly the signature is
a) The number of parameters
b) The data type of parameters
c) The order of appearance


My final question is:

Given the first sample code I provided above, does the program understand to send 5 to the method in the class which has the data type of int in its parameter just because 5 is an int ? so in another words, the idea of calling and passing by value depends on what data type you are trying to pass to the function and what data type the function/method has in its parameter to receive the right number ?

1
2
3
4
5
6
7
8
9
10
11
   printData pd;
 
   // Call print to print integer
   pd.print(5);
	
   // Call print to print float
   pd.print(500.263);
	
   // Call print to print character
   pd.print("Hello C++");
 
Last edited on Feb 6, 2017 at 1:25am
Feb 6, 2017 at 2:25am
does the program understand to send 5 to the method in the class which has the data type of int in its parameter just because 5 is an int

Indeed it does, try 5.0 instead of 5 and you'll find that the float overload is being called. If you are interested, the way the compiler achieves this is through name decoration or name mangling: https://msdn.microsoft.com/en-in/library/56h2zst2.aspx

Feb 6, 2017 at 4:33am
Awsome, thank you for your info and help.
Topic archived. No new replies allowed.