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


    (Page 1 of 4 )

    If you are new to the world of object-oriented programming, then your first thought after reading the title of this article is "what the heck could ever transform in code?" The answer is not so trivial that I can explain it in just a sentence, but join me throughout this article and you will get the idea.

    This article is an expansion of my previous article, published over here under the title of Inheritance in C++. In that article I explained how you could spare your coding time and long lines inside your text editor to achieve the same results when you are treating a hierarchy of classes. In that situation, each child object of a root has a few common traits with the other children, and all of these are gathered inside the base class.

    This approach ensures that you can program in general, leave the specific traits of each object for later on, and reuse the common traits by deriving them from the base class. Polymorphism is an extension of this, on an even larger scale.

    To illustrate the problem, let us consider a generic example. Let there be the hierarchy of animals. Most  animals are capable of moving. If you motivate/force a ferocious lion to move, it can cover a distance of 100 meters in ten seconds; however, if you do this with a snail, it won't cover more than one meter -- and even then, you'd have to get one in great shape. You see, both of them are moving, but the results differ.

    Therefore, if you call the member function to move, you had better believe that the Lion would be in front of the snail, supposing that they started from the same point. The question is, how we can treat them generally, so that we can have different consequences when we send the same message to each of them?

    The main idea is to store them generally inside a base class pointer, and when we access the same function, C++ will direct it, down to the corresponding object to which we are pointing.

    With Polymorphism, we can plan and implement a class hierarchy where we can easily “plug in”/add a new class, such that, if it follows a few conventions that we laid down in the base, will work with little or no modification for the base code, and using a base pointer as its access route. We should only modify those segments that need to directly know about the new, existing class.

    For a model, if we just discovered a new frog-like kind of animal that leaps to move, we should not modify the parts where we are only interested in the result of the move. Rather, we should modify the parts that focus on the method, and how it manages to move. Then again, we must only overwrite the specific qualities of the new animal, as the generic ones are already defined in the base class/classes.

    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