0% found this document useful (0 votes)
50 views8 pages

Experiment No 6 - Operators Overloading in C++

The document discusses operator overloading in C++. It explains that C++ allows users to redefine or overload operators like increment (++) and decrement (--) operators. These operators can be overloaded as member or non-member functions. The prefix and postfix forms of increment and decrement operators are distinguished by adding an int parameter, with a default value of 0. Overloading these operators allows them to operate on user-defined types like classes, extending their functionality beyond built-in types. Examples are provided to demonstrate overloading the prefix and postfix forms of increment and decrement operators.

Uploaded by

vinayak kumar
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)
50 views8 pages

Experiment No 6 - Operators Overloading in C++

The document discusses operator overloading in C++. It explains that C++ allows users to redefine or overload operators like increment (++) and decrement (--) operators. These operators can be overloaded as member or non-member functions. The prefix and postfix forms of increment and decrement operators are distinguished by adding an int parameter, with a default value of 0. Overloading these operators allows them to operate on user-defined types like classes, extending their functionality beyond built-in types. Examples are provided to demonstrate overloading the prefix and postfix forms of increment and decrement operators.

Uploaded by

vinayak kumar
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/ 8

EXPERIMENT NO 6

OPERATORS
OVERLOADING IN C++
by: Dr. Ram Paul Hathwal
Dept of CSE, ASET, AUUP
The Increment and Decrement Department of Computer
Operators in C++ Science and Engineering

C++ operator overloading is one of the most powerful features of C++ that allows
a user to change the way the operator works.
The increment (++) and decrement (--) operators are two important unary operators
available in C++.
You can redefine or overload the function of most built-in operators in C++.
These operators can be overloaded globally or on a class-by-class basis.
Overloaded operators are implemented as functions and can be member functions
or global functions.
You overload the prefix increment operator ++ with either a nonmember function
operator that has one argument of class type or a reference to class type, or with a
member function operator that has no arguments 2
Why do we need Operator Department of Computer
overloading in C++? Science and Engineering

Operators in C++ like +, -, *, / can operate in datatypes like int, float, double etc
as predefined operational meanings.
But these operators can’t operate in user-defined datatypes like objects without
extension or adding some sort of code to alter their operational meaning.
Such a way of extending the operational functionality of certain operators in C++
is called operator overloading.
To extend the meaning of an operator, an operator function is defined with a
keyword operator followed by the operator symbol.

Note : If increment/decrement operators are used before variable, they are called
prefix operators i.e ++x. And if increment/decrement operators are used
after variable, they are called postfix operators i.e x++.

3
Prefix Increment Operator Department of Computer
Science and Engineering

class X { The postfix increment operator ++ can be


public: overloaded for a class type by declaring a
// member prefix ++x
void operator++() { } };
nonmember function operator operator++
class Y { }; () with two arguments, the first having class
// non-member prefix ++y type and the second having type int.
void operator++(Y&) { } Alternatively, you can declare a member
function operator operator++() with one
int main() {
X x; argument having type int.
Y y; The compiler uses the int argument to
// calls x.operator++() distinguish between the prefix and postfix
++x; increment operators. For implicit calls, the
// explicit call, like ++x
x.operator++();
default value is zero.
// calls operator++(y)
++y;
// explicit call, like ++y
operator++(y); } 4
Postfix Increment Operator Department of Computer
Science and Engineering

class X { The compiler uses the int argument


public: to distinguish between the prefix
// member postfix x++
and postfix increment operators.
void operator++(int) { }; };
For implicit calls, the default value
class Y { };
// nonmember postfix y++ is zero.
void operator++(Y&, int) { };  The prefix and postfix
int main() { decrement operators follow the
X x; same rules as their increment
Y y; counterparts.
// calls x.operator++(0)
//default argument of zero is supplied by compiler
x++;
// explicit call to member postfix x++
x.operator++(0);
// calls operator++(y, 0)
y++;
// explicit call to non-member postfix y++
5
operator++(y, 0); }
Another Example Department of Computer
Science and Engineering

// unary_operator_overloading.cpp //after increment/decrement


#include <iostream> cout<<"\nAfter increment/decrement\n";
using namespace std; cout<<"x ="<<x.count_plus<<"\n";
class CheckCount { cout<<"y ="<<y.count_minus<<"\n";
public: return 0; }
int count_plus, count_minus;
CheckCount() {
count_plus = 0;
count_minus = 2; };
void operator ++() { ++count_plus; } // count increment
void operator --() { --count_minus; } // count increment };
int main() {
check_count x, y; //creating objects
//before increment/decrement
cout << "x =" << x.count_plus<<"\n";
cout <<"y =" << y.count_minus<<"\n";
++x;
--y;
Prefix ++ Increment Operator Department of Computer
Overloading with no return type Science and Engineering

#include <iostream>
using namespace std;
class Check {
private:
int i;
public:
Check(): i(0) { }
void operator ++() { ++i; }
void Display() { cout << "i=" << i << endl; } };
int main() {
Check obj;
// Displays the value of data member i for object obj
obj.Display();
// Invokes operator function
void operator ++( ) ++obj;
// Displays the value of data member i for object obj
obj.Display();
return 0;
}
Department of Computer
Science and Engineering

Thanks!…
Any question?

You might also like