Unit IV
Polymorphism
Polymorphism:
Introduction
,
, Compile Time Polymorphism
Function overloading(also known as method overloading)
Example:
#include <iostream>
using namespace std;
// Function to add two integers
int add(int a, int b) {
return a + b;
}
// Function to add three integers
int add(int a, int b, int c) {
return a + b + c;
}
// Function to add two floating-point numbers
float add(float a, float b) {
return a + b;
}
int main() {
int num1 = 5, num2 = 10, num3 = 15;
float float1 = 2.5, float2 = 3.5;
// Call the overloaded functions
cout << "Sum of " << num1 << " and " << num2 << " is " << add(num1, num2) << endl;
Polymorphism
Polymorphism:
Introduction
,
, Compile Time Polymorphism
Function overloading(also known as method overloading)
Example:
#include <iostream>
using namespace std;
// Function to add two integers
int add(int a, int b) {
return a + b;
}
// Function to add three integers
int add(int a, int b, int c) {
return a + b + c;
}
// Function to add two floating-point numbers
float add(float a, float b) {
return a + b;
}
int main() {
int num1 = 5, num2 = 10, num3 = 15;
float float1 = 2.5, float2 = 3.5;
// Call the overloaded functions
cout << "Sum of " << num1 << " and " << num2 << " is " << add(num1, num2) << endl;