Polymorphism  «Prev  Next»
Lesson 3 Conversion and overloading
Objective Difference between conversion and overloading.

Difference between Conversion and Overloading in C++

In the first part of this course, we will be focusing on ad hoc polymorphism, which has two forms:
  1. conversion
  2. overloading

C++ Data Conversion and Example

Conversion is the explicit or implicit change of value between types. A function or operator works on several different types by converting their values to the expected type. For example, a default constructor is by de facto a type conversion from the argument's type to the constructor's class type.
In C++, data type conversion is a process of converting one data type to another data type. Here is an example of how to convert a string to an integer using the stoi() function:
#include <iostream>
#include <string>

using namespace std;

int main() {
  string str_num = "123";
  int num = stoi(str_num);
  cout << "The integer value of the string is: " << num << endl;
  return 0;
}
In this example, the string "123" is converted to an integer using the stoi() function, which is part of the string library in C++. The converted integer is then stored in the variable num and displayed on the console using cout.
Output:
The integer value of the string is: 123

Note that data type conversion can sometimes result in loss of information or precision, so it's important to be aware of the potential risks when performing such conversions.

C++ Overloading

Overloading of functions or operators gives the same function name or operator different meanings. When a function is overloaded, the same function name has different interpretations that depend on its signature, which is the list of argument types in the function's parameter list. When an operator is overloaded, the operator has different meanings depending on the types of its operands. In this module, we will be looking at both conversion and function overloading in greater detail. Operator overloading is more involved and will be covered in a later module.

This module examines
  1. function overloading,
  2. copy constructors, and
  3. default arguments.
Function overloading is one of the defining aspects of the C++ programming language. Not only does it provide support for compile-time polymorphism, it also adds flexibility and convenience. Some of the most commonly overloaded functions are constructors. Perhaps the most important form of an overloaded constructor is the copy constructor. Closely related to function overloading are default arguments. Default arguments can sometimes provide an alternative to function overloading.

Function Overloading

Function overloading is the process of using the same name for two or more functions. The secret to overloading is that each redefinition of the function must use either different types of parameters or a different number of parameters. It is only through these differences that the compiler knows which function to call in any given situation. For example, this program overloads myfunc() by using different types of parameters

#include <iostream>
using namespace std;
int myfunc(int i); // these differ in types of parameters
double myfunc(double i);
int main(){ 
 cout << myfunc(10) << " "; // calls myfunc(int i)
 cout << myfunc(5.4); // calls myfunc(double i)
 return 0;
}
double myfunc(double i){
 return i;
}
int myfunc(int i){
 return i;
}

The next program overloads myfunc() using a different number of parameters:
#include <iostream>
using namespace std;
int myfunc(int i); // these differ in number of parameters
int myfunc(int i, int j);
int main(){ 
 cout << myfunc(10) << " "; // calls myfunc(int i)
 cout << myfunc(4, 5); // calls myfunc(int i, int j)
 return 0;
}
int myfunc(int i){
 return i;
}
int myfunc(int i, int j){
 return i*j;
}

As mentioned, the key point about function overloading is that the functions must differ in regard to the types and/or number of parameters. Two functions differing only in their return types cannot be overloaded. For example, this is an invalid attempt to overload myfunc():

int myfunc(int i); // Error: differing return types are
float myfunc(int i); // insufficient when overloading.

Sometimes, two function declarations will appear to differ, when in fact they do not. For example, consider the following declarations.
void f(int *p);
void f(int p[]); // error, *p is same as p[]