C++
  Home arrow C++ arrow Page 2 - 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++ - Pointers


    (Page 2 of 4 )

    To help you better understand today’s lecture, I constructed a simple class hierarchy. We are inside a major port and we are receiving different objects inside containers form the ships that arrive daily. Maintaining an up-to-date database is no simple task; we shall try to do so by using the inheritance and OOP concepts. The hierarchy looks like this:

     

     

    I tried to keep it as simple as possible and created just a few generic types of incoming containers: one for the car, one for the bananas and one that came in with no cargo inside. All of them inherit publicly the functions from the Base Container, holding the ID of the container and a place to hold the name of the shipment.

    To understand polymorphism, we must play around a little with the pointers. Start with the most straightforward ones. Assign a base object to a base pointer and invoke base object functionality. Respectively assign a derived object to a base pointer and invoke derived class functionality.

     

    BaseContainer base(2);

    BaseContainer* baseP = &base;

    cout << baseP->ID() << endl;

     

    CarContainer car(2, "Lamborghini Murcielago", 313000);

    CarContainer* carP = &car;

    cout << carP->name() << endl;

     

    Moreover, what we get on the screen is what we expect; there's nothing devilish here:

     

    2

    Lamborghini Murcielago

    Press any key to continue . . .

     

    Things get a little complicated when we start to mix them. Remember that I presented in my Inheritance in C++ article the idea that a derived class has an “is a” relationship with the base class. In other words, it is a sort of the base type; it's just that it is a little more specific. (For instance, a Toyota Camry is a type of car). Due to this, the compiler will allow you to store a derived object inside a base pointer.

    Therefore, knowing that I’ll be able to present this capability of C++, I implemented a function that is declared both in the base class and the derived class. Both have the same name and return type, and by using this, I just replaced the function inside the base class with a new one. Inside the base, I just add the ID into the input stream, while for the derived class, I also add the content of the new variables that are specific only to the derived class.

     

    stringstream inTo;

    CarContainer car(2, "Lamborghini Murcielago", 313000);

     

    BaseContainer* baseP = &car;

    baseP->appendToStream(inTo);

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

     

    The code above will leave no compilation errors, and produce the following result on the console screen:

     

    2

     

    Press any key to continue . . .

     

    Calling any function from the base pointer, regardless of whether you have an object pointing to a derived class, will call the base class's function. In the other sense, it does not work. The “is a” relationship goes in one direction. Assigning, to a derived class pointer, a base object, will result in error.

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