<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 30 Polymorphism_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 30: Polymorphism
 More of this Feature
• Introduction
• Virtual Functions
• Function Example
• Array Example
 
 Related Resources
• Inheritance
• Members and Methods
• Introduction to Classes
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• C++ Tutorial, All Lessons
 

by John Kopp

Introduction
Welcome to About.com's free tutorial on C++ programming. This lesson covers polymorphism and virtual methods. Polymorphism in C++ is exhibited by the ability of a pointer or reference to a base class type to behave in different ways when it is used to manipulate objects of different subtypes of that base class. Said another way, in our code a base class pointer or reference is used to access an object of some subclass, possibly unknown at compile time and known only during execution time when the program is running. This base class pointer can access the correct subclass method. That is, it can access the method of the object of the subclass it is pointing to or referencing, rather than the corresponding method within the base class.

Confused. Sorry, polymorphism is difficult to explain, but easy to demonstrate. In the last lesson, we saw how to override base class methods and how objects of subtypes called their method rather than the base class method. This worked fine in the examples only because every object was known completely at compile time. The compiler chooses the correct methods to call. This was possible only because no pointers or references of the base class were used to access the subclasses. Why would anyone use a reference or pointer of base class type to access an object of a subclass? Suppose we need to construct a function that will work with all subclasses of a base class. With polymorphism, the function could be coded to expect a pointer or reference to the base class type and behave correctly and distinctly when different subclasses are passed in. As a second example, suppose we wish to make a list or array containing references or pointers to objects of different subtypes of a single base class. If the type of the array is selected to be either reference or pointer of the base class, subclasses of multiple types can be stored. And more importantly, the pointers or references will behave correctly and distinctly according to what subclass they access. This is polymorphism. So, in C++, the first requirement for polymorphism is that the objects must be accessed via pointer or reference.

A second requirement for polymorphism to work is that the methods whose polymorphic behavior is desired must be declared as virtual. Overridden methods not declared as virtual will behave correctly only for statically bound objects; those known at compile time. For base class pointers or references bound to objects during execution, polymorphism will not be seen if a method is not declared virtual. That means that the base class method will be used rather than the subclass method even if the subclass overrides the method. This is again for a base class pointer or reference used on an object of a subclass. A pointer or reference of the type of the subclass would of course call the subclass method, but that wouldn't be of use in the examples of the previous paragraph. As we will see, it is also possible to dynamically cast a base class pointer into a subclass pointer to get the desired behavior, but this is less useful since it requires knowledge of the subclass of the object that the base class pointer refers.

Next page > Virtual Functions > Page 1, 2, 3, 4

Previous Articles




Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.