Function Overloading and Polymorphism
In C++ it is possible to give a function name 2 or more definitions
This is know as function overloading and is an example of polymorphism (from Greek meaning “many forms”)
Example: averaging numbers
- double avg(double n1, double n2);
- double avg(double n1, double n2, double n3);
Example: add rectangular pizza to the mix
- double unitprice(int diameter, double price);
- double unitprice(int length, int width, double price);
-
How does the compiler tell them apart?
- either the parameter count and/or types are different