OVERLOADING OF BINARY OPERATORS in C++
Binary operators overloaded by means of member functions take one formal argument which is the value to the right of the operator. Binary operators, overloaded by means of friend functions take two arguments.
As arithmetic operators are binary operators, they require two operands to perform the operation. Whenever an arithmetic operator is used for overloading, the operator overloading function is invoked with single class objects.
Example 1.
A program to perform overloading of a plus operator for fi nding the sum of the two given class objects.
//overloading arithmetic operators
#include <iostream>
using namespace std;
class sample {
private :
int value;
public :
sample ();
sample (int one);
sample operator+ (sample objb);
void display();
};
sample ::sample ()
{
value = 0;
}
sample :: sample (int one)
{
value = one;
}
sample sample :: operator+ (sample objb)
{
sample objsum;
objsum.value = value+objb.value;
return(objsum);
}
void sample :: display()
{
cout << “value = ” << value << endl;
}
int main()
{
sample obj1(10);
sample obj2(20);
sample objsum;
objsum = obj1 + obj2;
obj1.display();
obj2.display();
objsum.display();
return 0;
}
Output of the above program
value = 10
value = 20
value = 30
Example 2.
A program to perform simple arithmetic operations of two complex numbers using operator overloading.
// complex number operations
//using operator overloading
struct complex {
oat real;
oat imag;
};
complex operator + (complex a, complex b);
complex operator - (complex a, complex b);
complex operator * (complex a, complex b);
complex operator / (complex a, complex b);
#include <iostream>
using namespace std;
#include <stdio.h>
int main()
{
complex a,b,c;
int ch;
void menu(void);
cout << “enter a rst complex number \n”;
cin >> a.real >> a.imag;
cout << “ enter a second complex number \n”;
cin >> b.real >> b.imag;
cout << “ rst complex number \n”;
cout << a.real;
if (a.imag < 0)
cout << “-i” << (-1)*a.imag << endl;
else
cout << “+i”<< a.imag << endl;
cout << “ second complex number \n”;
cout << b.real;
if (b.imag < 0)
cout << “-i” << (-1)*b.imag << endl;
else
cout << “+i”<< b.imag << endl;
menu();
while (( ch = getchar()) != ‘q’) {
switch (ch) {
case ‘a’ :
c = a+b;
cout << “ Addition of two complex numbers \n”;
cout << c.real;
if (c.imag < 0)
cout << “-i” << (-1)*c.imag << endl;
else
cout << “+i”<< c.imag << endl;
break;
case ‘s’ :
c =a-b;
cout << “ Subtraction of two complex numbers \n”;
cout << c.real;
if (c.imag < 0)
cout << “-i” << (-1)*c.imag << endl;
else
cout << “+i”<< c.imag << endl;
break;
case ‘m’ :
c = a*b;
cout << “ Multiplication of two complex numbers \n”;
cout << c.real;
if (c.imag < 0)
cout << “-i” << (-1)*c.imag << endl;
else
cout << “+i”<< c.imag << endl;
break;
case ‘d’ :
c = a/b;
cout << “ Division of two complex numbers \n”;
cout << c.real;
if (c.imag < 0)
cout << “-i” << (-1)*c.imag << endl;
else
cout << “+i”<< c.imag << endl;
break;
case ‘t’:
menu();
break;
} // end of switch
}
return 0;
} // end of main program
void menu(void)
{
cout << “ complex number operations \n”;
cout << “ t -> menu () \n”;
cout << “ a -> addition \n”;
cout << “ s -> subtraction \n”;
cout << “ m -> multiplication \n”;
cout << “ d -> division \n”;
cout << “ q -> quit \n”;
cout << “ option, please ? \n”;
}
complex operator + ( struct complex a, struct complex b)
{
complex c;
c.real = a.real+b.real;
c.imag = a.imag+b.imag;
return(c);
}
complex operator - (struct complex a, struct complex b)
{
complex c;
c.real = a.real-b.real;
c.imag = a.imag-b.imag;
return(c);
}
complex operator * (struct complex a, struct complex b)
{
complex c;
c.real = (a.real*b.real)-(a.imag*b.imag);
c.imag = (a.real*b.imag)+(a.imag*b.real);
return(c);
}
complex operator / (struct complex a, struct complex b)
{
complex c;
oat temp;
temp = (b.real*b.real)+(b.imag*b.imag);
c.real = ((a.real*b.real)+(a.imag*b.imag))/temp;
c.imag = ((b.real*a.imag)-(a.real*b.imag))/temp;
return(c);
}
Output of the above program
enter a rst complex number
1 1
enter a second complex number
2 2
rst complex number
1+i1
second complex number
2+i2
complex number operations
t -> menu ()
a -> addition
s -> subtraction
m -> multiplication
d -> division
q -> quit
option, please?
a
Addition of two complex numbers
3+i3
s
Subtraction of two complex numbers
-1-i1
m
Multiplication of two complex numbers
0+i4
d
Division of two complex numbers
0.5+i0
q
Example 3.
A program to demonstrate how to overload a less than operator in a program to compare two classes of objects. The operator overloading returns either 1 or 0 as the case may be.
//overloading comparison operators
#include <iostream>
using namespace std;
class sample {
private:
int value;
public :
sample();
sample(int one);
void display();
int operator < (sample obj);
};
sample :: sample ()
{
value = 0;
}
sample :: sample (int one)
{
value = one;
}
void sample :: display()
{
cout << “ value ” << value << endl;
}
int sample :: operator < (sample obja)
{
return (value < obja.value);
}
int main()
{
sample obja(20);
sample objb(100);
cout << (obja < objb) << endl;
cout << (objb < obja) << endl;
return 0;
}
Output of the above program
1
0

0 comments
Post a Comment