Polymorphism
Polymorphism means the ability to take more than one form.An operation may exhibit different behaviours in different instances.The behaviour depends upon the types of data used in the operation.
- C++ achieves polymorphism through the use of function overloading.
- Two or more functions can share the same name as long as their parameter declarations are different.
- In this situation, the functions that share the same name are said to be overloaded, and the process is referred to as function overloading.
- Function overloading, allows the user to create multiple definitions for functions.
- The programmer can have several functions with the same name, but with different argument lists or signatures.
- The function definition also changes with the change of signature.
Example
class Colour {
public:
virtual ~Colour();
};
class Red : public Colour {
public:
~Red(); // Virtuality inherited from Colour
};
class LightRed : public Red {
public:
~LightRed();
};
void main(){
Colour *palette[3];
palette[0] = new Red; // Dynamically create a new Red object
palette[1] = new LightRed;
palette[2] = new Colour;
} |