Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

21. Explain the principle of polymorphism.

Posted on 2015-01-24
2
Medium Priority
?
72 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 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;
}

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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

885 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