Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# 21. Explain the principle of polymorphism.

Posted on 2015-01-24
Medium Priority
71 Views
Explain the principle of polymorphism.
Provide examples.
0
Question by:Nusrat Nuriyev
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 86

Assisted Solution

jkr earned 1000 total points
ID: 40568240
Polymorphism is the concept of accessing objects through pointers to common base classes. This allows to define a common interface for a group of classes that have common traits, e.g.

``````class Vehicle {
public:
virtual void Drive() {speed = 0;}
protected:
int speed;
};

class Bicycle: public Vehicle {
public:
virtual void Drive() {speed = 10;}
};

class Car: public Vehicle {
public:
virtual void Drive() {speed = 50;}
};

void Race(Vehicle** vehicles, size_t count) {

for (size_t i = 0; i < count; ++i) {

vehicles[i]->Drive();
}
}
int main () {

Car car;
Bicycle bike;

Vehicle* arr[] = { &car, &bike};

Race(arr, 2);

return 0;
}
``````

You will find another nice example at http://www.cplusplus.com/doc/tutorial/polymorphism/
0

LVL 32

Accepted Solution

phoffric earned 1000 total points
ID: 40568724
Just wanted you to be aware of static vs. dynamic polymorphism.
http://www.cpptutor.com/static-polymorphism-and-dynamic-polymorphism-in-c++.htm
http://en.wikipedia.org/wiki/Template_metaprogramming#Static_polymorphism

You have two identical function names, but they go to different functions - polymorphism. If the compiler can call the function directly - that's static polymorphism. If it cannot, and the correct function is only known at runtime - that's dynamic polymorphism.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
###### Suggested Courses
Course of the Month5 days, 11 hours left to enroll