Polymorphism is the capability to use an operator or method in different ways. Polymorphism gives the different function to the operators or methods. Polymorphism assigns to codes, operations that perform differently in different contexts. Polymorphism is a useful feature of the object oriented programming language in C++. Polymorphism is a way to call different functions by accepting only one type of function call. In a short way we can say that Polymorphism means having a multi form of code, operator, function and object who work differently in different classes.
For Example: (+) operator in C++
4 + 7 <– integer addition
3.16 + 2.0 <– floating point addition
s1 + “bar” <– string concatenation
A single operator (+) perform differently in different contexts like integer, float or string referring the concept of Polymorphism. It is the type of Polymorphism is called overloading.
Types of Polymorphism:-
-
Run Time Polymorphism:
The Run Time Polymorphism implements with inheritance and virtual functions.
-
Compile Time Polymorphism:
The Compile Time Polymorphism implements with templates.
Advantages of Polymorphism:
- Code is uncomplicated to write and read.
- Same interface could be used for making methods with different implementations.
- Complete implementations can be reintegrated by using same method signatures.
- Provides simpler maintenance of applications.
- Supports making extensible systems.
- Handling different objects.
- Single operator can be used to store multiple data.
- It cut downs coupling.
Disadvantages of Polymorphism:
- Complex in very large level programming.
- Use for limited application commonly.
- Not very simple for the beginner to just pick up and go with it.
Example of C++ polymorphism:-
Employee’s Example:-
Here, we will show 2 types of employees as classes in C++:
- A non-exclusive employee (class Employee) and
- A manager (class Manager)
For these employees, we will store data, like their:
- name id
- salary rate
And we will need some functionality, like being able to:
- Initialize the employee
- Get the employee’s fields
- Calculate the employee’s salary
To help determine Polymorphism in C++, we will target on the methods that calculate an employee’s salary.
Employee class:-
Here is a class signification for a generic Employee:
class Employee { public: Employee(string theNameID, float theSalaryRate); string getNameID() const; float getSalaryRate() const; float salary(float hoursWorked) const; protected: string nameid; float salaryRate; }; //Definations for each of the methods: Employee::Employee(string theNameID, float theSalaryRate) { nameid = theNameID; salaryRate = theSalaryRate; } string Employee::getNameID() const { return nameid; } float Employee::getSalaryRate() const { return salaryRate; } float Employee::salary(float hoursWorked) const { return hoursWorked * salaryRate; }
Note that the salaryRate is used as an hourly conduct.
Manager class:-
We will also have a Manager class, that describes reusing the Employee class (i.e., via inheritance).
Recall, if a manager obtain from an employee, then it will get all the data and performance of an employee. We can add any different data and methods required for a manager and override (i.e.,redefine) all methods that differ for a manager.
Here is the class explanation for a Manager:
#include "employee.h" class Manager : public Employee { public: Manager(string theNameID, float theSalaryRate, bool isSalaried); bool getSalaried() const; float salary(float hoursWorked) const; protected: bool salaried; }; Explanation for the added or <em>overridden </em>methods follow: Manager::Manager(string theNameID, float theSalaryRate, bool isSalaried) : Employee(theNameID, theSalaryRate) { salaried = isSalaried; } bool Manager::getSalaried() const { return salaried; } float Manager::salary(float hoursWorked) const { if (salaried) return salaryRate; /* else */ return Employee::salary(hoursWorked); }
The salary() method is providing a new explanation, in which the salaryRate has 2 available uses. If the manager is salaried, salaryRate is the settled rate for the salary period; otherwise, it produce an hourly rate, just like it does for a formal employee.
Note: Employees paid by a salary (i.e., those that are salaried) earn a fixed salary each salary period (e.g., week, 2 weeks, month) regardless of how many hours they work.
Using Employee and Manager objects:-
Employee and Manager classes can be represented as follows:
#include "employee.h" #include "manager.h" // Print out nameid and salary (based on 40 hours work). Void main() { Employee empl("John", 25.0); cout << "NameID: " << empl.getNameID() << endl; cout << "Salary: " << empl.salary(40.0) << endl; Manager mgr("Smith", 1200.0, true); cout << "NameID: " << mgr.getNameID() << endl; cout << "Salary: " << mgr.salary(40.0) << endl; cout << "Salaried: " << mgr.getSalaried() << endl; getch(); }
Remember that a Manager pick up all the methods inherited from Employee, like getNameID(), new versions for employee and manager it overrode, like salary(), plus ones it added, like getSalaried().
Explanation of Polymorphism C++ Example:
- Write the class Explanation for a class named Employee with name id and salary rate as employee objects. The class has two member functions. The assembler and a function that allows a program to refer values to the data members.
- Add two member functions name and salary rate to the Employee class. One member function should refer any program using an employee object to prospect the contents of the name id data member. The other member function should refer the program to prospect the contents of the employee salary rate data member.
- Add three member function name id, salary rate, salary to the Manager class. The member function should tally an employee objects new salary, based on a salary rate gave by the program using the object. Before tally the salary, the member function should check that the salary rate is greater than or equal to zero. If the salary rate is less than zero, the member function should array an error message.
- Now take both class one is Employee class and second is Manager class together and add member function of Employee class and member function of Manager class.
- Write a main function that will build an array of employee and manager objects, select values to the objects, array the names and current salaries for all objects, ask user for the salary rate and then calculate and array new salaries for all objects.
Reference:-
http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/