C++ 函数重载和函数覆盖的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11912022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Differentiate between function overloading and function overriding
提问by Amol Joshi
Differentiate between function overloading and function overriding in C++?
C++中函数重载和函数覆盖的区别?
采纳答案by user827992
You are putting in place an overloading when you change the original types for the arguments in the signature of a method.
当您更改方法签名中参数的原始类型时,您正在实施重载。
You are putting in place an overriding when you change the original Implementation of a method in a derived class.
当您更改派生类中方法的原始实现时,您正在实施覆盖。
回答by Prasad G
Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.
在 C++ 中重载一个方法(或函数)是定义同名函数的能力,只要这些方法具有不同的签名(不同的参数集)。方法覆盖是继承类重写基类虚方法的能力。
a) In overloading, there is a relationship between methods available in the same class whereas in overriding, there a is relationship between a superclass method and subclass method.
a) 在重载中,同一类中可用的方法之间存在关系,而在覆盖中,超类方法和子类方法之间存在关系。
(b) Overloading does not block inheritance from the superclass whereas overriding blocks inheritance from the superclass.
(b) 重载不会阻止从超类的继承,而覆盖会阻止从超类的继承。
(c) In overloading, separate methods share the same name whereas in overriding, subclass method replaces the superclass.
(c) 在重载中,不同的方法共享相同的名称,而在覆盖中,子类方法替换超类。
(d) Overloading must have different method signatures whereas overriding must have same signature.
(d) 重载必须具有不同的方法签名,而覆盖必须具有相同的签名。
回答by Anirudha
Function overloading is done when you want to have the samefunction with different parameters
当您希望具有不同参数的相同函数时,会进行函数重载
void Print(string s);//Print string
void Print(int i);//Print integer
Function overriding is done to give a different meaning to the function in the base class
完成函数覆盖是为了给基类中的函数赋予不同的含义
class Stream//A stream of bytes
{
public virtual void Read();//read bytes
}
class FileStream:Stream//derived class
{
public override void Read();//read bytes from a file
}
class NetworkStream:Stream//derived class
{
public override void Read();//read bytes from a network
}
回答by Jahid
Overriding means, giving a different definition of an existing function with same parameters, and overloading means adding a different definition of an existing function with different parameters.
覆盖意味着,给一个具有相同参数的现有函数一个不同的定义,重载意味着添加一个具有不同参数的现有函数的不同定义。
Example:
例子:
#include <iostream>
class base{
public:
//this needs to be virtual to be overridden in derived class
virtual void show(){std::cout<<"I am base";}
//this is overloaded function of the previous one
void show(int x){std::cout<<"\nI am overloaded";}
};
class derived:public base{
public:
//the base version of this function is being overridden
void show(){std::cout<<"I am derived (overridden)";}
};
int main(){
base* b;
derived d;
b=&d;
b->show(); //this will call the derived overriden version
b->show(6); // this will call the base overloaded function
}
Output:
输出:
I am derived (overridden)
I am overloaded
回答by MERLIN THOMAS
1.Function Overloading is when multiple function with same name exist in a class. Function Overriding is when function have same prototype in base class as well as derived class.
1.函数重载是当一个类中存在多个同名函数时。函数覆盖是指函数在基类和派生类中具有相同的原型。
2.Function Overloading can occur without inheritance. Function Overriding occurs when one class is inherited from another class.
2.函数重载可以在没有继承的情况下发生。当一个类从另一个类继承时,就会发生函数覆盖。
3.Overloaded functions must differ in either number of parameters or type of parameters should be different. In Overridden function parameters must be same.
3.重载函数必须不同,要么参数数量不同,要么参数类型不同。在覆盖的函数参数必须相同。
For more detail you can visit below link where you get more information about function overloading and overriding in c++ https://googleweblight.com/i?u=https://www.geeksforgeeks.org/function-overloading-vs-function-overriding-in-cpp/&hl=en-IN
有关更多详细信息,您可以访问以下链接,获取有关 C++ 中函数重载和覆盖的更多信息 https://googleweblight.com/i?u=https://www.geeksforgeeks.org/function-overloading-vs-function- overriding-in-cpp/&hl=en-IN
回答by Parmar bharat
Function overloading is same name function but different arguments. Function over riding means same name function and same as arguments
函数重载是同名函数但不同的参数。函数覆盖意味着同名函数和参数相同
回答by xeem
in overloading function with same name having different parameters whereas in overridding function having same name as well as same parameters replace the base class to the derived class (inherited class)
在重载具有不同参数的同名函数中,而在重载具有相同名称和相同参数的函数中,将基类替换为派生类(继承类)
回答by archana
Function overloading
- functions with same name, but different number of arguments
Function overloading
- 具有相同名称但参数数量不同的函数
Function overriding
- concept of inheritance. Functions with same name and same number of arguments. Here the second function is said to have overridden the first
Function overriding
- 继承的概念。具有相同名称和相同数量参数的函数。这里据说第二个函数覆盖了第一个函数
回答by Praveen_Shukla
Overloading means having methods with same name but different signature Overriding means rewriting the virtual method of the base class.............
重载意味着具有相同名称但不同签名的方法覆盖意味着重写基类的虚方法......................
回答by Rashmit
Function overloading may have different return types whereas function overriding must have same or matching return types.
函数重载可能具有不同的返回类型,而函数覆盖必须具有相同或匹配的返回类型。