The World of Mayukh Bose

<< Back to Main Page Mayukh's world: e-mail me | about
Mayukh's World: Operator Overloading With C++ Thursday, November 23, 2017
Index
  • Introduction
  • What Operators?
  • Rules
  • Assignment Operator
  • More on Assignment
  • Arithmetic Operators
  • Arithmetic with Globals
  • Increment/Decrement
  • Operator-Assignment
  • Unary Operators
  • Relational Operators
  • Bitshift/Extraction
  • Subscript Operator
  • Function Call Operator
  • Bit and Logical Ops
  • Comma Operator
  • Pointer to Member
  • new and delete Ops
  • Credits and Thanks
  • My Free Software
  • Delphi/C++ Builder
  • Pocket PC
  • FreeSpeech Chat
  • C/C++ Freebies
  • Perl
  • Python
  • Ruby
  • C++ Operator Overloading Tutorial e-mail me

    Overloading Arithmetic Operators (+, -, *, /, %)

    In the previous sections, we learned how to overload the assignment operator. In this section, we will study how to overload arithmetic operators (+, -, *, / and %). We will study in detail how to overload the addition (+) operator and the same principles can be applied to the other arithmetic operators as well. If you've read the tutorial from the beginning, you can probably guess how the arithmetic operators should be overloaded. We start by adding a new member function to the class definition:
    	  class Complex {
    	  private:
    	    double real, imag;
    	  public:
    	    Complex() { real = imag = 0; }
    	    Complex(double r, double i) { real = r; imag = i; }
    	    double GetReal(void) { return real; }
    	    double GetImag(void) { return imag; }
    	    Complex& operator=(const Complex& num);
    	    Complex& operator=(const double& d);
    	    const Complex operator+(const Complex& num);
    	  };
    	
    Well, it isn't quite like the previous member functions we implemented. For one thing, this doesn't return Complex &, but instead returns a const Complex class variable. For now, let's just note the difference and move on to implementing the function.
    	  const Complex Complex::operator+(const Complex& num) {
    	    Complex tmp;
    	    tmp.real = real + num.real;
    	    // Can write as:
    	    // tmp.real = this->real + num.real;
    	    tmp.imag = imag + num.imag;
    	    return tmp;
    	  }
    	
    As you can see, the implementation differs a bit from the previous example too. For one thing, we're not returning *this. Instead, we're creating a temporary variable and assigning the results of the addition to tmp. This explains why the function returns const Complex and not Complex &. So, why do we need the temporary variable. This is because, if we did this instead:
    	  Complex Complex::operator+(const Complex& num) {
    	    real += num.real; // WRONG THING. It alters the current variable
    	    imag += num.imag; 
    	    return this;
    	  }
    	
    then, when we do something like this:
    	  Complex a(2,2), b(2,2);
    	  Complex c = a + b;
    	
    then the value of a will also be altered when adding b to it. This is clearly NOT what we want to do. Only the value of c should be affected. Hence we declare a temporary variable and return it. Since we can't return a reference to a temporary variable, this explains why the function returns Complex instead of Complex &.

    Now let's test this new operator out:
    	  int main(void) {
    	    Complex num1(2, 3), num2(5, 9);
    	    Complex num3;
    	    num3 = num1 + num2;
    	    cout << "num1 is " << num1.GetReal() << " + "
    	         << num1.GetImag() << "i" << endl;
    	    cout << "num2 is " << num2.GetReal() << " + "
    	    << num2.GetImag() << "i" << endl;
    	    cout << "num3 is " << num3.GetReal() << " + "
    	         << num3.GetImag() << "i" << endl;
    	    return 0;
    	  }
    	
    When we run this, we get the following output.
    	  num1 is 2 + 3i
    	  num2 is 5 + 9i
    	  num3 is 7 + 12i
    	
    We can also chain addition operators like this:
    	   Complex a(3, 2), b(4, 5), c(9, 6);
    	   Complex d = a + b + c;
    	
    Next, we will study how to do the same operation with a global function and see where the friend function comes in handy.

    Exercises

    1. Overload the subtraction (-) operator between two Complex objects.


    <<Previous: More on Assignment ^Up to Mayukh's World^ Next: Arithmetic with Globals >>

    Copyright © 2004 Mayukh Bose. All rights reserved.
    This work may be freely reproduced provided this copyright notice is preserved.
    OPTIONAL: Please consider linking to this website (http://www.mayukhbose.com/) as well.