1#include <iostream>2 using namespacestd;3 /*Run this program using the console Pauser or add your own getch, System ("pause") or input loop*/4 classA {5 Public:6 voidm () {7 n ();8 }9 Ten Public: One Virtual voidN () { Acout<<"The n function of the parent class was called"; - } - the }; - - classB: PublicA { - + Public: - voidm () { + a::m (); A } at - Public: - voidN () { -cout<<"called the N function of the subclass"; - } - in }; - intMainintargcChar**argv) { to b b; + b.m (); - return 0; the}
Class a{public void M () {n ();} public void N () {System.out.println ("n function called the parent Class");}} Class B extends a{public void m () {super.m ();} public void N () {System.out.println ("N function called sub-Class");}} public class Main {public static void main (string[] args) {//TODO auto-generated method stub B b=new B (); B.M ();}}
C + + Output parent class, Java output subclass.
Reason Java default polymorphism, dynamic binding. C + + needs to be added to virtual, plus the sub-class
The same code has different results in Java and C + +