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

Week 6 Operator Over

Oop

Uploaded by

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

Week 6 Operator Over

Oop

Uploaded by

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

Object Oriented Programming

Week 6

Lecture 1 & 2

Operators Overloading in C++


Operator overloading is a compile-time polymorphism in which the operator is overloaded to
provide the special meaning to the user-defined data type. Operator overloading is used
to overload or redefines most of the operators available in C++. It is used to perform the
operation on the user-defined data type.

You can redefine or overload most of the built-in operators available in C++. Thus, a
programmer can use operators with user-defined types as well.

Overloaded operators are functions with special names: the keyword "operator" followed by the
symbol for the operator being defined. Like any other function, an overloaded operator has a
return type and a parameter list.

Which operators can we overload and which we cannot?


Following are the operators that cannot be overloaded

 Pointer to member access operator ( .* )

 Scope resolution operator ( :: )

 Member access operator ( . )

 Condition operator ( ?: )

 size operator ( sizeof )

Page 1 of 9
Object Oriented Programming

Following is the list of overloadable operators

Category Operators

Airthmetic +,–,*,/,%

Bit-wise & , | , ~ , ^ , << , >>

Bit-wise assignment &= , |= , ^= , <<== , >>=

Relational < , > , == , != , <= , >=

Logical || , && , !

Assignment =

Arithmetic assignment -=, += , *= , /= , %=

Unary ++ , —

Subscripting []

Deference *

Function call ()

Address of &

Member access through member pointer ->*

Page 2 of 9
Object Oriented Programming

Member access through object pointer ->

Dynamic Allocation and release new, delete, new[ ], delete[ ]

Comma ,

Types of Operator Overloading


 Unary Operator Overloading

 Binary Operator Overloading

Unary Operator Overloading:


Let us consider to overload (-) unary operator. In unary operator function, no arguments should
be passed. It works only with one class objects. It is a overloading of an operator operating on a
single operand.

As the name suggests, Unary operators operate on single operand or data.

Following are the examples of Unary operators:

 Unary minus ( – ) operator

 Logical not ( ! ) operator

 Decrement ( — ) and Increment ( ++ ) operator

Example:
Assume that class Distance takes two member object i.e. feet and inches, create a function by

Page 3 of 9
Object Oriented Programming

which Distance object should decrement the value of feet and inches by 1 (having single
operand of Distance Type).

/ C++ program to show unary operator overloading

#include <iostream>

using namespace std;

class Distance {

public:

// Member Object

int feet, inch;

// Constructor to initialize the object's value

Distance(int f, int i)

this->feet = f;

this->inch = i;

// Overloading(-) operator to perform decrement

// operation of Distance object

void operator-()

feet--;

inch--;

Page 4 of 9
Object Oriented Programming

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

};

// Driver Code

int main()

// Declare and Initialize the constructor

Distance d1(8, 9);

// Use (-) unary operator by single operand

-d1;

return 0;

Output:

Feet & Inches(Decrement): 7'8

In the above program, it shows that no argument is passed and no return_type value is returned,
because unary operator works on a single operand. (-) operator change the functionality to its
member function.

Note: d2 = -d1 will not work, because operator-() does not return any value.

Page 5 of 9
Object Oriented Programming

Binary Operator Overloading:


In binary operator overloading function, there should be one argument to be passed. It is
overloading of an operator operating on two operands.

Let’s take the same example of class Distance, but this time, add two distance objects.

/ C++ program to show binary operator overloading

#include <iostream>

using namespace std;

class Distance {

public:

// Member Object

int feet, inch;

// No Parameter Constructor

Distance()

this->feet = 0;

this->inch = 0;

// Constructor to initialize the object's value

// Parametrized Constructor

Distance(int f, int i)

this->feet = f;

Page 6 of 9
Object Oriented Programming

this->inch = i;

// Overloading (+) operator to perform addition of

// two distance object

Distance operator+(Distance& d2) // Call by reference

// Create an object to return

Distance d3;

// Perform addition of feet and inches

d3.feet = this->feet + d2.feet;

d3.inch = this->inch + d2.inch;

// Return the resulting object

return d3;

};

// Driver Code

int main()

// Declaring and Initializing first object

Distance d1(8, 9);

Page 7 of 9
Object Oriented Programming

// Declaring and Initializing second object

Distance d2(10, 2);

// Declaring third object

Distance d3;

// Use overloaded operator

d3 = d1 + d2;

// Display the result

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

return 0;

Output:

Total Feet & Inches: 18'11

Here in the above program,


See Line no. 26, Distance operator+(Distance &d2), here return type of function is distance and
it uses call by references to pass an argument.
See Line no. 49, d3 = d1 + d2; here, d1 calls the operator function of its class object and takes
d2 as a parameter, by which operator function return object and the result will reflect in the d3
object.

Page 8 of 9
Object Oriented Programming

Page 9 of 9

You might also like