Thursday 17 February 2011

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

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< }

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();
}

Overloading [] Operator in a class to access data within the class by indexing method.

In the previous article Overloading [] Operator, we overloaded the [] operator in a class to access data within the class by indexing method.

The operator [] function was defined as below:

  int myclass::operator[](int index)

{

// if not out of bound

if(index
return a[index];

}

As you can see, the above operator function is returning values, hence it could only be used on the right hand side of a statement. It’s a limitation!

You very well know that a statement like below is very common with respect to arrays:

a[1]=10;

But as I said, the way we overloaded the [] operator, statement like the one above is not possible. The good news is, it is very easy to achieve this.

For this we need to overload the [] operator like this:

  int &myclass::operator[](int index)

{

// if not out of bound

if(index
return a[index];

}

By returning a reference to the particular element, it is possible to use the index expression on the left hand side of the statement too.

The following program illustrates this:



// Example Program illustrating

// the overloading of [] operator

// ----

// now the index expression can be

// used on the left side too

#include



class myclass

{

// stores the number of element

int num;

// stores the elements

int a[10];



public:

myclass(int num);



int &operator[](int);

};



// takes the number of element

// to be entered.(<=10)

myclass::myclass(int n)

{

num=n;

for(int i=0;i
{

cout<<"Enter value for element "<1
<<":";

cin>>a[i];

}

}



// returns a reference

int &myclass::operator[](int index)

{

// if not out of bound

if(index
return a[index];

}



void main()

{

myclass a(2);



cout<0]<
cout<1]<


// indexing expression on the

// left-hand side


a[1]=21;

cout<1];

}