In this ad function Print
void Print(Base c) { c.Print(); }
The parameter is of type Base . Accordingly, the compiler looks for the declaration of the Print function in the Base class and calls it.
In this function call
Print(*A);
The *A argument, of type Class converted to an object of type Base .
You could achieve a similar effect in a Java program if 1) a function parameter would be declared either as a pointer to a Base object or as a reference to a Base object, and the member function of the Print class would be declared as virtual.
In Java, objects are passed by reference, while in your example programs for C ++ objects are passed by value.
In this regard, C # is “in the middle” between C ++ and Java. That is, objects are passed by reference, but functions can be both virtual and non-virtual.
Java
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Base { void Print() { System.out.println( 0 ); } } class Class extends Base { void Print() { System.out.println( 1 ); } } class Ideone { static void Print( Base b ) { b.Print(); } public static void main (String[] args) throws java.lang.Exception { Print( new Class() ); } }
Program output
1
C #
using System; class Base { public void Print() { Console.WriteLine( 0 ); } } class Class :Base { public new void Print() { Console.WriteLine( 1 ); } } public class Test { static void Print( Base b ) { b.Print(); } public static void Main() { Print( new Class() ); } }
Program output
0
Declaring a virtual function
using System; class Base { public virtual void Print() { Console.WriteLine( 0 ); } } class Class :Base { public override void Print() { Console.WriteLine( 1 ); } } public class Test { static void Print( Base b ) { b.Print(); } public static void Main() { Print( new Class() ); } }
Program output
1
C ++
#include <iostream> class Base { public: void Print() { std::cout << 0 << std::endl; } }; class Class : public Base { public: void Print() { std::cout << 1 << std::endl; } }; void Print( Base b ) { b.Print(); } int main() { Class *A = new Class(); Print( *A ); delete A; return 0; }
Program output
0
Declaring a virtual function and passing an object by reference
#include <iostream> class Base { public: virtual void Print() { std::cout << 0 << std::endl; } }; class Class : public Base { public: void Print() override { std::cout << 1 << std::endl; } }; void Print( Base &b ) { b.Print(); } int main() { Class *A = new Class(); Print( *A ); delete A; return 0; }
Program output
1
Declaring a virtual function and passing an object through the pointer
#include <iostream> class Base { public: virtual void Print() { std::cout << 0 << std::endl; } }; class Class : public Base { public: void Print() override { std::cout << 1 << std::endl; } }; void Print( Base *b ) { b->Print(); } int main() { Class *A = new Class(); Print( A ); delete A; return 0; }
Program output
1
As you can see from these examples, in C #, although objects are passed by reference, functions must nevertheless be virtual in order to achieve the same effect as in Java.
In C ++, it is necessary not only to declare functions virtual, but also to explicitly pass objects either by reference or through a pointer.