Let's say I create a class A with a virtual function print for dynamic binding.
Should I then use the virtual keyword in the derived class' print function as well? If so, why? because it works without.
#include <iostream>
using namespace std;
class A {
public:
A() {}
virtual ~A() {}
virtual void print() { cout << "A" << endl; }
};
class B : public A {
public:
B() : A() { }
void print() { cout << "B" << endl; } // why use the virtual keyword here?
};
int main()
{
A* a[100];
a[0] = new A();
a[1] = new B();
a[0]->print();
a[1]->print();
while (true);
return 0;
}