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

Unit 4 Operator Overloading

This document discusses operator overloading in C++. It explains that operator overloading allows operators to work with user-defined data types by defining their custom behavior. It lists some commonly overloaded operators like +, -, *, / and discusses how operators can be overloaded through member functions or non-member functions. The document provides examples of unary and binary operator overloading using both member functions and friend functions. It also gives an example of overloading the insertion operator.

Uploaded by

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

Unit 4 Operator Overloading

This document discusses operator overloading in C++. It explains that operator overloading allows operators to work with user-defined data types by defining their custom behavior. It lists some commonly overloaded operators like +, -, *, / and discusses how operators can be overloaded through member functions or non-member functions. The document provides examples of unary and binary operator overloading using both member functions and friend functions. It also gives an example of overloading the insertion operator.

Uploaded by

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

Unit - 4

Operator Overloading
Operator Overloading
 Method of making operators to work for user defined
data types
 It is also a type of polymorphism in which an operator
is overloaded to give user defined meaning to it
 For example, ‘+’ operator can be used for addition for
number data types and concatenation for string
 We can overload all the C++ operators except
the following:
 Class member access operator ( . , .*)
 Scope resolution operator (::)
 Size operator (sizeof)
 Conditional operator ( ? : )
 Terniary operator (?:)
int a;

float b,sum;

sum = a + b;

Here a is int and b is float which are built in data types. Hence ‘+’ operator can be used to add
those “a” and “b”. This is because ‘+’ is predefined to add variables of built in data type.

class Student {

};

int main()

A studentOne, studentTwo, studentTheree;

studentTheree = studentOne + studentTwo;

 In this example studentOne,studentTwo and studentThree are variables of type Student.Here we


are trying to add two objects of type Student using ‘+’ operator which is not allowed
Defining operator overloading
 Choose an operator: Some commonly overloaded
operators are +, -, *, /, <<, >>, ==, !=, <, >, <=, >=, =,
and many others.
 Define the operator function: Create a member
function or a non-member function with the name
operator followed by operator symbol.
 Implement the operator: Inside the operator function,
define the desired behavior of the operator.
 Return the result
 Syntax :

 return_type operator operator_symbol(args){


// function body

 }

 Eg :

 Student operator+(Student s){


// function body

 }

 Here return type is value returned by specified operation

 Operator is a keyword

 Operator function must be either member function or friend function

 Friend function will have only one argument for unary operation and two for binary
operators
Unary Operator Overloading
 If overloading takes place on a single operand
using operators like increament(++),
decreament(--), unary minus(-), logical not(!),
etc. is called unary operator overloading
 Normally this operator appear as prefix
Example of unary
Operator
Overloading
Using member
function
Example of unary
Operator
Overloading
Using friend
function
Binary Operator Overloading
 If overloading takes place on two operands
using binary operators then it is called binary
operator overloading.
 The operator overloading mechanism is same
as in unary operator overloading
Example of Binary
Operator
Overloading
Using Member
function
Example of Binary
Operator
Overloading
Using friend
function
Example of Insertion
Operator
Overloading

Output :

You might also like