Ad

Friday, August 17, 2007

Class with all the Overloaded Arithmetic Operators

So far we have learnt to overload +, -, +=, -= etc. operators and we know what is the basic theory behind operator overloading.

In this article we are going to design a program with a class that overloads almost all the arithmetic operators (+, -, +=, -=, /, *, ++, --)

This is a program centric article, so we straightway have a look at the example program.

Since nothing new has been introduced, I leave it up to you to understand everything yourself.


  // Example Program with a
  // a class having almost
  // all the arithmetic
  // operators overloaded
  #include <iostream.h>

  class myclass
  {
    int a;
    int b;

  public:
    myclass(){}
    myclass(int,int);
    void show();

    myclass operator+(myclass);
    myclass operator-(myclass);

    // prefix
    myclass operator++();
    myclass operator--();

    // postfix
    myclass operator++(int);
    myclass operator--(int);

    myclass operator+=(myclass);
    myclass operator-=(myclass);

    myclass operator/(myclass);
    myclass operator*(myclass);

  };

  myclass::myclass(int x,int y)
  {
    a=x;
    b=y;
  };

  void myclass::show()
  {
    cout<<a<<endl<<b<<endl;
  }

  myclass myclass::operator+(myclass ob)
  {
    myclass temp;

    temp.a=a + ob.a;
    temp.b=b + ob.b;

    return temp;
  }

  myclass myclass::operator-(myclass ob)
  {
    myclass temp;

    temp.a=a - ob.a;
    temp.b=b - ob.b;

    return temp;
  }

  myclass myclass::operator++()
  {
    a++;
    b++;

    return *this;
  }

  myclass myclass::operator--()
  {
    a--;
    b--;

    return *this;
  }

  myclass myclass::operator++(int x)
  {
    myclass old;
    old=*this;

    a++;
    b++;

    return old;
  }

  myclass myclass::operator--(int x)
  {
    myclass old;
    old=*this;

    a--;
    b--;

    return old;
  }

  myclass myclass::operator+=(myclass ob)
  {
    a+=ob.a;
    b+=ob.b;

    return *this;
  }

  myclass myclass::operator-=(myclass ob)
  {
    a-=ob.a;
    b-=ob.b;

    return *this;
  }

  myclass myclass::operator/(myclass ob)
  {
    myclass temp;

    temp.a=a / ob.a;
    temp.b=b / ob.b;

    return temp;
  }

  myclass myclass::operator*(myclass ob)
  {
    myclass temp;

    temp.a=a * ob.a;
    temp.b=b * ob.b;

    return temp;
  }

  void main()
  {
    myclass ob1(10,20);
    myclass ob2(100,200);

    ob1+=ob2;

    ob1.show();
    ob2.show();

    ob1=ob1/ob2;
    ob1.show();

    ob1=ob1*ob2;
    ob1.show();

    ob2.show();
  }

Related Articles:

2 comments:

  1. In these examples, and a few others I saw in the related links, you have always hard-coded the values sent as parameters. How do we do it by accepting user input?

    ReplyDelete
  2. Hi Darshit Shah,

    Thanks for your comment.

    Well, this is a post on "Overloading operators" and not to teach you how to accept values from user. if you can't do that better learn that first.

    In each post I focus on the main funda and not on those things.

    And what do you suppose I'd have used to input value from user?

    From keyboard, then someone else might have asked me to use combo box or something else. The thing is I focus on the core, leaving other things to the progarmmer (if you're one).

    ReplyDelete

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.