www.easyCPlusPlus.com

Easy C++

www.easyCPlusPlus.com

C++ Tutorial - Lesson 30: Polymorphism

Introduction

by John Kopp

Support this site at no cost to you

Introduction
Welcome to EasyCPlusPlus.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.

Previous Page       Next Page