Ad

Sunday, August 26, 2007

Using Friends to Overload all the Overloaded Arithmetic Operators

In the article Class with all the Overloaded Arithmetic Operators, we overloaded (almost) all the arithmetic operators in one program, similarly in this article we’ll be overloading them once again but now using friend functions.

We have already overloaded similar operators before (using friend functions), so you won’t be having any troubles in understanding the program below:


  // Program that Overloads
  // all the arithmetic operators
  // using friend functions
  #include <iostream.h>

  class myclass
  {
    int a;
    int b;

  public:
    myclass(){}
    myclass(int x,int y){a=x;b=y;}
    void show()
    {
      cout<<a<<endl<<b<<endl;
    }

    // declared as friend
    friend myclass operator+=(myclass&, myclass);
    friend myclass operator-=(myclass&, myclass);

    friend myclass operator++(myclass&);
    friend myclass operator--(myclass&);

    friend myclass operator++(myclass&, int);
    friend myclass operator--(myclass&, int);

    friend myclass operator+(myclass,myclass);
    friend myclass operator-(myclass,myclass);

    friend myclass operator/(myclass, myclass);
    friend myclass operator*(myclass, myclass);
    friend myclass operator%(myclass, myclass);
  };

  myclass operator+=(myclass &ob1, myclass ob2 )
  {
    ob1.a+=ob2.a;
    ob1.b+=ob2.b;

    return ob1;
  }

  myclass operator-=(myclass &ob1, myclass ob2 )
  {
    ob1.a-=ob2.a;
    ob1.b-=ob2.b;

    return ob1;
  }

  myclass operator++(myclass &ob)
  {
    ob.a++;
    ob.b++;

    return ob;
  }

  myclass operator--(myclass &ob)
  {
    ob.a--;
    ob.b--;

    return ob;
  }

  myclass operator++(myclass &ob, int x)
  {
    myclass temp;
    temp=ob;

    ob.a++;
    ob.b++;

    return temp;
  }

  myclass operator--(myclass &ob, int x)
  {
    myclass temp;
    temp=ob;

    ob.a--;
    ob.b--;

    return temp;
  }

  myclass operator+(myclass ob1,myclass ob2)
  {
    myclass temp;

    temp.a = ob1.a + ob2.a;
    temp.b = ob1.b + ob2.b;

    return temp;
  }

  myclass operator-(myclass ob1,myclass ob2)
  {
    myclass temp;

    temp.a = ob1.a - ob2.a;
    temp.b = ob1.b - ob2.b;

    return temp;
  }

  myclass operator/(myclass ob1,myclass ob2)
  {
    myclass temp;

    temp.a = ob1.a / ob2.a;
    temp.b = ob1.b / ob2.b;

    return temp;
  }

  myclass operator*(myclass ob1,myclass ob2)
  {
    myclass temp;

    temp.a = ob1.a * ob2.a;
    temp.b = ob1.b * ob2.b;

    return temp;
  }

  myclass operator%(myclass ob1,myclass ob2)
  {
    myclass temp;

    temp.a = ob1.a % ob2.a;
    temp.b = ob1.b % ob2.b;

    return temp;
  }

  void main()
  {
    myclass a(10,20);
    myclass b(100,200);

    a+=b;
    a.show();

    a++;
    a.show();

    a=a*a;
    a.show();

    a=a%b;
    a.show();
  }

Related Articles:

1 comment:

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.