search

Home  >  Q&A  >  body text

c++中函数重载的原则是什么啊?

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

怪我咯怪我咯2808 days ago774

reply all(5)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 11:10:32

    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.

    reply
    0
  • 阿神

    阿神2017-04-17 11:10:32

    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.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 11:10:32

    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);
    }
    

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 11:10:32

    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.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 11:10:32

    • Overloaded functions must be distinguished by the number of parameters or parameter types
    • Different function call instances use the same function name

    reply
    0
  • Cancelreply