Money Program

Why is "operator" used in this program or better yet what is its purpose?

#include<iostream>

using namespace std;

class Money

{

private:

int dollars;

int cents;

public:

Money();

Money(int newDollars, int newCents);

void Output() {cout<<dollars<<" "<<cents;}

const Money operator +(const Money &amount) const;

bool operator ==(const Money &amount) const;

};

Money::Money()
{
dollars=cents=0;
}

Money::Money(int newDollars, int newCents)
{
dollars=newDollars;

cents=newCents;
}

const Money Money::operator +(const Money &amount) const

{

return Money(dollars+amount.dollars, cents+amount.cents);

}

bool Money::operator ==(const Money &amount) const

{

return (dollars==amount.dollars && cents==amount.cents);

}

void main()

{
Money a(8,25), b(8,25);

Money c;

c=a+b;

a.Output();

cout<<endl;

b.Output();

cout<<endl;

c.Output();

cout<<endl<<endl;

Money y(8,25), z(8,25);

if (y==z)

cout<<"Object y and z are the same.";

else

cout<<"Object y and z are not the same.";

cout<<endl<<endl;
}
Please use code tags:
http://www.cplusplus.com/articles/jEywvCM9/

The key word operator is used to signify that you are declaring or defining an overloaded operator. Look up "operator overloading".
Topic archived. No new replies allowed.