C++ と Java の仮想メカニズムに関連する混乱があります。次のプログラムの出力は異なります。私はなぜ理解できないのですか?
Java の場合:
class Base
{
Base()
{
show();
}
public void show()
{
System.out.println("Base::show() called");
}
}
class Derived extends Base
{
Derived()
{
show();
}
public void show()
{
System.out.println("Derived::show() called");
}
}
public class Main
{
public static void main(String[] args)
{
Base b = new Derived();
}
}
出力は次のとおりです。
Derived::show() called
Derived::show() called
C++ では、次の出力:
#include<bits/stdc++.h>
using namespace std;
class Base
{
public:
Base()
{
show();
}
virtual void show()
{
cout<<"Base::show() called"<<endl;
}
};
class Derived : public Base
{
public:
Derived()
{
show();
}
void show()
{
cout<<"Derived::show() called"<<endl;
}
};
int main()
{
Base *b = new Derived;
}
は:
Base::show() called
Derived::show() called
誰でも説明できますか?