Java differs from C + +: overloaded (overloading)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.