Friday, 20 March 2015

Operator overloading using Unary operators

Operator overloading using Unary operators with examples:

Reading about the operators will not give us a clear picture about overloading of the
Unary operators
Here I am providing the example program which covers catalog of all the unary functions.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class TestOperator
{
long i;
TestOperator* This () { return this; }
public:
TestOperator (long ll = 0) : i(ll) {}
friend const TestOperator&
operator+(const TestOperator& ocObjTest);
friend const TestOperator operator-(const TestOperator& ocObjTest);
friend const TestOperator operator~(const TestOperator& ocObjTest);
friend TestOperator* operator&(TestOperator& ocObjTest);
friend int operator!(const TestOperator& ocObjTest);

// Prefix:
friend const TestOperator& operator++(TestOperator& ocObjTest);

// Postfix:
friend const TestOperator operator++(TestOperator& ocObjTest, int);

// Prefix:
friend const TestOperator& operator--(TestOperator& ocObjTest);

// Postfix:
friend const TestOperator operator--(TestOperator& ocObjTest, int);
};

Implemetation ofthe TestOperator class
const TestOperator& operator+ (const TestOperator& ocObjTest)
{
  cout << "+TestOperator\n";
  return ocObjTest; // Unary + has no effect
}
const TestOperator operator-(const TestOperator& ocObjTest)
{
  cout << "-TestOperator\n";
  return TestOperator(-ocObjTest.i);
}
const TestOperator operator~(const TestOperator& ocObjTest)
{
  cout << "~TestOperator\n";
  return TestOperator(~ocObjTest.i);
}
TestOperator* operator&(TestOperator& ocObjTest)
{
  cout << "&TestOperator\n";
  return ocObjTest.This ();
}
int operator!(const TestOperator& ocObjTest)
{
  cout << "!TestOperator\n";
  return !ocObjTest.i;
}
const TestOperator& operator++(TestOperator& ocObjTest)
{
  cout << "++TestOperator\n";
  ocObjTest.i++;
  return ocObjTest;
}
const TestOperator operator++ (TestOperator& ocObjTest, int)
{
  cout << "TestOperator++\n";
  TestOperator before(ocObjTest.i);
  ocObjTest.i++;
  return before;
}
const TestOperator& operator--(TestOperator& ocObjTest)
{
  cout << "--TestOperator\n";
  ocObjTest.i--;
  return ocObjTest;
}
const TestOperator operator--(TestOperator& ocObjTest, int)
{
  cout << "TestOperator--\n";
  TestOperator before(ocObjTest.i);
  ocObjTest.i--;
  return before;
}

Global function that is used to test the TestOperator function
void TestFun (TestOperator ocObjTest)
{
  +ocObjTest;
  -ocObjTest;
  ~ocObjTest;
  TestOperator* ip = &ocObjTest;
  !ocObjTest;
  ++ocObjTest;
  ocObjTest++;
  --ocObjTest;
  ocObjTest--;
}

The below class is used to test the unary operators functionality
class TestUnaryOperator
{
unsigned char bRes;
public:
TestUnaryOperator(unsigned char bVal = 0) : bRes (bVal) {}
const TestUnaryOperator& operator+() const
{
  cout << "+TestUnaryOperator\n";
  return *this;
}
const TestUnaryOperator operator- () const
{
  cout << "-TestUnaryOperator\n";
  return TestUnaryOperator (-bRes);
}
const TestUnaryOperator operator~ () const
{
  cout << "~TestUnaryOperator\n";
  return TestUnaryOperator (~bRes);
}
TestUnaryOperator operator!() const
{
  cout << "!TestUnaryOperator\n";
  return TestUnaryOperator (!bRes);
}
TestUnaryOperator* operator& ()
{
  cout << "&TestUnaryOperator\n";
  return this;
}

 // Prefix incremet
const TestUnaryOperator& operator++()
{
  cout << "++TestUnaryOperator\n";
  bRes++;
  return *this;
}

// Postfix increment
const TestUnaryOperator operator++(int)
{
  cout << "TestUnaryOperator++\n";
  TestUnaryOperator before(bRes);
  bRes++;
  return before;
}

// Prefix decrement
const TestUnaryOperator& operator--()
{
  cout << "--TestUnaryOperator\n";
  --bRes;
  return *this;
}

// Postfix decrement
const TestUnaryOperator operator--(int)
{
  cout << "TestUnaryOperator--\n";
  TestUnaryOperator before(bRes);
  --bRes;
  return before;
}
};

Global function that is used to test the TestUnaryOperator class
void FunTestUnary (TestUnaryOperator bRes)
{
  +bRes;
  -bRes;
  ~bRes;
 
  TestUnaryOperator* bp = &bRes;
  !bRes;
  ++bRes;
  bRes++;
  --bRes;
  bRes--;
}

The below main function is used to call the global function that we wrote above.
int main ()
{
TestOperator ocObjTest;
TestFun(ocObjTest);
TestUnaryOperator bRes;
FunTestUnary (bRes);
getchar ();
return 0;
} 

The output of the above program is
+TestOperator
-TestOperator
~TestOperator
&TestOperator
!TestOperator
++TestOperator
TestOperator++
--TestOperator
TestOperator--
+TestUnaryOperator
-TestUnaryOperator
~TestUnaryOperator
&TestUnaryOperator
!TestUnaryOperator
++TestUnaryOperator
TestUnaryOperator++
--TestUnaryOperator
TestUnaryOperator--


In the above example for more understanding I have grouped all the functions based on the arguments that are passed.

No comments: