Operator Overloading in C++ Programming - Part 3

Operator Overloading of Postfix Operator
Overloading of increment operator up to this point is only true if it is used in prefix form. This is the modification of above program to make this work both for prefix form and postfix form.

/* C++ program to demonstrate the working of ++ operator overloading. */
#include <iostream>
using namespace std;
class Check
{
    private:
        int i;
    public:
        Check(): i(0) { }
        Check operator ++ ( )
        {
        Check temp;
        temp.i=++i;
        return temp;
        }

/* Notice int inside barcket which indicates postfix increment. */
        Check operator ++ (int)
        {
        Check temp;
        temp.i=i++;
        return temp;
        }
        void Display( )
        { 
        cout<<"i="<<i<<endl; 
        }
};
int main( )
{
Check obj, obj1; 
obj.Display( ); 
obj1.Display( );
obj1=++obj; /* Operator function is called then only value of obj is assigned to obj1. */
obj.Display( );
obj1.Display( );
obj1=obj++; /* Assigns value of obj to obj1++ then only operator function is called. */
obj.Display( );
obj1.Display( );
return 0;
}

Output
i=0
i=0
i=1
i=1
i=2
i=1

When increment operator is overloaded in prefix form; Check operator ++ () is called but, when increment operator is overloaded in postfix form; Check operator ++ (int) is invoked. Notice, the int inside bracket. This int gives information to the compiler that it is the postfix version of operator. Don't confuse this int doesn't indicate integer.

Operator Overloading of Decrement -- Operator
Decrement operator can be overloaded in similar way as increment operator. Also, unary operators like: !, ~ etc can be overloaded in similar manner.

C++ Program to Subtract Complex Number Using Operator Overloading
In this tutorial, subtraction - operator is overloaded to perform subtraction of a complex number from another complex number. Since - is a binary operator (operator that operates on two operands ), one of the operands should be passed as argument to the operator function and the rest process is similar to the overloading of unary operators.

Binary Operator Overloading to Subtract Complex Number
/* C++ program to demonstrate the overloading of binary operator by subtracting one complex number from another. */
#include <iostream>
using namespace std;
class Complex
{
    private:
        float real;
        float imag;
    public:
        Complex( ): real(0), imag(0){ }
        void input( )
        {
        cout<<"Enter real and imaginary parts respectively: ";
        cin>>real;
        cin>>imag;
        }
        Complex operator - (Complex c2) /* Operator Function */
        {
        Complex temp;
        temp.real=real-c2.real;
        temp.imag=imag-c2.imag;
        return temp;
        }
        void output( )
        {
        if(imag<0)
        cout<<"Output Complex number: "<<real<<imag<<"i";
        else
        cout<<"Output Complex number: "<<real<<"+"<<imag<<"i";
        }
};
int main( )
{
Complex c1, c2, result;
cout<<"Enter first complex number:\n";
c1.input( );
cout<<"Enter second complex number:\n";
c2.input( );
/* In case of operator overloading of binary operators in C++ programming, the object on right hand side of operator is always assumed as argument by compiler. */ 
result=c1-c2; /* c2 is furnised as an argument to the operator function. */
result.output( );
return 0;
}

Explanation: In this program, three objects of type Complex is created and user is asked to enter the real and imaginary parts for two complex numbers which is stored in objects c1 and c2. Then statement result=c1-c2 is executed. This statement invokes the operator function Complex operator - (Complex c2). When result=c1-c2 is executed, c2 is passed as argument to the operator function. In case of operator overloading of binary operators in C++ programming, the object on right hand side of operator is always assumed as argument by compiler. Then, this function returns the resultant complex number (object) to main() function and then, it is displayed.

Though, this tutorial contains the overloading of - operators, binary operators in C++ programming like: +, *, <, += etc. can be overloaded in similar manner.

No comments:

Post a Comment