Thursday, May 13, 2010

Operator overloading

There are two types of operator overloading:

* Unary operator overloading
* Binary operator overloading


Operator overloading is a very important aspect of object-oriented programming. Binary operators can be overloaded in a similar manner as unary operators.
Operator Overloading – Binary Operators

Binary operators, when overloaded, are given new functionality. The function defined for binary operator overloading, as with unary operator overloading, can be member function or friend function.

The difference is in the number of arguments used by the function. In the case of binary operator overloading, when the function is a member function then the number of arguments used by the operator member function is one (see below example). When the function defined for the binary operator overloading is a friend function, then it uses two arguments.

Binary operator overloading, as in unary operator overloading, is performed using a keyword operator.

Binary operator overloading example:
#include
class Exforsys
{
private:
int x;
int y;

public:
Exforsys() //Constructor
{ x=0; y=0; }

void getvalue( ) //Member Function for Inputting Values
{
cout << “\n Enter value for x: “;
cin >> x;
cout << “\n Enter value for y: “;
cin>> y;
}

void displayvalue( ) //Member Function for Outputting Values
{
cout <<”value of x is: “ << x <<”; value of y is: “<}

Exforsys operator +(Exforsys);
};

Exforsys Exforsys :: operator + (Exforsys e2)
//Binary operator overloading for + operator defined
{
int x1 = x+ e2.x;
int y1 = y+e2.y;
return Exforsys(x1,y1);
}

void main( )
{
Exforsys e1,e2,e3; //Objects e1, e2, e3 created
cout<<\n”Enter value for Object e1:”;
e1.getvalue( );
cout<<\n”Enter value for Object e2:”;
e2.getvalue( );
e3= e1+ e2; //Binary Overloaded operator used
cout<< “\nValue of e1 is:”<cout<< “\nValue of e2 is:”<cout<< “\nValue of e3 is:”<}

The output of the above program is:

Enter value for Object e1:
Enter value for x: 10
Enter value for y: 20
Enter value for Object e2:
Enter value for x: 30
Enter value for y: 40
Value of e1 is: value of x is: 10; value of y is: 20
Value of e2 is: value of x is: 30; value of y is: 40
Value of e3 is: value of x is: 40; value of y is: 60


unary operator overloading

#include
using namespace std;

class MyClass {
int x, y, z;
public:
MyClass() {
x = y = z = 0;
}
MyClass(int i, int j, int k) {
x = i;
y = j;
z = k;
}

MyClass operator+(MyClass op2);
MyClass operator=(MyClass op2);
MyClass operator++();

void show() ;
} ;

// Overload +.
MyClass MyClass::operator+(MyClass op2)
{
MyClass temp;

temp.x = x + op2.x; // These are integer additions
temp.y = y + op2.y; // and the + retains is original
temp.z = z + op2.z; // meaning relative to them.
return temp;
}

// Overload assignment.
MyClass MyClass::operator=(MyClass op2)
{
x = op2.x; // These are integer assignments
y = op2.y; // and the = retains its original
z = op2.z; // meaning relative to them.
return *this;
}

// Overload the prefix version of ++.
MyClass MyClass::operator++()
{
x++; // increment x, y, and z
y++;
z++;
return *this;
}

// Show X, Y, Z coordinates.
void MyClass::show()
{
cout << x << ", ";
cout << y << ", ";
cout << z << endl;
}

int main()
{
MyClass a(1, 2, 3), b(10, 10, 10), c;
a.show();
b.show();

c = a + b; // add a and b together
c.show();

c = a + b + c; // add a, b and c together
c.show();

c = b = a; // demonstrate multiple assignment
c.show();
b.show();

++c; // increment c
c.show();

return 0;
}



Functions for both the cases are defined as follows:

a) Pre Increment


Rational operator++()
{
Rational temp;
temp.num = num + den;
temp.den = den;
num = temp.num;
return temp;
}


b) Post Increment


Rational operator++(int dummy)
{
Rational temp;
temp.num = num + den;
temp.den = den;
num = temp.num;
return temp;
}


Static Members

Static means something maintaining state either forever or up to some point. In C++, static members get their life active as soon as the execution of the program starts, no matter, whether they are created in local,
global or class scope.

Following points are to be noted regarding the static members:

* They are allocated as soon as the execution of program starts, regardless of their scope.
* They are de-allocated at the end of the program execution.
* Since they have lifetime, during the whole of the program execution, they retain the values in them.
* They are usable in their scope only though their life is for the whole of the program execution.
* On creation, they are initialized with ZERO.(other locally created variables shall get some undefined (garbage) value in them).


#include
using namespace std;

class Test {

public:
static int objCounter;

Test()
{
Test::objCounter++;
}

~Test()
{
Test::objCounter--;
}
};

int Test::objCounter;

int main(int argc, char * argv[]) {
Test obj1, *ptr, obj2;
cout << endl << "Number of Objects : " << Test::objCounter;
//Will print -> Number of Objects : 2
ptr= new Test;
cout << endl << "Number of Objects : " << Test::objCounter;
//Will print -> Number of Objects : 3
delete ptr;
cout << endl << "Number of Objects : " << Test::objCounter;
//Will print -> Number of Objects : 2
return 0;
}



#include
using namespace std;

class Test {

public:
static int objCounter;

Test()
{
Test::objCounter++;
}

~Test()
{
Test::objCounter--;
}
};

int Test::objCounter;

int main(int argc, char * argv[]) {
Test obj1, *ptr, obj2;
cout << endl << "Number of Objects : " << Test::objCounter;
//Will print -> Number of Objects : 2
ptr= new Test;
cout << endl << "Number of Objects : " << Test::objCounter;
//Will print -> Number of Objects : 3
delete ptr;
cout << endl << "Number of Objects : " << Test::objCounter;
//Will print -> Number of Objects : 2
return 0;
}


Following are the important points specific to CPP:

* Static members of the class must be re-declared outside the class (globally).
* Static data member can be used directly in static member functions only, otherwise they are used using Scope Resolution Operator.
* Static members are created only once as soon as the execution of the program starts and the same is shared among all the objects (obj1, obj2, *ptr) of the class.
* Static members are constrained by the access modifiers (public, private, protected).
* Static functions can only use static data members or local variables, it can not use non static data members of the class.

#include
using namespace std;

class Test1
{
static int testvar;
int testvar1; //non static member
public:
static void testfunc()
{
cout << endl << "Value of static variable : " << testvar;

//Non static members can not be used in static functions.
//cout << endl << "Value of nonstatic variable : " << testvar1;
}
};

int Test1::testvar;

int main(int argc, char *argv[]) {
Test1::testfunc();
return 0;
}




No comments:

Post a Comment

Followers

HomeraDurani

My photo
Rajkot, Gujarat, India
Lecturer in MCA Department Atmiya Institute of Technology & Science Rajkot