Home arrow C++ arrow 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
(Page 1 of 5 )

This is the second part of my series covering the polymorphic traits in C++ and object-oriented programming, so if you missed it, make sure to catch up with the first part. This article will pick up from where we took a break last time. Just look for Polymorphism in C++ on this web site; start a search or  simply click on my name to see my profile and get the link from there.

Now then, if you managed to follow the first part of this article series, welcome back. We learned what the virtual function itself is, and what direct effect it will have on functions where you declare them with the magic keyword in front of them. To put it as simply as possible, from the point of view of working with the functions where no pointer is implicated, it has no effect whatsoever.

However, when you assign a derived object to the base pointer, whenever you call a virtual function of the base object, if that function was redefined (what in fact in this case is an overwrite) inside the derived class, the derived class function will be called. While we go through this article, you will learn about abstract classes, pure virtual functions, a method for implementing polymorphic behavior, and ultimately why it is so important for destructors to be virtual.

We have much to observe and learn, so here we go. Expanding on the knowledge you amassed last week, you will find that, when the function that you call from a base pointer is always going to be decided at run time, the programmers call that dynamic (late) binding. When you call the function from a reference or you call a non-virtual function, that is static binding, and you will not have any polymorphic behavior.

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