0% found this document useful (0 votes)
3 views14 pages

Week4b Op Overloading

The document explains operator overloading in C++, allowing users to define how operators behave for user-defined types like classes and structures. It outlines rules and limitations, such as which operators can be overloaded and the syntax for doing so. Additionally, it provides examples of overloading unary and binary operators through both member and non-member functions.

Uploaded by

hurraysmart141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

Week4b Op Overloading

The document explains operator overloading in C++, allowing users to define how operators behave for user-defined types like classes and structures. It outlines rules and limitations, such as which operators can be overloaded and the syntax for doing so. Additionally, it provides examples of overloading unary and binary operators through both member and non-member functions.

Uploaded by

hurraysmart141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

CSET334 - Programming using C++

Module : Operator Overloading


Operator Overloading
In C++, we can define how operators behave for user-defined types like class and structures.

For example, The + operator, when used with values of type int, returns their sum. However, when used
with objects of a user-defined type, it is an error.

In this case, we can define the behavior of the + operator to work with objects as well.

• The concept of defining operators to work with objects and structure variables is known as
operator overloading.
Rules and Limitations
 Existing operators can only be overloaded, but the new operators cannot be overloaded.
eg. overloading +, -, *, /, <<, >> etc are allowed but creating a new operator like %%, ** etc not
allowed.

 The overloaded operator contains atleast an operand of the user-defined data type. You cannot
overload an operator for primitive types like int or float alone.

eg. overloading “+” for adding two instances of a class is allowed but for adding int and float is not
allowed.

 We cannot use friend function to overload certain operators. However, the member function can be
used to overload those operators.
eg. some operators, such as “=“, “()”, “[]” and “->” cannot be overloaded using friend functions.
Rules and Limitations
 When unary operators are overloaded through a member function take no explicit arguments, but, if
they are overloaded by a friend function, takes one argument.

 When binary operators are overloaded through a member function takes one explicit argument, and if
they are overloaded through a friend function takes two explicit.

 Operator that cannot be overloaded are as follows:


 Scope operator (::)
 Sizeof
 member selector(.)
 member pointer selector(*)
 ternary operator(?:).
Syntax for definition of the operator being overloa
returnType operator symbol (arguments) {
... .. ...
}

Here,
• returnType – the return type of the function
• operator – a keyword
• symbol – the operator we want to overload (+, <, -, etc)
• arguments – the arguments passed to the function
Member Function Non Member Function

Syntax
Expression Syntax Definition Syntax Definition Syntax

operator_symbol returnType& operator symbol() { } returnType& operator symbol


object_name; (returnType& parameter_name) {}
Note –
• No argument is passed. Note –
Prefix • The function returns *this. • Takes an object by reference.
• The function returns reference to
modified object.

Unary
Operator object_name returnType operator symbol(int) {} returnType operator symbol(returnType&
operator_symbol; parameter_name, int) {}
Note –
• The dummy int parameter differentiates postfix Note –
Postfix from prefix. • Takes an object by reference.
• Returns a copy of the original object. • Uses a dummy int parameter to
differentiate from prefix.
• Returns a copy of the original object
(before modification).
Member Function Non Member Function

Syntax
Expression Syntax Definition Syntax Definition Syntax

object_name_1 operator_symbol ReturnType operator ReturnType operator


object_name_2; symbol(ReturnType& symbol(ReturnType&
parameter_name) {} parameter_name_1, ReturnType&
parameter_name_1) {}
Note –
Binary Operator • The left operand is the calling Note –
object (this). • Both left-hand side (lhs) and
• The right operand is passed as a right-hand side (rhs) are passed.
parameter. • The return type is usually the
• The return type is typically the same class type.
same class type.
Overloading Prefix ‘++’ through a member function
#include <iostream>
using namespace std; int main() {
Counter c(5);
class Counter { ++c; // Calls operator++()
private: c.display(); // Output: Value: 6
int value; }
public:
Counter(int v = 0) : value(v) {} // Constructor

// Prefix increment (++obj)


Counter& operator++() {
++value;
return *this; // Return updated object
}

void display() const {


cout << "Value: " << value << endl;
}
};
Overloading Postfix ‘++’ through a member functio
class Counter {
private: int main() {
int value; Counter c(5);
public: Counter old = c++; // Calls operator++(int)
Counter(int v = 0) : value(v) {}
old.display(); // Output: Value: 5 (old value)
// Postfix increment (obj++) c.display(); // Output: Value: 6 (incremented)
Counter operator++(int) { }
Counter temp = *this; // Store current state
++value; // Increment current object
return temp; // Return old value
}

void display() const {


cout << "Value: " << value << endl;
}
};
Overloading Prefix ‘++’ through a non member fun
#include <iostream> // Prefix increment
using namespace std; Counter& operator++(Counter& c) {
++c.value; // Modify object
class Counter { return c; // Return updated object
private: }
int value;
public: int main() {
Counter(int v = 0) : value(v) {} Counter c(5);
++c; // Calls non-member operator++()
void display() const { c.display(); // Output: Value: 6
cout << "Value: " << value << endl; }
}

// Granting non-member operator function access to


private members
friend Counter& operator++(Counter& c);
};
Overloading Postfix ‘++’ through a non member fun
class Counter {
private: int main() {
int value; Counter c(5);
public: Counter old = c++; // Calls operator++(Counter&, int)
Counter(int v = 0) : value(v) {}
old.display(); // Output: Value: 5 (old state)
void display() const { c.display(); // Output: Value: 6 (incremented)
cout << "Value: " << value << endl; }
}

friend Counter operator++(Counter& c, int); // Postfix version


};

// Postfix increment
Counter operator++(Counter& c, int) {
Counter temp = c; // Store old value
++c.value; // Modify original object
return temp; // Return old state
}
Overloading binary ‘+’ through a member function
#include <iostream> int main() {
using namespace std; Complex c1(3, 4), c2(1, 2);
Complex sum = c1 + c2; // Calls operator+
class Complex {
private: sum.display(); // Output: 4 + 6i
double real, imag; }
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}

// Member function to overload +


Complex operator+(const Complex& obj) {
return Complex(real + obj.real, imag + obj.imag);
}

void display() const {


cout << real << " + " << imag << "i" << endl;
}
};
Overloading binary ‘+’ through a non member func
#include <iostream>
using namespace std; // Overloading + as a non-member function
Complex operator+(const Complex& lhs, const
class Complex { Complex& rhs) {
private: return Complex(lhs.real + rhs.real, lhs.imag +
double real, imag; rhs.imag);
public: }
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
int main() {
// Friend function for binary operator overloading Complex c1(3, 4), c2(1, 2);
friend Complex operator+(const Complex& lhs, const Complex sum = c1 + c2; // Calls operator+
Complex& rhs);
sum.display(); // Output: 4 + 6i
void display() const { }
cout << real << " + " << imag << "i" << endl;
}
};
Thank You

You might also like