Copyright

Polymorphism in C++ Programming: Definition & Example

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Practical Application for C++ Programming: Inheritance, Polymorphism & Encapsulation

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Polymorphism Definition
  • 0:24 Polymorphism Importance
  • 0:55 Implementing…
  • 1:59 Other Examples
  • 3:08 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

In this lesson, you will learn what polymorphism is and why it is of importance in object-oriented programming (OOP). You will also learn to apply a core concept of OOP in C++ and how to implement polymorphism through the use of pointers and derived classes.

Polymorphism Definition

A key component of object-oriented programming is polymorphism, or the ability to re-use and extend code. It means you can have the same code act differently depending on the context. In terms of programming, this means you can have the same function in different contexts.

The figure appearing below shows an example of how this might look.


Figure 1: Polymorphism Example
Polymorphism example


Polymorphism Importance

Polymorphism saves the programmer a lot of time in re-creating code. You don't want to have to write completely different modules for every possible permutation. For example, if you had methods for tree growth, it would be hard to have to write a specific growth method for maple, spruce, pine, etc. Instead, you can have a growth function that spans across all tree types, and tweak it as you see fit for each possible tree. We can all understand the concept of growth, and understand there are variations in growth, depending on the object (tree) we are working with.

Implementing Polymorphism in C++

Before digging into polymorphism in C++, you should have a good sense of pointers and how inheritance works in C++. Additionally, in other lessons we learned about derived and base classes. If you're still unclear, please review lessons which speak on that subject.

Polymorphism allows you to create a pointer to a derived class which is also compatible with the base class. Still confused? Let's look at an example of how this would look.

Appearing below is code for a Container base (parent) class. The derived (child) classes are Cylinder and Sphere. Create the following in your compiler:

#include <iostream>
#include <cmath>
using namespace std;
const float PI = 3.1415927;
class Container {
 protected:
  float height;
  float width;
  float radius;
 public:
  void set_volume (float h, float w, float r) {
   height = h;
   width = w;
   radius = r;
  }
};
class Sphere: public Container {
 public:
  float volume() {
   float v = ((4/3) * PI * pow(radius, 3));
   return v;
  }
 
};
class Cylinder: public Container {
 public:
  float volume() {
   float v = PI * pow(radius, 2) * height;
   return v;
  }
};
int main() {
}

Next, in the main function we will create an instance of a Sphere (sphere) and an instance of a Cylinder (cylinder). We'll also create pointers from the parent/base class of Container that point to each of these instances.

  Sphere sphere;
  Cylinder cylinder;
  Container *ptrContainer1 = &sphere;
  Container *ptrContainer2 = &cylinder;
  ptrContainer1 -> set_volume(33.53, 25.11, 0);
  ptrContainer2 -> set_volume(13, 15, 0);
  cout << sphere.volume() << endl;
  cout << cylinder.volume() << endl;

Notice how each pointer calls the set_volume function. The key here is that, because of polymorphism, we can have any number of set_volume functions, but used a little differently. Recall that polymorphism really means multiple forms.

Other Examples

Let's take a look at some more examples of polymorphism in C++

1. Overloading

Recall how you may have overloaded functions in previous lessons. This is a prime example of polymorphism.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support