Overloadingoperator
Overloadingoperator
Fundamentals
There are many operators available that work on built-in types,
like int and double.
Operator overloading -- is the creation of new versions of these operators for
use with user-defined types.
Operators usually refer to C++ predefined operators:
o arithmetic operators: +, -, *, /, %
o relational operators: <, <=, ==, !=, >, >=
o assignment operator: =
o logical operators: &&, ||, !
o input/output operators: <<, >>
It is not as difficult as it sounds. Some things to note:
o An operator in C++ is just a function that is called with special
notation (usually more intuitive or familiar notation). Overloading an
operator simply involves writing a function.
o C++ already does some operator overloading implicitly on built-in
types. Consider the fact that the + operator already works for ints,
floats, doubles, and chars. There is really a different version of the +
operator for each type.
Operator overloading is done for the purpose of using familiar operator notation
on programmer-defined types (classes).
But, for user-defined data types (Classes), C++ doesn't have definitions for
those operators.
class Money
{
public:
Money();
Money(int d, int c);
Money(int allc);
So if you want to use familiar syntax with classes, you must write the
definition for the (overloading) operators for the class.
Member function
The first way is by class method (member function). This is the most popular
way.
#include <iostream>
using namespace std;
class Money
{
public:
Money() : dollar(0), cent(0) {}
Money(int d, int c) : dollar(d), cent(c) {}
Money(int allc);
// friend functions
friend ostream& operator<<(ostream& out, const Money & m);
friend istream& operator>>(istream& in, Money & m);
friend bool operator>(const Money &, const Money &);
private:
int dollar;
int cent;
};
#endif
__________________________________________________________________________
Money::Money(int allc)
{
dollar = allc / 100;
cent = allc % 100;
}
return out;
}
m.dollar = static_cast<int>(moneyAsDouble);
m.cent = static_cast<int>(moneyAsDouble * 100) % 100;
return in;
}
// friend function
bool operator>(const Money & m1, const Money & m2)
{
int thistotal = m1.dollar * 100 + m1.cent;
int m2total = m2.dollar * 100 + m2.cent;
return (thistotal > m2total);
}
bool operator<(const Money & m1, const Money & m2) // note: 2 arguments and
NO Money::
{
int thistotal = m1.getDollars() * 100 + m1.getCents();
int m2total = m2.getDollars() * 100 + m2.getCents();
return (thistotal < m2total);
}
____________________________________________________________________________
_
// filename: myMoneyApp.cpp
//
// An application program which uses Money objects (defined in
// "money.h").
#include <iostream>
using namespace std;
#include "money.h"
int main()
{
Money m1(2, 98), m2(15, 2), m3;
cout << m1 << " + " << m2 << " = " << m3 << endl;
if (m1 != m2)
cout << "Not equals.\n";
else
cout << "Equals.\n";
if (m1 > m2)
cout << "Greaterthan.\n";
else
cout << "NOT Greaterthan.\n";
system("pause");
return 0;
}
Also the operator cannot access private members in the parameter objects.
class Money
{
public:
Money();
Money(int d, int c);
Money(int allc);
Friend function
Yet another way is to use friend function. Friend functions are declared
within a class, but they are NOT class methods.
A friend function is actually a regular function which has a privilege to access
private members in the parameter objects.
class Money
{
public:
Money();
Money(int d, int c);
Money(int allc);
Money operator+(const Money & mo2) const;
...
// friend functions
friend ostream& operator<<(ostream& out, const Money & m); // to be able
to do cout << obj
friend istream& operator>>(istream& in, Money & m); // to be able to do
cint >> obj
private:
int dollar;
int cent;
};
// no keyword "friend" in the function definition
ostream& operator<<(ostream& out, const Money & m)
{
out << "$" << m.dollar // dollar private in m -- OK
<< "." << m.cent; // cent private in m -- OK
return out;
}
m.dollar = static_cast<int>(moneyAsDouble);
m.cent = static_cast<int>(moneyAsDouble * 100) % 100;
return in;
}
Important Remarks
For all 3 ways for operator overloading, they are all called the same
way (i.e., infix notation).
int main()
{
Money m1(2, 98), m2(15, 2), m3;
private:
int dollar;
int cent;
};
.....
Money operator*(double r, const Money & m)
{
int total = m.toAllCents() * r;
Money local(total);
return local;
}
Also for the reason above, the operator<< and operator>> are often
implemented as friend functions.
int main()
{
Money m1(2, 98);
private:
int ar[5];
};
int main()
{
DoubleArray5 myarray(0.0);
private:
int count;
};
Counter Counter::operator++()
{
// First, increment 'count'.
count++;
// Second, create a local object with the new count.
Counter local(count);
// Third, return the local object.
return local;
}
Counter Counter::operator++(int i) // param value is IGNORED!!
{
// First, create a local object with the current count.
Counter local(count);
// Second, increment count.
count++;
// Third, return the local object.
return local;
}
Polar p;
Cartesian c;
p=c;
//or
c=p;
Class Cartesian
{
double x;
double y;
public:
Cartesian()
{x=0;y=0;}
Cartesian(doubly x, double y)
{
this.x=x;
this.y=y;
}
//added constructor
Cartesian(Polar p)
{
double r=P.getRadius();
double a=p.getAngle();
x=r*cos(a;)
y=r*cos(a);
}
};
Class Polar
{
double radius;
double angle;
public:
Polar()
{
radius=0;
angle=0;
}
Polar (double r, double a)
{
radius=r;
angle=a;
}
operator Cartesian()
{
double x=Radius*cos(angle);
double y=radius*sin(angle);
return Cartesian(x,y);
}
};
15