A function overload of a class in Java can be done between functions in this class and from functions in the parent class, and the function overloads in the C + + class can only be in this class (that is, they do not include functions from the parent class), which is a very important difference for them. In other respects the requirements are consistent, i.e. the function name is required to be consistent and the parameters are different. Only functions with different return value types cannot be overloaded (there will be compilation errors that prompt the function definition to repeat).
1. The function overloading scope of Java is the visible function in the entire inheritance system
Public classOverrideandoverload { Public Static voidMainFinalstring[] args) { //TODO auto-generated Method StubOVERCLASSB obj =NewOVERCLASSB (); Obj.say ("Hi"); Obj.say (123); }}classOverclassa { Public voidSayFinalString words) {System.out.println ("A:" +words); }}classOverclassbextendsOverclassa { Public voidSayFinal intN) {System.out.println ("B:" +N); }}
2. The function overloads in a class in C + + can only be in the original function, and if the function in this class is coincident with the function name of the parent class, the corresponding function of the parent class is hidden (without overloading the effect). If you continue to use the function of the parent class, the compilation fails:
#include <iostream>#include<cstdlib>using namespacestd;classA { Public: voidSayConst Char*words) {cout<<"A:"<<words<<Endl; }};classB { Public: voidSayintN) {cout<<"B:"<<n<<Endl; }};intMain () {b b; B.say ("haha");//This line generates a compilation error system ("Pause"); return 0;}
Java differs from C + +: overloaded (overloading)