Skip to content

Commit 9e46907

Browse files
author
Sumit tripathi
committed
Compile Time Polymorphism using Function Overloading (same function name, different parameters)
1 parent e44aab6 commit 9e46907

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

polymorphism.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int main(){
4444
4545
4646
47-
*/
47+
4848
4949
5050
@@ -98,3 +98,45 @@ int main()
9898
return 0;
9999
}
100100
101+
102+
103+
*/
104+
105+
// Compile Time Polymorphism
106+
107+
// Function Overloading (same function name, different parameters)
108+
109+
#include <iostream>
110+
using namespace std;
111+
112+
class Calculator
113+
{
114+
public:
115+
// Function to add two integers
116+
int add(int a, int b)
117+
{
118+
return a + b;
119+
}
120+
121+
// Function to add three integers
122+
int add(int a, int b, int c)
123+
{
124+
return a + b + c;
125+
}
126+
127+
// Function to add two floats
128+
float add(float a, float b)
129+
{
130+
return a + b;
131+
}
132+
};
133+
134+
int main()
135+
{
136+
Calculator calc;
137+
138+
cout << "Sum of 2 and 3: " << calc.add(2, 3) << endl;
139+
cout << "Sum of 1, 2 and 3: " << calc.add(1, 2, 3) << endl;
140+
cout << "Sum of 1.5 and 2.5: " << calc.add(4.5f, 2.5f) << endl;
141+
return 0;
142+
}

polymorphism.exe

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)