CPP Overloading operators

What is Operator Overloading?

  • C++ enables you to add operators to your own classes, which could be:
    *    /    +    -    %    ^    &    |    ~    !    ,    =    <    >
    <=   >=   ++   ––   <<   >>   ==   !=   &&   ||   *=   /=   %=   ^=
    &=   |=   +=   -=   <<=  >>=  ->   ->*  []   ()   new  delete 
  • You are free to code whatever you want when you implements the operator to your class as long as you return the correct type in reasonable way.
  • You use the statement operator when you want to implement a overloaded operator for your class: Here is declaration for some of the operator you can use to implement in you class:
    Operator Declaration Description
    ++
    (prefix)
    const YourClass & operator ++ (); The return value should be a reference to you class so that it can be assigned to another object of your class.
    ++
    (postfix)
    const YourClass operator ++ (int); The return value should be a object of your class so that it can be assigned to another object of your class. The int argument is to flag to the compiler that this is a postfix version.
    --
    (prefix)
    const YourClass & operator -- (); The return value should be a reference to you class so that it can be assigned to another object of your class.
    --
    (postfix)
    const YourClass operator -- (int); The return value should be a object of your class so that it can be assigned to another object of your class. The int argument is to flag to the compiler that this is a postfix version.
    + const YourClass operator + (const YourClass & ) The return value should be a object of your class so that it can be assigned to another object of your class. The argument is a reference to the object, which is the second operand in a + operation.
    - const YourClass operator - (const YourClass & ) The return value should be a object of your class so that it can be assigned to another object of your class. The argument is a reference to the object, which is the second operand in a - operation.
    * const YourClass operator * (const YourClass & ) The return value should be a object of your class so that it can be assigned to another object of your class. The argument is a reference to the object, which is the second operand in a * operation.
    / const YourClass operator / (const YourClass & ) The return value should be a object of your class so that it can be assigned to another object of your class. The argument is a reference to the object, which is the second operand in a / operation.
    = const YourClass & operator = (const YourClass & ) The return value should be a reference to you class so that it can be assigned to another object of your class. The argument is a reference to the object, which is the from object in a = operation.
    += const YourClass & operator += (const YourClass & ) The return value should be a reference to you class so that it can be assigned to another object of your class. The argument is a reference to the object on the right side in a += operation.
    -= const YourClass & operator -= (const YourClass & ) The return value should be a reference to you class so that it can be assigned to another object of your class. The argument is a reference to the object on the right side in a -= operation..
    *= const YourClass & operator *= (const YourClass & ) The return value should be a reference to you class so that it can be assigned to another object of your class. The argument is a reference to the object on the right side in a *= operation.
    /= const YourClass & operator /= (const YourClass & ) The return value should be a reference to you class so that it can be assigned to another object of your class. The argument is a reference to the object on the right side in a /= operation.
    > bool operator > (const YourClass & ) The return value should be a bool type. The argument is a reference to the object, which is compared with in a > operation.
    >= bool operator >= (const YourClass & ) The return value should be a bool type. The argument is a reference to the object, which is compared with in a >= operation.
    < bool operator < (const YourClass & ) The return value should be a bool type. The argument is a reference to the object, which is compared with in a < operation.
    <= bool operator <= (const YourClass & ) The return value should be a bool type. The argument is a reference to the object, which is compared with in a <= operation.
    == bool operator == (const YourClass & ) The return value should be a bool type. The argument is a reference to the object, which is compared with in a == operation.
    != bool operator != (const YourClass & ) The return value should be a bool type. The argument is a reference to the object, which is compared with in a != operation.
    []
    (to change)
    ArrayObject & operator [] (unsigned short offset) The argument is the array index to use. You must return a reference of the object representing the item in an array at index posistion.
    []
    (to copy)
    const ArrayObject & operator [] (unsigned short offset) const The argument is the array index to use. You must return a copy object of the object representing the item in an array at index posistion.

What is Conversion Operator Overloading?

  • If you want to convert a object to a different object type rather than the object of your class, C++ provides the conversion operator.
  • Conversion operators do not specify a return value, even though they do return a converted value. As for operator overloading you use the operator keyword :
    operator typeOfObjectToReturn()
    Where typeOfObjectToReturn could be a primitive type or a class you have declared. Look for an implementation in the following example.
    Example using operator overloading and Conversion operators:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    static double PI=3.14;
    class Circle {
    private:
      double radius;
    public :
      Circle (double radius) { this->radius=radius;}
      double getArea() const {return radius*radius*PI; }
      double getRadius() const {return radius; }
      void setRadius(double radius)  { this->radius=radius; }
    };
    class Point {
    private:
      double top;
      double left;
    public :
      Point():top(0),left(0) {}
      void setPoint (double left, double top) {
        this->top=top;
        this->left=left;
      }
      double getTop() const {return top; }
      double getLeft() const {return left; }
    };
    
    class Rectangle {
    private:
      double width;
      double height;
      Point * points;
      void setPoints() {
        if (points!=0) delete [] points;
        points= new Point[4];
        points[0].setPoint(0,0);
        points[1].setPoint(width,0);
        points[2].setPoint(0,height);
        points[3].setPoint(width,height);
      }
    public :
      Rectangle():width(3),height(10), points(0) {setPoints();}
      Rectangle (double width,double height): points(0) {
        this->width=width;
        this->height=height;
        setPoints();
      }
      Rectangle(const Rectangle & rect): points(0) {
        this->width=rect.getWidth();
        this->height=rect.getWidth();
        setPoints();
      }
      ~Rectangle() {if (points!=0) delete [] points;}
      double getArea() const {return width*height; }
      double getWidth() const {return width; }
      void setWidth(double width)  { this->width=width; setPoints(); }
      double getHeight() const {return height; }
      void setHeight(double height)  { this->height=height; setPoints(); }
      // Implement prefix ++ operator 
      const Rectangle & operator ++ () {
        ++width;
        ++height;
        setPoints();
        return *this;
      }
      // Implement postfix ++ operator 
      const Rectangle operator ++ (int) {
        Rectangle temp(*this);
        width++;
        height++;
        setPoints();
        return temp;
      }
      // Implement prefix -- operator 
      const Rectangle & operator -- () {
        --width;
        --height;
        setPoints();
        return *this;
      }
      // Implement postfix -- operator 
      const Rectangle  operator -- (int) {
        Rectangle temp(*this);
        width--;
        height--;
        setPoints();
        return temp;
      }
      // Implement + operator 
      const Rectangle  operator + (const Rectangle & op2) {
        Rectangle temp(*this);
        if (temp.getWidth() > op2.getWidth()) {
          temp.setHeight(temp.getHeight()+op2.getArea()/temp.getWidth());
        } else {
          temp.setWidth(temp.getWidth()+op2.getArea()/temp.getHeight());
        }
        return temp;
      }
      // Implement - operator 
      const Rectangle  operator - (const Rectangle & op2) {
        Rectangle temp(*this);
        if (temp.getWidth() > op2.getWidth()) {
          temp.setWidth(temp.getWidth()- op2.getArea()/temp.getHeight());
          if (temp.getWidth()<0) temp.setWidth(0);
        } else {
          temp.setHeight(temp.getHeight()- op2.getArea()/temp.getWidth());
          if (temp.getHeight()<0) temp.setHeight(0);
        }
        return temp;
      }
    
      // Implement * operator 
      const Rectangle  operator * (const Rectangle & op2) {
        Rectangle temp(*this);
        temp.setWidth(temp.getWidth()*op2.getWidth());
        temp.setHeight(temp.getHeight()*op2.getHeight());
        return temp;
      }
      // Implement / operator 
      const Rectangle  operator / (const Rectangle & op2) {
        Rectangle temp(*this);
        temp.setWidth(temp.getWidth()/op2.getWidth());
        temp.setHeight(temp.getHeight()/op2.getHeight());
        return temp;
      }
      // Implement = operator 
      const Rectangle & operator = (const Rectangle & op2) {
        width=op2.getWidth();
        height=op2.getHeight();
        setPoints();
        return *this;
      }
    
      // Implement += operator 
      const Rectangle & operator += (const Rectangle & op2) {
        width+=op2.getWidth();
        height+=op2.getHeight();
        setPoints();
        return *this;
      }
    
      // Implement -= operator 
      const Rectangle & operator -= (const Rectangle & op2) {
        width -= op2.getWidth();
        if (width<0) width=0;
        height -= op2.getHeight();
        if (height<0) height=0;
        setPoints();
        return *this;
      }
    
      // Implement *= operator 
      const Rectangle & operator *= (const Rectangle & op2) {
        *this=*this * op2;
        return *this;
      }
    
      // Implement /= operator 
      const Rectangle & operator /= (const Rectangle & op2) {
        *this=*this / op2;
        return *this;
      }
    
      // Implement > operator 
      bool  operator > (const Rectangle & op2) {
        return getArea() > op2.getArea();
      }
      // Implement >= operator 
      bool  operator >= (const Rectangle & op2) {
        return getArea() >= op2.getArea();
      }
    
      // Implement < operator 
      bool  operator < (const Rectangle & op2) {
        return getArea() < op2.getArea();
      }
    
      // Implement <= operator 
      bool  operator <= (const Rectangle & op2) {
        return getArea() <= op2.getArea();
      }
      // Implement [] operator as reference
      Point & operator [](unsigned short offset)  {
        if (offset>=4)  {
          return points[3];
        }else {
          return points[offset];
        }
      }
      // Implement [] operator as copy 
      const Point operator [](unsigned short offset) const  {
        if (offset>=4)  {
          return points[3];
        }else {
          return points[offset];
        }
      }
    
      // Conversion Operators for a Circle object
      operator Circle() {
        return Circle(sqrt(getArea()/PI));
      }
    
    };
    int main(){
    
      Rectangle  Rect1(5,4);
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl << endl;
    
      // Testing prefix ++ operator
      cout << "Testing prefix ++ operator: \n";
      Rectangle Rect2=++Rect1 ;
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "Rect2 width: " << Rect2.getWidth()
        << " and height: " << Rect2.getHeight() << endl << endl;
    
      // Testing postfix ++ operator
      cout << "Testing postfix ++ operator: \n";
      Rectangle Rect3=Rect1++ ;
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "Rect3 width: " << Rect3.getWidth()
        << " and height: " << Rect3.getHeight() << endl << endl;
    
      // Testing prefix -- operator
      cout << "Testing prefix -- operator: \n";
      Rectangle Rect4=--Rect1 ;
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "Rect4 width: " << Rect4.getWidth()
        << " and height: " << Rect4.getHeight() << endl << endl;
    
      // Testing postfix -- operator
      cout << "Testing postfix -- operator: \n";
      Rectangle Rect5=Rect1-- ;
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "Rect5 width: " << Rect5.getWidth()
        << " and height: " << Rect5.getHeight() << endl << endl;
    
      // Testing +  operator
      cout << "Testing + operator: \n";
      Rectangle Rect6=Rect1 + Rect2;
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "Rect2 width: " << Rect2.getWidth()
        << " and height: " << Rect2.getHeight() << endl;
      cout << "Rect6 width: " << Rect6.getWidth()
        << " and height: " << Rect6.getHeight() << endl << endl;
    
      // Testing  -  operator
      cout << "Testing + operator: \n";
      Rectangle Rect7=Rect1 - Rect2;
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "Rect2 width: " << Rect2.getWidth()
        << " and height: " << Rect2.getHeight() << endl;
      cout << "Rect7 width: " << Rect7.getWidth()
        << " and height: " << Rect7.getHeight() << endl << endl;
    
      // Testing *  operator
      cout << "Testing * operator: \n";
      Rectangle Rect8=Rect1 * Rect2;
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "Rect2 width: " << Rect2.getWidth()
        << " and height: " << Rect2.getHeight() << endl;
      cout << "Rect8 width: " << Rect8.getWidth()
        << " and height: " << Rect8.getHeight() << endl << endl;
    
      // Testing  /  operator
      cout << "Testing / operator: \n";
      Rectangle Rect9=Rect1 / Rect2;
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "Rect2 width: " << Rect2.getWidth()
        << " and height: " << Rect2.getHeight() << endl;
      cout << "Rect8 width: " << Rect9.getWidth()
        << " and height: " << Rect9.getHeight() << endl << endl;
    
      // Testing = operator
      cout << "Testing = operator: \n";
      Rect7=Rect1 ;
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "Rect7 width: " << Rect7.getWidth()
        << " and height: " << Rect7.getHeight() << endl << endl;
    
      // Testing += operator
      cout << "Testing += operator: \n";
      cout << "Rect6 width: " << Rect6.getWidth()
        << " and height: " << Rect6.getHeight() << endl;
      Rect6 += Rect1 ;
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "Rect6 width: " << Rect6.getWidth()
        << " and height: " << Rect6.getHeight() << endl << endl;
    
      // Testing -= operator
      cout << "Testing -= operator: \n";
      cout << "Rect6 width: " << Rect6.getWidth()
        << " and height: " << Rect6.getHeight() << endl;
      Rect6 -= Rect1 ;
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "Rect6 width: " << Rect6.getWidth()
        << " and height: " << Rect6.getHeight() << endl << endl;
    
      // Testing *= operator
      cout << "Testing *= operator: \n";
      cout << "Rect6 width: " << Rect6.getWidth()
        << " and height: " << Rect6.getHeight() << endl;
      Rect6 *= Rect1 ;
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "Rect6 width: " << Rect6.getWidth()
        << " and height: " << Rect6.getHeight() << endl << endl;
    
      // Testing /= operator
      cout << "Testing /= operator: \n";
      cout << "Rect6 width: " << Rect6.getWidth()
        << " and height: " << Rect6.getHeight() << endl;
      Rect6 /= Rect1 ;
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "Rect6 width: " << Rect6.getWidth()
        << " and height: " << Rect6.getHeight() << endl << endl;
    
      // Testing > operator
      cout << "Testing > operator: \n";
      if (Rect2>Rect1)   cout << "Rect2 > Rect1 \n";
      else cout << "Rect2 <= Rect1 \n";
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "Rect2 width: " << Rect2.getWidth()
        << " and height: " << Rect2.getHeight() << endl << endl;
    
      // Testing < operator
      cout << "Testing < operator: \n";
      if (Rect2<Rect1)   cout << "Rect2 < Rect1 \n";
      else cout << "Rect2 >= Rect1 \n";
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "Rect2 width: " << Rect2.getWidth()
        << " and height: " << Rect2.getHeight() << endl << endl;
    
      // Testing [] operator
      cout << "Testing [] operator: \n";
      Rectangle Rect10(60,90);
      cout << "Rect10 upper left  point: " << Rect10[0].getLeft()
        << "," << Rect10[0].getTop() << endl;
      cout << "Rect10 upper right point: " << Rect10[1].getLeft()
        << "," << Rect10[1].getTop() << endl;
      cout << "Rect10 lower left  point: " << Rect10[2].getLeft()
        << "," << Rect10[2].getTop() << endl;
      cout << "Rect10 lower right point: " << Rect10[3].getLeft()
        << "," << Rect10[3].getTop() << endl << endl;
      Rect10[3].setPoint(234,456);
      cout << "Changed Rect10 lower right point: " << Rect10[3].getLeft()
        << "," << Rect10[3].getTop() << endl << endl;
    
    
      // Testing Conversion Operators for Rectangle to Circle
      cout << "Testing Conversion Operators for Rectangle to Circle: \n";
      Circle circle=Rect1;
      cout << "Rect1 width: " << Rect1.getWidth()
        << " and height: " << Rect1.getHeight() << endl;
      cout << "circle radoius: " << circle.getRadius() << endl << endl;
      return 0;
    }
    When we run this application the result will be:
    Rect1 width: 5 and height: 4
    
    Testing prefix ++ operator:
    Rect1 width: 6 and height: 5
    Rect2 width: 6 and height: 6
    
    Testing postfix ++ operator:
    Rect1 width: 7 and height: 6
    Rect3 width: 6 and height: 6
    
    Testing prefix -- operator:
    Rect1 width: 6 and height: 5
    Rect4 width: 6 and height: 6
    
    Testing postfix -- operator:
    Rect1 width: 5 and height: 4
    Rect5 width: 6 and height: 6
    
    Testing + operator:
    Rect1 width: 5 and height: 4
    Rect2 width: 6 and height: 6
    Rect6 width: 12.2 and height: 12.2
    
    Testing + operator:
    Rect1 width: 5 and height: 4
    Rect2 width: 6 and height: 6
    Rect7 width: 5 and height: 5
    
    Testing * operator:
    Rect1 width: 5 and height: 4
    Rect2 width: 6 and height: 6
    Rect8 width: 30 and height: 30
    
    Testing / operator:
    Rect1 width: 5 and height: 4
    Rect2 width: 6 and height: 6
    Rect8 width: 0.833333 and height: 0.833333
    
    Testing = operator:
    Rect1 width: 5 and height: 4
    Rect7 width: 5 and height: 4
    
    Testing += operator:
    Rect6 width: 12.2 and height: 12.2
    Rect1 width: 5 and height: 4
    Rect6 width: 17.2 and height: 16.2
    
    Testing -= operator:
    Rect6 width: 17.2 and height: 16.2
    Rect1 width: 5 and height: 4
    Rect6 width: 12.2 and height: 12.2
    
    Testing *= operator:
    Rect6 width: 12.2 and height: 12.2
    Rect1 width: 5 and height: 4
    Rect6 width: 61 and height: 61
    
    Testing /= operator:
    Rect6 width: 61 and height: 61
    Rect1 width: 5 and height: 4
    Rect6 width: 12.2 and height: 12.2
    
    Testing > operator:
    Rect2 > Rect1
    Rect1 width: 5 and height: 4
    Rect2 width: 6 and height: 6
    
    Testing < operator:
    Rect2 >= Rect1
    Rect1 width: 5 and height: 4
    Rect2 width: 6 and height: 6
    
    Testing [] operator:
    Rect10 upper left  point: 0,0
    Rect10 upper right point: 60,0
    Rect10 lower left  point: 0,90
    Rect10 lower right point: 60,90
    
    Changed Rect10 lower right point: 234,456
    
    Testing Conversion Operators for Rectangle to Circle:
    Rect1 width: 5 and height: 4
    circle radoius: 2.52377

    You can download this example here (needed tools can be found in the right menu on this page).

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.