0% found this document useful (0 votes)
18 views

Lecture 2 Operator Overloading

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lecture 2 Operator Overloading

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lecture 2

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>

using namespace std;

class Complex {

private:

int real, imag;

public:

Complex(int r = 0, int i = 0) : real(r), imag(i) {}

// Overloading the '+' operator

Complex operator+(Complex const& obj) {

Complex res;

res.real = real + obj.real;

res.imag = imag + obj.imag;

return res;

void print() { cout << real << " + i" << imag << '\n'; }

};
int main() {

Complex c1(10, 5), c2(2, 4);

Complex c3 = c1 + c2; // Using the overloaded '+' operator

c3.print(); // Outputs: 12 + i9

return 0;

Key Points:

 Operator Functions: Overloaded operators are implemented as member functions or friend


functions. They have the form Type operator<op>(Type const& obj) where <op> is the operator
being overloaded.

 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.

Differences from Normal Functions:

 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.

Operators You Can and Cannot Overload:

 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:

1. Overloading Unary Operators:

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

Return_Type classname::operator op() {

// Function body

Here, oop is the unary operator being overloaded.

Example: Overloading the - (negation) operator

#include <iostream>

using namespace std;

class Distance {

public:

int feet, inch;

Distance(int f, int i) : feet(f), inch(i) {}

// Overloading the unary '-' operator

void operator-() {

feet--;

inch--;

cout << "\nFeet & Inches (Decrement): " << feet << "'" << inch;

};

int main() {

Distance d1(8, 9);

-d1; // Use of overloaded unary '-' operator

return 0;
}

Output:

mathematics

Feet & Inches (Decrement): 7'8

 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.

2. Overloading Binary Operators:

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:

Return_Type classname::operator op(ClassName const& obj) {

// Function body

Here, op is the binary operator being overloaded, and obj is the second operand.

Example: Overloading the + (addition) operator

#include <iostream>

using namespace std;

class Distance {

public:

int feet, inch;

Distance() : feet(0), inch(0) {}

Distance(int f, int i) : feet(f), inch(i) {}

// Overloading the binary '+' operator

Distance operator+(Distance const& d2) {

Distance d3;

d3.feet = feet + d2.feet;


d3.inch = inch + d2.inch;

return d3;

};

int main() {

Distance d1(8, 9);

Distance d2(10, 2);

Distance d3 = d1 + d2; // Use of overloaded binary '+' operator

cout << "\nTotal Feet & Inches: " << d3.feet << "'" << d3.inch;

return 0;

Output:

mathematics

Total Feet & Inches: 18'11

 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.

Rules and Criteria for Operator Overloading:

1. Unary Operators:

o Non-static member functions for unary operators take no arguments.

o Friend functions for unary operators can take one argument.

2. Binary Operators:

o Non-static member functions for binary operators take one argument.

o Friend functions for binary operators take two arguments.

3. Operators That Cannot Be Overloaded:

o Cannot Be Overloaded: .*, ::, ?:

o Cannot Be Friend Functions: =, (), [], ->

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.

You might also like