C++
  Home arrow C++ arrow Page 2 - Overview of Virtual Functions
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Overview of Virtual Functions
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-06-15

    Table of Contents:
  • Overview of Virtual Functions
  • Abstract class
  • Virtual versus Pure Virtual
  • Virtual Deconstructor
  • Virtual Deconstructor continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    More C++ Articles
    More By Gabor Bernat


     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek