Compare function overloading, overwrite, and hide in C + + with Java

Source: Internet
Author: User

in C + +

1. Overloading of functions

  The overloads of functions in C + + require that the same parameter list for the function name must be different for the same or not the same as the return value type, and that the compilation environment can choose exactly which function to call when the function is called. For example: void display (), void display (int i), void dispaly (double i), void display (int i,double i);

void display (double i,int i); These functions are overloaded with each other; if only the return value type is different, it is an incorrect overload such as: int display (); with void display (); is an incorrect overload;

In addition, overloads of C + + functions can only occur in classes of the same level, and functions of the same name between the base and derived classes do not form overloads

2. Hiding the function

A function in C + + is hidden between a function of the same name between a base class and a derived class, and a function of a derived class that hides the base class, and if the function of the base class is the same as the function name of the derived class, and the second argument list is different, the function of the base class is also hidden, and the scope must be

1#include <iostream>2 using namespacestd;3 class Person4 {5      Public :6         voidDisplayinti) {cout<<"The base class is called"<<Endl;}7         //int display (int i) {return 0;}8 };9 classStudent Public PersonTen { One     Public:voidDisplay () {cout<<"derived classes are called"<<Endl;} A }; - intMain () - { theStudent *stu=Newstudent (); -     //stu->display (0);//Error: An overload of a function in C + + can only be overridden in the same class at the same time -     //stu->person::d isplay (0);//can write like this -Stu->display ();//Output derived class is called

/* Person *per=new student ();
Per->display (); / /Output base class is called//This is the difference between the overlay
*/ + return 0 ; (}

3. Coverage of functions

The conditions for function overrides in C + + are:

(1) The function of the base class must be a virtual function

(2) The two functions that are overwritten are located in the derived class and the base class

(3) the function name and argument list must be exactly the same

#include <iostream>using namespacestd;classperson{ Public :    Virtual voidDisplay () {cout<<"The base class function is called"<<Endl;};} ;classStudent Publicperson{ Public :    voidDisplay () {cout<<"A derived class function is called"<<Endl;}};intMain () { person*per=NewPerson (); Per->display ();//The output base class is calledDelete per; Per=Newstudent (); Per->display ();// output derived class function is called//This is the difference between covering and hiding    return 0;}

                                In Java

1 Overloading of functions

overloading of functions in Java can occur between a base class and a derived class that also requires the same function name, a different argument list, and a return value type that can be the same.

1 classBaseClass2 {3      Public Static voidDisplayinti) {System.out.println ("BaseClass");}4 }5  Public classThisClassextendsBaseClass6 {7      Public Static voidDisplay () {System.out.println ("ThisClass");}8      Public Static voidMain (String[]args)9     {Ten display ();//Call a function of a derived class OneDisplay (0);//function that calls the base class
////////////////////////////////////////////////////////////
Overwrite and hide are not the same as C + +
A } -}

   In this example, the void display (int i) in the base class BaseClass, and the Void display () in the derived class ThisClass, makes up the overloaded

2. Coverage of functions

  overriding a function in Java requires that the base class be exactly the same as the function name and argument list of the derived class . Examples are as follows:

1  Public classPrivateoverride2 {3      Public  voidF () {System.out.println ("Private f ()");}4     //private void F () {System.out.println ("Private f ()");}5      Public Static voidMain (String[]args)6     {7Privateoverride po=NewDerived ();8 po.f (); //Output public f ()//indicates that a derived class function overrides a function of the base class 9     }Ten      One } A classDerivedextendsPrivateoverride - { -          Public voidF () {System.out.print ("Public f ()");} the}

3. In the above example, if the access attribute of F () in the base class is set to private, the output private f () concludes that only non-private methods can be overridden

Compare function overloading, overwrite, and hide in C + + with Java

Related Article

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.