function overloading and overriding
function overloading and overriding
and Overriding
Introduction to Functions
int main() {
cout << add(3, 4); // Calls int version: Output 7
cout << add(3.5, 4.5); // Calls double version: Output 8.0
return 0;
}
Example
int main() {
cout << add(3, 4); // Calls two-parameter version: Output 7
cout << add(3, 4, 5); // Calls three-parameter version: Output 12
return 0;
}
Advantages of Function
Overloading
Simplifies Code: Only one function name needed, making code
more readable.
Flexibility: Easily use similar operations for different data types or
parameter counts.
Practicality: Commonly used in libraries to handle various data
types with the same function name.
What is Function Overriding?
Rectangle and Circle classes both override area() with specific implementations.
Rectangle calculates area as length * width.
Circle calculates area as π * radius^2.
Main Function: