Sie sind auf Seite 1von 6

# AJ/Handout 8 -1- Object-Oriented Programming

Lesson 8

Objectives

 Comparison operator

##  The subscript operator

We have done two different kind of operator overloading in previous two examples. One
was unary operator and second was binary operator. We can put both of them in same
program or class. And can use them accordingly. Compiler is sharp enough to distinguish
which operator is being used and which member function to invoke.

Comparison operator
We are already familiar with comparison operators like “if (x < y) ? true : false”. Lets have
an example which covers this part very beautifully.
------------------------------------------------------------------------------------------------
#include <iostream> //overloaded ‘<’ operator compare two Distances
using namespace std;
class Distance
{
private:
int feet;
float inches;
public:
Distance() : feet(0),inches(0.0)
{}
Distance(int ft, float in):feet(ft),inches(in)
{}
Void getdis()
{
cout<<”\n Enter Feet:”; cin>>feet;
cout<<”\n Enter Inches:”; cin>>inches;
}
Void showdist() const
{
Cout<<feet<<”\’-”<<inches;
}

## Bool operator < (Distance) const;

};
Bool Distance::operator < (Distance d2) const
{
Float bf1=feet+inches/12;
AJ/Handout 8 -2- Object-Oriented Programming

Float bf2=d2.feet+d2.inches/12;
Return (bf1<bf2) ? true : false;
}
////////////////////////////////////////
Void main()
{
Distance dist1;
Dist1.getdist();

Distance dist2(6,2.5);

Cout<<”\nDist1=”; dist1.showdist();
Cout<<”\nDist2=”; dist2.showdist();

If(dist1<dist2)
Cout<<”\nDist1 is less than dist 2”;
Else
Cout<<”\nDist1 is greater than or equal to dist2”;
}

Comparing strings
#include <iostream> //overloaded ‘ = = ’ operator compare two Distances
using namespace std;
class String
{
private:
enum {sz=80};
char str[sz];
public:
String()
{strcpy(str,’ ’);}
String(char s[])
{strcpy(str,s);}
void display () const
{cout<<str;}
void getstr()
{cin.get(str,sz);}
bool operator = = (String ss) const
{
return (strcmp(str,ss.str) = = 0) ? true : false;
}
};
//////////////////
void main()
{
String s1=”Yes”;
String s2=”No”;
String s3;

## Cout<<”Enter Yes or No:”;

S3.getstr();
AJ/Handout 8 -3- Object-Oriented Programming

If (s3 = = s1)
Cout<<”You said Yes”;
Else if (s3= =s2)
Cout<<”You said no”;
Else
}

## Arithmetic assignment operators

Now we want to deal with arithmetic assignment operators like +=, -=, *=, /= etc. the way
is quite simple we already seen the things.
#include <iostream> //overloaded ‘ + = ’ the arithmetic assignment operator
using namespace std;
class Distance
{
private:
int feet;
float inches;
public:
Distance():feet(0),inches(0.0)
{}
Distance(int ft, float in): feet(ft), inches (in)
{}
void getdist()
{
cout<<”Enter Feet:”; cin>>feet;
cout<<”Enter inches:”; cin>>inches;
}
void showdist()
{
cout<<feet<<”\’-”<<inches;
}

## void operator += (Distance);

};
//////////////////////////////////////////////
void Distance :: operator +=(Distance d2)
{
feet +=d2.feet;
inches +=d2.inches;
if(inches>12.0)
{
inches -=12.0;
feet ++;
}
}
////////////////////////////////////////////
void main()
{
Distance dist1;
Dist1.getdist();
Cout<<”Dist1=”; dist1.showdist();
AJ/Handout 8 -4- Object-Oriented Programming

Distance dist2(11.6.25);
Cout<<”Dist 2=”; dist2.showdist();

Dist1 +=dist2;
}
Example: do dist3=dist1 +=dist2; using “return Distance (feet, inches);”

## The Subscript Operator ( [] )

The subscript operator which is normally used to access the array elements can be
overloaded. It is useful if you want to modify the way array works in C++. For example
you might think of making a safe array. If you want to put or excess some particular array
index and if it is not available simply it display some error. This can be done by three ways
lets see what is each.
1. Separate get() and set() functions
This program provides two functions to put and read the element of any array. Both
functions check the validity first and then go accordingly.

## #include <iostream> //Safe array by get() and set() functions

#include<process.h>
using namespace std;
const int LIMIT=100;
class safearay
{
private:
int arr[LIMIT];
public:
void putel(int n, int elvalue)
{
if( n<0 || n>=LIMIT)
{cout<<”Array out of bound:”; exit(1);}
arr[n] = elvalue;
}
int getel(int n) const
{
if (n<0 || n>=LIMIT)
{cout<<”Array out of bound:”; exit(1);}
return arr[n];
}
};
////////////////////////
void main()
{
safearay sa1;

## for (int j=0;j<LIMIT;j++)

sa1.putel(j, j*10);

for (j=0;j<LIMIT;j++)
{
int temp = sa1.getel(j);
AJ/Handout 8 -5- Object-Oriented Programming

cout<<”Element”<<j<<”is”<<temp<<endl;
}
}
2. Single access() Function Returning by Refernce
#include <iostream> //Safe array by single access() function
#include<process.h>
using namespace std;
const int LIMIT=100;
class safearay
{
private:
int arr[LIMIT];
public:
int& access(int n) //return by reference
{
if( n<0 || n>=LIMIT)
{cout<<”Array index out of bound:”; exit(1);}
return arr[n];
}
};
////////////////////////
void main()
{
safearay sa1;

## for (int j=0;j<LIMIT;j++)

sa1.access(j) = j*10; //left side of equal sign

for (j=0;j<LIMIT;j++)
{
int temp = sa1.access(j);
cout<<”Element”<<j<<”is”<<temp<<endl;
}
}
3. Overloaded [] Operator Returning by Reference
Ultimately we are there, we want to be. So lets see how can be done this.
#include<process.h>
using namespace std;
const int LIMIT=100;
class safearay
{
private:
int arr[LIMIT];
public:
int& operator [] (int n) //return by reference
{
if( n<0 || n>=LIMIT)
{cout<<”Array index out of bound:”; exit(1);}
return arr[n];
}
};
AJ/Handout 8 -6- Object-Oriented Programming

////////////////////////
void main()
{
safearay sa1;

## for (int j=0;j<LIMIT;j++)

sa1.[j] = j*10; //left side of equal sign (input of the safe array)

for (j=0;j<LIMIT;j++)
{
int temp = sa1[j]; //right side of the equal sign (output of safe array)
cout<<”Element”<<j<<”is”<<temp<<endl;
}
}