Channels ▼
RSS

Security

Simulating Polymorphic Operators in C++

Source Code Accompanies This Article. Download It Now.


Michael is an Assistant Professor of Computer Science at Augusta State University. He can be contacted at mdowell@aug.edu.


In C++, polymorphism lets a variable refer to objects of different data types. The only catch is that the different data types must be members of the same inheritance hierarchy and they must be lower in the hierarchy than the variable's data type. The cool part is this ability lets the same instruction behave differently, based on the actual object's data type instead of the variable's data type.

Consider the hierarchy of the UML class diagram in Figure 1. Here, I have derived the Manager and Salesperson classes from the Employee class. This lets me use an Employee pointer to point to an Employee, Manager, or Salesperson object.

[Click image to view at full size]

Figure 1: A class hierarchy.

The following code is an attempt at polymorphism:

Employee* pEmp = new Manager;
cout << *pEmp << endl;

However, this attempt fails since the code does not invoke the Manager's insertion operator (<<) as I intended—it invokes the Employee's insertion operator. This happens because the indirection operation (*) is performed first, so *pEmp returns the data type of the pointer. In this case, because pEmp is an Employee pointer, the *pEmp operation returns the Employee data type in spite of pointing to a Manager object. After *pEmp is done, the function call is matched using the Employee data type. Consequently, the Employee's insertion function is matched and not the Manager's function as intended.

In C++, polymorphism normally requires virtual methods. Because only methods can be inherited, many programmers think the insertion (<<) and extraction (>>) operators cannot display polymorphic behavior because these operators are implemented as nonmember friend functions. However, there are several ways of making these operators polymorphic. In this article, I compare and contrast three different techniques.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video