Home arrow C++ arrow Page 2 - Overview of Virtual Functions

Overview of Virtual Functions

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.

Author Info:
By: Gabor Bernat
Rating: 4 stars4 stars4 stars4 stars4 stars / 2
June 15, 2009
  1. · Overview of Virtual Functions
  2. · Abstract class
  3. · Virtual versus Pure Virtual
  4. · Virtual Deconstructor
  5. · Virtual Deconstructor continued

print this article

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

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials