Back-End Programming Exam  >  Back-End Programming Notes  >  Learn to Program with C++: Beginner to Expert  >  Programming Examples - Operator Overloading

Programming Examples - Operator Overloading | Learn to Program with C++: Beginner to Expert - Back-End Programming PDF Download

Increment ++ and Decrement -- Operator Overloading in C++ Programming
In this example, you'll learn to overload increment ++ and decrement -- operators in C++.

Program 1: Prefix ++ Increment Operator Overloading with no return type

#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;
}
Output
i=0
i=1

Initially when the object obj is declared, the value of data member i for object obj is 0 (constructor initializes i to 0).
When ++ operator is operated on obj, operator function void operator++( ) is invoked which increases the value of data member i to 1.
This program is not complete in the sense that, you cannot used code:
obj1 = ++obj;
It is because the return type of operator function in above program is void.
Here is the little modification of above program so that you can use code obj1 = ++obj

Program 2: Prefix Increment ++ operator overloading with return type

#include <iostream>
using namespace std;
class Check
{
  private:
    int i;
  public:
    Check(): i(0) {  }
    // Return type is Check
    Check operator ++()
    {
       Check temp;
       ++i;
       temp.i = i;
       return temp;
    }
    void Display()
    { cout << "i = " << i << endl; }
};
int main()
{
    Check obj, obj1;
    obj.Display();
    obj1.Display();
    obj1 = ++obj;
    obj.Display();
    obj1.Display();
    return 0;
}

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

This program is similar to the one above.
The only difference is that, the return type of operator function is Check in this case which allows to use both codes ++obj; obj1 = ++obj;. It is because, temp returned from operator function is stored in object obj.
Since, the return type of operator function is Check, you can also assign the value of obj to another object.
Notice that, = (assignment operator) does not need to be overloaded because this operator is already overloaded in C++ library.

Program 3: Postfix Increment ++ Operator Overloading
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.

#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();
    // Operator function is called, only then value of obj is assigned to obj1
    obj1 = ++obj;
    obj.Display();
    obj1.Display();
    // Assigns value of obj to obj1, only then operator function is called.
    obj1 = obj++;
    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.

Program 4: Operator Overloading of Decrement -- Operator
Decrement operator can be overloaded in similar way as increment operator. 

#include <iostream>
using namespace std;
class Check
{
  private:
    int i;
  public:
    Check(): i(3) {  }
    Check operator -- ()
    {
        Check temp;
        temp.i = --i;
        return temp;
    }
    // Notice int inside barcket which indicates postfix decrement.
    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();
    // Operator function is called, only then value of obj is assigned to obj1
    obj1 = --obj;
    obj.Display();
    obj1.Display();
    // Assigns value of obj to obj1, only then operator function is called.
    obj1 = obj--;
    obj.Display();
    obj1.Display();
    return 0;
}

Output
i = 3
i = 3
i = 2
i = 2
i = 1
i = 2

Also, unary operators like: !, ~ etc can be overloaded in similar manner.

Example 2: C++ Program to Subtract Complex Number Using Operator Overloading
In this example, you'll learn to subtract complex numbers using operator overloading of the - operator.

Program: Binary Operator Overloading to Subtract Complex Number

#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;
       }
       // Operator overloading
       Complex operator - (Complex c2)
       {
           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;
    result.output();
    return 0;
}

In this program, three objects of type Complex are created and user is asked to enter the real and imaginary parts for two complex numbers which are 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 which is displayed on to the screen.
Though, this tutorial contains the overloading of - operators, binary operators in C++ programming like: +, *, <, += etc. can be overloaded in a similar manner.

The document Programming Examples - Operator Overloading | Learn to Program with C++: Beginner to Expert - Back-End Programming is a part of the Back-End Programming Course Learn to Program with C++: Beginner to Expert.
All you need of Back-End Programming at this link: Back-End Programming
Are you preparing for Back-End Programming Exam? Then you should check out the best video lectures, notes, free mock test series, crash course and much more provided by EduRev. You also get your detailed analysis and report cards along with 24x7 doubt solving for you to excel in Back-End Programming exam. So join EduRev now and revolutionise the way you learn!
Sign up for Free Download App for Free
73 videos|7 docs|23 tests

Up next

73 videos|7 docs|23 tests
Download as PDF

Up next

Explore Courses for Back-End Programming exam
Related Searches

Programming Examples - Operator Overloading | Learn to Program with C++: Beginner to Expert - Back-End Programming

,

Previous Year Questions with Solutions

,

Summary

,

Programming Examples - Operator Overloading | Learn to Program with C++: Beginner to Expert - Back-End Programming

,

Sample Paper

,

MCQs

,

Important questions

,

mock tests for examination

,

practice quizzes

,

pdf

,

Free

,

study material

,

Objective type Questions

,

ppt

,

video lectures

,

Semester Notes

,

Exam

,

past year papers

,

Programming Examples - Operator Overloading | Learn to Program with C++: Beginner to Expert - Back-End Programming

,

Viva Questions

,

Extra Questions

,

shortcuts and tricks

;