The differences between C/C ++ and Java function overloading!

Source: Internet
Author: User

# Include <iostream> <br/> using namespace STD; </P> <p> class base {<br/> public: </P> <p> virtual void FN (int x) {<br/> cout <"in base class, int x =" <x <Endl; <br/>}< br/>}; </P> <p> class subclass: public base {<br/> public: </P> <p> // function overloading. This method can be used in Java, not in C/C ++ <br/> virtual void FN (float X) {<br/> cout <"in subclass, float x = "<x <Endl; <br/>}< br/>}; </P> <p> void test (base & B) {<br/> int I = 1; <br/> B. FN (I); <br/> float f = 2.0; <br/> B. FN (f); <br/>}</P> <p> int main () {<br/> base BC; <br/> subclass SC; <br/> cout <"calling test (BC)/n"; <br/> test (BC); <br/> cout <"calling test (SC) /n "; <br/> test (SC); <br/> return 0; <br/>}< br/>

 

 

Calling test (BC)
In base class, int x = 1
In base class, int x = 2
Calling test (SC)
In base class, int x = 1
In base class, int x = 2

Run successfully (total time: 312 ms)

 

Note that all functions in the parent class are called.

 

 

Package CN. vicky; </P> <p> public class mytest {</P> <p> Public void add (int I) {<br/> system. out. println ("in parent"); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> mytest 2 m2 = new mytest2 (); <br/> m2.add (1); <br/> m2.add (1.1 ); <br/>}</P> <p> class mytest2 extends mytest {</P> <p> Public void add (double I) {<br/> system. out. println ("in child"); <br/>}< br/>

 

In parent
In child

Java can be distinguished...

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.