c++中函数重载的原则是什么啊?
怪我咯
怪我咯 2017-04-17 11:08:32
0
5
773

如题,我是应届生。这个问题是我面试的时候遇到的,找了很多,不知道正确的原则到底是什么。

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(5)
左手右手慢动作

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.

#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.

小葫芦
  • Overloaded functions must be distinguished by the number of parameters or parameter types
  • Different function call instances use the same function name
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template