L4 Overloading
L4 Overloading
Example }
cout << endl << "sum = " << (a + b);
// Driver code
int main()
{
add(10, 2);
add(5.3, 6.2);
return 0;
}
#include <iostream>
using namespace std;
void add(int a, int b)
{
cout << "sum = " << (a + b);
Parameters }
void add(int a, int b, int c)
should {
have a }
cout << endl << "sum = " << (a + b + c);
#include <iostream>
using namespace std;
void print(int i)
{
cout << "Integer: " << i << endl;
}
void print(double d)
Function {
cout << "Double: " << d << endl;
}
Overloading void print(string s)
{
cout << "String: " << s << endl;
}
int main()
{
print(5);
print(3.14);
print("Hello");
return 0;
}
C++ allows operators to be overloaded to
work with user-defined types (classes).
The keyword operator is used to define
operator functions.
Operator
Overloading Overloading Unary Operator
Unary operators (like ++, --, -, !) can be
overloaded as member or friend functions.
int a;
float b,sum;
sum = a + b;
Example {
return Complex(real + c.real, imag + c.imag);
}
};
int main() {
Complex c1(3, 4), c2(1, 2);
Complex c3 = c1 + c2;
c3.show();
return 0;
}
Thank you