I understand what a virtual function is:
My question is: Why wouldn't you make all functions virtual?Code:class Base { public: virtual int DoSomething(); int DoSomethingElse(); }; class Derived : public Base { public: int DoSomething(); int DoSomethingElse(); }; int main(void) { Derived* d = new Derived; Base* b = d; d->DoSomething(); // Calls Derived::DoSomething() b->DoSomething(); // Calls Derived::DoSomething() d->DoSomethingElse(); // Calls Derived::DoSomethingElse() b->DoSomethingElse(); // Calls Base::DoSomethingElse() d->Base::DoSomethingElse(); // Calls Base::DoSomethingElse() delete d; return 0; }
I am just learning C++ and this concept doesn't make alot of sense right now. Also, does a virtual function have to be overriden by a derived class???
Thanks for any help




Reply With Quote