Solved

21. Explain the principle of polymorphism.

Posted on 2015-01-24
2
63 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

839 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