Thursday, 23 April 2026

OVERLOADING OF UNARY OPERATORS in C++


OVERLOADING OF UNARY OPERATORS in C++
Unary operators overloaded by member functions take no formal arguments, whereas when they are overloaded by friend functions they take a single argument.

    Overloading of incrementer and decrementer It is well known that C++ supports very unusual notation or operator that is used for incrementing and decrementing by 1. These operators can be used as either prefix or postfix. In general, overloading of these operators cannot be distinguished between prefix or postfix operation. However, whenever a postfix operation is overloaded, it takes a single argument along a member function of a class object.

The following program segment illustrates the overloading of an incrementor operator with prefix operation.

//overloading pre x ++ incrementer operator
#include <iostream>
using namespace std;
class bonacci {
public:
void operator++();
-------
-------
};
void bonacci :: operator++()
{
-------
-------
}
int main()
{
bonacci obj;
-------
-------
++obj;
return 0;
}

Example 1.
A program to generate a Fibonacci series by overloading a prefix operator.

//overloading pre x ++ incrementer operator
#include <iostream>
#include <iomanip>
using namespace std;
struct bonacci {
public:
unsigned long int f0,f1, b;
bonacci(); // constructor
void operator++();
void display();
};
bonacci :: bonacci ()
{
f0 = 0;
f1 = 1;
b = f0+f1;
}
void bonacci :: display()
{
cout << setw(4) << b;
}
void bonacci :: operator++()
{
f0 = f1;
f1 = b;
b = f0+f1;
}
int main()
{
bonacci obj;
int n;
cout << “ How many bonacci numbers are to be displayed ? \n”;
cin >>n;
cout << obj.f0 << setw(4) << obj.f1;
for (int i = 2; i <= n-1; ++i) {
obj.display();
++obj;
}
cout << endl;
return 0;
}
Output of the above program
How many bonacci numbers are to be displayed?
8
0 1 1 2 3 5 8 13

Example 2.
A program to generate a Fibonacci series by overloading a postfix operator.

//generation of bonacci numbers
//using operator overloading of post x incrementer
#include <iostream>
#include <iomanip>
using namespace std;
class bonacci {
public :
unsigned long int f0,f1, b;
bonacci(); // constructor
bonacci operator++(int);
void display();
};
bonacci :: bonacci ()
{
f0 = 0;
f1 = 1;
b = f0+f1;
}
void bonacci :: display()
{
cout << setw(4) << b;
}
bonacci bonacci :: operator++ (int x)
{
f0 = f1;
f1 = b;
b = f0+f1;
return *this;
}
int main()
{
bonacci obj;
int n;
cout << “ How many bonacci numbers are to be displayed ? \n”;
cin >>n;
cout << obj.f0 << setw(4) << obj.f1;
for (int i = 2; i<= n-1; ++i) {
obj.display();
obj++;
}
cout << endl;
return 0;
}
Output of the above program
How many bonacci numbers are to be displayed?
9
0 1 1 2 3 5 8 13 21

Operator                    Meaning
[]                                    Array element reference
()                                    Function call
new                                New operator
delete                            Delete operator
*                                     Multiplication
/                                     Division
%                                   Modulus
+                                    Addition
-                                     Subtraction
<<                                 Left shift
>>                                 Right shift
<                                    Less than
<=                                 Less than or equal to
>                                    Greater than
>=                                  Greater than or equal to
==                                  Equality
!=                                   Inequality
&                                    Bitwise AND
^                                    Bitwise XOR
|                                     Bitwise OR
&&                                 Logical AND
||                                    Logical OR
=                                     Assignment
*=                                   Multiply and assign
/=                                   Divide and assign
%=                                 Modulus and assign
+=                                  Add and assign
-=                                   Subtract and assign
<<=                               Shift left and assign
>>=                               Shift right and assign
&=                                 Bitwise AND and assign
|=                                  Bitwise OR and assign
^=                                 Bitwise one’s complement and assign
,                                     Comma
->                                  Indirect member operator
!                                     Logical negation
*                                    Pointer reference (indirection)
&                                   Address
~                                   Ones complement
->*                               Indirect pointer to member
+                                  Addition
-                                   Subtraction
++                               Incrementer
--                                 Decrementer
-                                  Unary minus

0 comments

Post a Comment