POLYMORPHISM

Consists of two words: poly, which means “many,” and morph, which means “form.” In programming, polymorphism is the ability to have many forms for the same name.  An example of polymorphism is when different functions do a similar task and have the same function name or similarly when using many forms for the same operator. Polymorphism, encapsulation, and inheritance are three important features of object-oriented programming. Function overloading and operator overloading are two examples of polymorphism. A user function or a built-in operator can be overloaded while performing what it was originally designated to do. A computer teacher can become overloaded by teaching an additional math or other course. One form of polymorphism is function overloading. For example, the same function name:  print( ) prints the list of items in a queue or a stack, regardless if they were created statically or dynamically. Another example of polymorphism is operator overloading which adds a new meaning to most of the C++ operators. C++ operators are defined for the basic data types; with operation overloading, the same operators can be applied to user-defined types, keeping the operation in the same form as it is applied to the basic types. The goal of operator overloading is to allow user defined types (classes) to behave in the same way as built-in types.  Polymorphism contributes to the concept of abstraction by providing a meaningful name that can be used differently for several similar functions and operations. Therefore, a programmer can focus on the function’s and operator’s work rather than getting involved in the overwhelming details of the implementation. As long as the same interface is exposed, the implementation is irrelevant. Finally, for a language to be considered an object-programming language, it must support the features of polymorphism. C++ templates allow programmers to code and reuse code regardless of the specific data type. When the type of data is defined, the compiler will generate the code as if it was written for that specific data type.

 Polymorphism for salary computation:

For example with the class hourly and the class Salary
 


class hourly
Definitions for each of the methods follow:
Hourly::Hourly(string theName, float thePayRate)
{
name = theName;
payRate = thePayRate;
}

string hourly::getName() const
{
return name;
}

float hourly::getPayRate() const
{
return payRate;
}

float hourly::pay(float hoursWorked) const
{
return hoursWorked * payRate;
}
Note that the payRate is used as an hourly wage.
 
class Salary
Definitions for the additional or overridden methods follow:
Salary::Salary(string theName,
float thePayRate,
bool isSalaried)
: Employee(theName, thePayRate)
{
salaried = isSalaried;
}

bool Manager::getSalaried() const
{
return salaried;
}

float Manager::pay(float hoursWorked) const
{
if (salaried)
return payRate;
/* else */
return Employee::pay(hoursWorked);
}


Note that the payRate is used as an hourly wage in the employee class and
The pay() method here is given a new definition, in which the payRate has 2 possible uses. If the employee is salaried, payRate is the fixed rate for the pay period; otherwise, it represents an hourly rate, just like it does for a regular employee (hourly based).

Hosted by www.Geocities.ws

1