Lecture 2 Operator Overloading
Lecture 2 Operator Overloading
Operator Overloading
Operator Overloading in C++ allows programmers to redefine the behavior of operators for user-
defined data types. This feature, a form of compile-time polymorphism, enables operators to have
specific meanings for custom classes, extending their functionality beyond built-in types.
Concept and Purpose: Operator overloading provides the ability to define or redefine how operators
work with user-defined classes. For instance, the + operator can be overloaded to perform operations
like string concatenation or complex number addition, which aren't supported by default for user-
defined types.
Basic Example: Consider the following example where the + operator is used with a user-defined class
Complex:
#include <iostream>
class Complex {
private:
public:
Complex res;
return res;
void print() { cout << real << " + i" << imag << '\n'; }
};
int main() {
c3.print(); // Outputs: 12 + i9
return 0;
Key Points:
Syntax: For a class A, an operator function for the + operator would be declared as A operator+
(A const& obj).
Functionality: Overloading allows operators to work with user-defined objects similarly to how
they work with built-in types, thus providing a more intuitive syntax for custom operations.
Naming: Operator functions are named using the keyword operator followed by the operator
symbol.
Usage: Operator functions are invoked when their corresponding operators are used with class
objects, providing a seamless way to work with user-defined types.
Can Overload: Most operators including arithmetic (+, -, *, /), relational (<, >, ==), and others like
[] and ().
Cannot Overload: Some operators like :: (scope resolution), . (member access), and ?: (ternary
conditional) cannot be overloaded.
Operator overloading enhances the flexibility and readability of code, making complex operations on
user-defined types as intuitive as operations on built-in types.
Operator Overloading in C++ allows programmers to redefine the functionality of operators for user-
defined types. This feature supports two primary types of operator overloading: unary and binary. Here's
an overview of both:
Unary operators operate on a single operand. When overloading a unary operator, the operator function
does not take any arguments (except for the implicit this pointer) and does not return a value.
Syntax:
cpp
Copy code
// Function body
#include <iostream>
class Distance {
public:
void operator-() {
feet--;
inch--;
cout << "\nFeet & Inches (Decrement): " << feet << "'" << inch;
};
int main() {
return 0;
}
Output:
mathematics
In this example, operator-() decrements the feet and inch values of the Distance object. Unary
operators, such as -, modify the object but do not return a value.
Binary operators operate on two operands. When overloading a binary operator, the operator function
should take one argument (the second operand) and return a new object of the class.
Syntax:
// Function body
Here, op is the binary operator being overloaded, and obj is the second operand.
#include <iostream>
class Distance {
public:
Distance d3;
return d3;
};
int main() {
cout << "\nTotal Feet & Inches: " << d3.feet << "'" << d3.inch;
return 0;
Output:
mathematics
In this example, operator+() adds two Distance objects. It takes another Distance object as a
parameter and returns a new Distance object that represents the sum.
1. Unary Operators:
2. Binary Operators:
4. Function Types:
o Operator functions can be non-static member functions, global free functions, or friend
functions.
Operator overloading in C++ enhances the expressiveness of the language by allowing custom classes to
interact with operators in a way that aligns with their intended behavior. This results in more intuitive
and readable code.