>So that's why, dave said he didn't really understand it!
Well, inheritance in C++ has a lot of facets to it. There are multiple ways of inheriting, for example. You can have public inheritance, private inheritance, protected inheritance, each of them can be virtual, and they can all be mixed and matched using multiple inheritance. Compare that with Java.
>What is a Virtual Function?
A virtual function is a member function that can be redefined in derived classes to do something different than the base class. It's different from how non-virtual functions in derived classes override their base class counterparts because using virtual functions you can create behavioral polymorphism. That's a fancy way of saying that you can create an object of the derived class, assign it to a pointer or reference to the base class, and the redefined member function will be called instead of the base class function:
class Base {
public:
void f1() { cout<<"Base class"<<endl; }
virtual void f2() { cout<<"Base class"<<endl; }
};
class Derived: public Base {
public:
void f1() { cout<<"Derived class"<<endl; }
void f2() { cout<<"Derived class"<<endl; }
};
int main()
{
Base a;
Derived b;
Base& r = b;
}
In the above code, if you say a.f1(), you'll get "Base class" as expected. If you say a.f2(), you'll also get "Base class" as expected. The same goes for b, b.f1() and b.f2() both print "Derived class". The fun part comes when you call f1 and f2 from r.
C++ will look at the type of the object itself when calling member functions unless the member function is virtual. That is, because r is a reference to Base, the Base member functions will be called instead of the Derived member functions. You can verify that by calling r.f1(). It prints "Base class". r's
static type is reference to Base.
Now, since r actually references an object of Derived, the
dynamic type of r is Derived. This is where virtual member functions come in. A virtual member function will be called on the dynamic type of an object rather than the static type, so calling r.f2() will print "Derived class" because f2 is virtual and r's dynamic type is Derived.
That's really all a virtual function is; it's a way to implement behavioral polymorphism.