The word virtual is strange enough on its own. It is defined philosophically as "that which is not real" but contains all of the properties of the real object. However, ultimately something virtual is "fake." Virtual functions are similar in concept, so follow along with me through this article to discover how to use them and when to do so in the C++ language.
Overview of Virtual Functions - Abstract class (Page 2 of 5 )
Abstract classes are the ones that exist only in theory, and there is no way you can perceive an instance of them. Imagine a class hierarchy of the geometrical items, with a common base class under the name GeEntity. From this, we will later form later the circle, line, and so forth.
If we want to draw all objects on the screen from the internal database, we would like to treat them as generally as we can. To achieve this, we will pass on the objects that need to be drawn via the base pointer and exploit the virtual functions, so inside a draw () member function each class will resolve the draw procedure in its own way.
The only problem is this: imagine what happens when you pass on, inside this stream, a pointer to a GeEntity. How would you draw a GeEntity on the screen? An entity of itself simply has no meaning. It exists for the sole purpose to allow us to store all of them inside a vector (via the base pointer) and to increase reusability of the code.
Abstract classes resolve exactly this issue. They can never have an instance, and you can see them as an incomplete derivable (base) class for which the child classes must complete the missing parts; otherwise, they on their own are incomplete (abstract) classes.
This technique allows us to provide a class from which others can inherit, but at the same time avoid invalid representations. These classes are so generic that some sequences of them first need to get a definition before you can use them.
This is just like the case of our port with the incoming cargo via the container. A container must exist on a level with some cargo inside it. For example, it may be empty (thus containing air), have a car, boxes with bananas, and so forth. Figuring out how to implement this leads us to the virtual functions. Moreover, it leads us specifically to the pure virtual functions.
Declaring pure virtual functions will tell/assure the compiler that the function doesn't make sense there, but it will certainly do so in its derivatives, and they will be present when we call the function from the base pointer. Every container will have a cargo with a price (even if it is empty) so I implemented a getPrice () function. It does not makes sense for the function to exist on a container in general, but will have all of its derivatives.
The routine of declaring a pure virtual function involves assigning the function the zero value, just as I have done inside the BaseContainer:
virtual int getPrice () const = 0;
The "=0" is the pure specification. As you'll see in the following example, no implementation is allowed or necessary for this function on the BaseContainer. If you try to declare a type of this class, the following error will be thrown:
1>...: error C2259: 'BaseContainer' : cannot instantiate abstract class
1> due to following members:
1> 'int BaseContainer::getPrice(void) const' : is abstract