Solved

21. Explain the principle of polymorphism.

Posted on 2015-01-24
2
67 Views
Last Modified: 2015-02-11
Explain the principle of polymorphism.
Provide examples.
0
Comment
Question by:Nusrat Nuriyev
2 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 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;
}

Open in new window



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

Accepted Solution

by:
phoffric earned 250 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question