C++
  Home arrow C++ arrow Page 3 - Polymorphism in C++
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++

Polymorphism in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 7
    2009-06-16

    Table of Contents:
  • Polymorphism in C++
  • Pointers
  • Downcasting
  • The Virtual Keyword

  • 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


    Polymorphism in C++ - Downcasting


    (Page 3 of 4 )

    The problem remains: how we can access, from a base pointer pointing to a derived object, the derived object's functions? One solution is downcasting. You use the dynamic_cast keyword.

    The method is simple. This safe casting method will try to cast the input pointer into the specified type of pointer. If this is possible (there exists an “is a” relationship between them), it will return a correct pointer; otherwise, it will return NULL. Now we will cast out the base pointer into a derived type. Append the following lines after the earlier ones.

     

    inTo.str(""); //clean the stream

     

    CarContainer* carP = NULL;

    carP = dynamic_cast<CarContainer*>(baseP);

    if (baseP)

    {

    carP->appendToStream(inTo);

    cout << inTo.str() << endl;

    }

    else

    cout << "The base pointer does not points to a CarContainer Object" <<endl;

     

     

    The new result allows us to do what we wanted to accomplish.

     

    2

     

    2

    CarContainer

    Car Type: Lamborghini Murcielago

    Car Value : 313000

     

     

    Press any key to continue . . .

     

    This will produce a function call for the base, but this is far from polymorphism, and we would need to try to test for each existing derived class from the base. When you have a hundred derived classes, you don’t want to try this, and besides, this will lead to a switch-like logic in programming, making the code less readable and maintainable in the future when the hierarchy is expanded.

    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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek