The similarities and differences of virtual functions between Java and C + +

Source: Internet
Author: User

Reference blog: Dot Me

  

Important: The normal function in Java defaults to a virtual function, so the behavior of dynamic binding is the default, and C + + must declare a method as virtual (virtual keyword), which is dynamically bound when executed, with a detailed distinction between code and annotations.

The code roughly: implements the parent class animal class, contains the data member name and the age, as well as implements the Eat method and the Informa method, the subclass dog class inherits animal, and implements the method the overwrite. Both Java and C + + are not shown as virtual functions, but the observation output shows that Java implements dynamic binding, and C + + does not, only the corresponding function with the virtual keyword, the implementation of dynamic binding. This is the difference between Java and C + + processing

C + + code:

#include <cstdio>#include<cstring>#include<iostream>using namespacestd;classanimal{ Public:    Charname[ -]; intAge ; Animal () { age=0; name[0] =0; } Animal (Char* Nname,CharNAge)        {strcpy (name, nname); Age=NAge; }    voideat () {printf ("Animal can eat!\n"); }    //Here you can add the virtual keyword to observe the output difference    voidinformation () {printf ("%s is a Animal, it ' s%d years old!\n", name, age); }};classDog: Publicanimal{ Public: Dog (): Animal () {} dog (Char* Nname,intnAge): Animal (Nname, NAge) {}voideat () {printf ("Dog can eat!\n"); }    voidinformation () {printf ("%s is a Dog, it ' s%d years old!\n", name, age); }};intMain () {Animal* A =NewAnimal ("A",Ten); Animal* B =NewDog ("B", -); A-information (); //C + + must show that it is declared as a virtual function to be polymorphic, or else it simply calls the method of the parent class and does not call the child class.B->information (); return 0;}
View Code

Java code:

Animal class:

 PackageCom.hao; Public classAnimal {String name= ""; intAge = 0; Animal () {}/**     * @paramName of the animal *@paramage of an animal*/     PublicAnimal (String name,intAge ) {        Super();//its super class is actually the object class         This. Name =name;  This. Age =Age ; }         Public voideat () {System.out.println ("Animal can eat!"); }         Public voidinformation () {System.out.printf ("%s is a Animal, it ' s%d years old!\n", name, age); }        }//Animal
View Code

Dog class:

 PackageCom.hao; Public classDogextendsAnimal {/*** Default constructor*/     PublicDog () {Super(); //TODO Auto-generated constructor stub    }    /*** @function with parameter constructor *@paramName of dog name *@paramage of the dog*/     PublicDog (String name,intAge ) {        Super(name, age); //TODO Auto-generated constructor stub    }    /*(non-Javadoc) * @see com.hao.animal#eat ()*/@Override Public voideat () {//TODO Auto-generated method stubs//super.eat ();System.out.println ("Dog can eat!"); }    /*(non-Javadoc) * @see com.hao.animal#information ()*/@Override Public voidinformation () {//TODO Auto-generated method stubs//super.information ();System.out.printf ("%s is a Dog, it ' s%d years old!\n", name, age); }    }
View Code

The main class that contains the primary function:

 package   Com.hao;  public  class   Main { static  void   main (string[] args) {//  TODO auto-generated method stub  Animal A = new  Animal ("A", 10 = new  Dog ("B", 20 // java uses dynamic bindings by default---differs from C + +   A.eat ();            B.eat (); }}
View Code

The similarities and differences of virtual functions between Java and C + +

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.