Home arrow C++ arrow Page 4 - Polymorphism in C++
C++

Polymorphism in C++


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.

Author Info:
By: Gabor Bernat
Rating: 4 stars4 stars4 stars4 stars4 stars / 18
June 16, 2009
TABLE OF CONTENTS:
  1. · Polymorphism in C++
  2. · Pointers
  3. · Downcasting
  4. · The Virtual Keyword

print this article
SEARCH DEVARTICLES

Polymorphism in C++ - The Virtual Keyword
(Page 4 of 4 )

The correct solution is to apply the virtual keyword. The direct effect of this on the pointed function is that, on functions from the base classes, rather than being redefined, it will be overwritten.

We will declare as virtual our function inside the base class.

 

virtual bool appendToStream(stringstream& inTo) const;

 

Once you declare a function virtual, it will remain virtual in all of the derived classes and the classes derived from the derived class; once virtual, always virtual, to sum it up. The real strength in this solution is that, for all functions declared like this in the base class, when we have a base pointer pointing to a derived object, the called function will be declared.

By just adding the virtual keyword in front of the appendToStream function declaration, the same code entered inside the main function at the end of the Pointers segment of this article will now call the derived objects class function, and produce a very different result:

 

2

CarContainer

Car Type: Lamborghini Murcielago

Car Value : 313000

 

 

Press any key to continue . . .

 

Now we have the general usage. Imagine the same when we have a stream of incoming pointers to derived classes through base class pointers; for instance, a vector of base class pointers. If we decide to add a new class, if we overwrite for the virtual function, we do not need to modify a single character in the vector handling. All of this will be done by the compiler in the background. Here is a fast example:

 

BananaContainer banana(1, 1, 50);

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

EmptyContainer empty(3);

 

stringstream inTo;

 

std::vector<BaseContainer*> allContainer;

allContainer.push_back(&banana);

allContainer.push_back(&car);

allContainer.push_back(&empty);

 

std::vector<BaseContainer*>::iterator it, end;

for (it = allContainer.begin(), end = allContainer.end();

it != end; ++it)

{

(*it)->appendToStream(inTo);

}

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

 

We can also decide to not overwrite the virtual function, as by default in this scenario the base class's function will be used. This is the case of the EmptyContainer, for which I decided to not write this function, and as follows, only the ID of the container is printed on the screen of your computer. In addition, the following appears on the console:

1

BananaContainer

Price per box: 1

Number of boxes 50

 

2

CarContainer

Car Type: Lamborghini Murcielago

Car Value : 313000

 

3

 

Press any key to continue . . .

 

Here, in a downloadable form, is the code for all of this written in Visual Studio 2008. It will work under any C++ compiler as long as you build a new empty console project and add these files to it. Be aware that it already contains some stuff that will be covered in the next article, so if you happen to bump into something that is not covered here, wait a little while. Nevertheless, feel free to try the things I have shown you in this article.

 

 

Take the time to understand the content I offered to you today. I've just scratched the surface of polymorphism and virtual functions. Feel free to post your comment here on the blog.

Live with Passion!


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- 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++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials