Definition of function overloading:
Function overloading means that in the same scope, there can be a group of functions with the same function name and different parameter lists. This group of functions is called an overloaded function. ,
So the principle is the same function name, different parameter list. The return value cannot be used as the basis for overloading.
In C class inheritance, the subclass will inherit the functions of the parent class. When you declare an object with the subclass, call a method in the object. It happens that this method is inherited from the parent class, then Call this method directly from the parent class. However, if you define a method with the same name in a subclass, the method will be called from the subclass, not the parent class. And the parent class's methods will be completely ignored. This is the principle of function overloading.
#include <iostream>
// volume of a cube
int volume(int s)
{
return s*s*s;
}
// volume of a cylinder
double volume(double r, int h)
{
return 3.14*r*r*static_cast<double>(h);
}
// volume of a cuboid
long volume(long l, int b, int h)
{
return l*b*h;
}
int main()
{
std::cout << volume(10);
std::cout << volume(2.5, 8);
std::cout << volume(100, 75, 15);
}
The principle of function overloading is that the functions of two functions are very similar, with only a few differences (such as parameter types). If the functions implemented by two functions are quite different, they should be given different names.
Definition of function overloading: Function overloading means that in the same scope, there can be a group of functions with the same function name and different parameter lists. This group of functions is called an overloaded function. ,
So the principle is the same function name, different parameter list. The return value cannot be used as the basis for overloading.
In C class inheritance, the subclass will inherit the functions of the parent class. When you declare an object with the subclass, call a method in the object. It happens that this method is inherited from the parent class, then Call this method directly from the parent class. However, if you define a method with the same name in a subclass, the method will be called from the subclass, not the parent class. And the parent class's methods will be completely ignored. This is the principle of function overloading.
This is an example of function overloading.
The principle of function overloading is that the functions of two functions are very similar, with only a few differences (such as parameter types). If the functions implemented by two functions are quite different, they should be given different names.