Let's assume this scenario in Visual C++ 2010:
#include <iostream>
#include <conio.h>
using namespace std;
class Base
{
public:
int b;
void Display()
{
cout<<"Base: Non-virtual display."<<endl;
};
virtual void vDisplay()
{
cout<<"Base: Virtual display."<<endl;
};
};
class Derived : public Base
{
public:
int d;
void Display()
{
cout<<"Derived: Non-virtual display."<<endl;
};
virtual void vDisplay()
{
cout<<"Derived: Virtual display."<<endl;
};
};
int main()
{
Base ba;
Derived de;
ba.Display();
ba.vDisplay();
de.Display();
de.vDisplay();
_getch();
return 0;
};
Theoretically, the output of this little application should be:
- Base: Non-virtual display.
- Base: Virtual display.
- Base: Non-virtual display.
- Derived: Virtual display.
because the Display method of the Base class is not a virtual method so the Derived class should not be able to override it. Right?
The problem is that when I run the application, it prints this:
- Base: Non-virtual display.
- Base: Virtual display.
- Derived: Non-virtual display.
- Derived: Virtual display.
So either I didn't understand the concept of virtual methods or something strange happens in Visual C++.
Could someone help me with an explanation?