This article is aimed at programmers who are new to C++ or an initiate to programming in general.
Virtual
The term conjures up a vision of something that is non-tangible. That is, something that doesn't exist in the physical world. In C++, this meaning is extended to describe functions which may not exist. By that, we mean that it may not be defined.A virtual function, therefore, is a function which may not be real. While a class may call a function it defines, a virtual function could also exist somewhere else in it's object hierarchy. The class doesn't know what function it's actually calling until it is compiled or possibly until runtime.
Essentially, the virtual keyword signals to a developer that the function is intended to be able to be implemented or overridden in a derived class.
The Base Class (Non-Virtual)
To demonstrate, start by creating a simple base class with two public functions: Foo() and Bar(). Inside the Foo() function, call Bar(). It may be helpful to add some output so you know which function is being called.Something like this:
class Base { public: void Foo() { cout << "Base::Foo" << endl; Bar(); } void Bar() { cout << "Base::Bar" << endl; };};In a main function, instantiate a new Base class and call Foo(). You should get ouput similar to this:
Base::Foo Base::BarThis is pretty standard fair and works as expected.
The Derived Class
Next, create a derived function that inherits from Base. In it, create a function with the same name and signature as Bar(). If printing out a statement, make sure you update it to report the new class name.class Derived : public Base { public: void Bar() { cout << "Derived::Bar" << endl; } };Call Foo() on this new class and you'll see that you get the same output. The function Bar() is only called on the Base class.
The Base Class (Virtual)
Add the virtual keyword to the function Bar() in the Base class then try running the code again. The code should look like this:class Base { public: void Foo() { cout << "Base::Foo" << endl; Bar(); } virtual void Bar() { cout << "Base::Bar" << endl; };};You should get output like this:
Base::Foo Derived::BarThis time, Bar() was called in the Derived class instead of the Base class. Why?
No comments:
Post a Comment