0% found this document useful (0 votes)
0 views11 pages

L4 Overloading

The document explains the concept of overloading in programming, specifically in C++, where multiple functions can share the same name but differ in parameters. It provides examples of function overloading, operator overloading for both unary and binary operators, and demonstrates how the compiler determines which function to call based on the arguments. Additionally, it includes code snippets to illustrate these concepts in practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views11 pages

L4 Overloading

The document explains the concept of overloading in programming, specifically in C++, where multiple functions can share the same name but differ in parameters. It provides examples of function overloading, operator overloading for both unary and binary operators, and demonstrates how the compiler determines which function to call based on the arguments. Additionally, it includes code snippets to illustrate these concepts in practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Overloading

Lecturer: Dr. Rodnie K. Mafa (PhD)


Office Block 1: Office no. 17
Office line: 493 1102
Overloading
Definition: is where two or more functions can have the same name
but different parameters
#include <iostream>
using namespace std;
void add(int a, int b)
{
cout << "sum = " << (a + b);
}
void add(double a, double b)
{

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);

different // Driver code


int main()
number {
add(10, 2);
add(5, 6, 4);
return 0;
}
-This allows multiple functions with the same name but different parameter
lists.
-The compiler determines which function to call based on the arguments.

#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;

variables “a” and “b” are of types “int” and


“float”, which are built-in data types. Hence
Example the addition operator ‘+’ can easily
add the contents of “a” and “b”. This is
because the addition operator “+” is
predefined to add variables of built-in data
type only.
#include <iostream>
using namespace std;
class Number
{
int value;
public:
Number(int v) : value(v) {}
void show() { cout << "Value: " << value << endl;
Example: }
// Overloading unary minus (-)
Unary Number operator-() {
return Number(-value);
Overloading }
};
int main() {
Number num(10);
Number neg = -num;
neg.show();
return 0;
}
Overloading Binary operators (like +, -, *, /, ==, +=)
operate on two operands.
Binary Overloaded as member or friend
Operators functions.
using namespace std;
class Complex {
int real, imag;
public:
Complex(int r, int i) : real(r), imag(i) {}
void show() { cout << real << " + " << imag << "i" << endl;
}
// Overloading binary +
Complex operator+(const Complex &c)

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

You might also like