Overload + operator

How do I overload the + operator? this is my code:

#ifndef MONEY_H
#define MONEY_H
#include <iostream>
using namespace std;

class Money;

ostream &operator<<(ostream&, const Money&);


class Money
{
private:
int dollars;
int cents;
char *type;

public:


Money()
{ char *t = "US DOLLARS";
dollars = 0;
cents = 0;
type = new char[strlen(t) + 1];
strcpy(type, t);
}

Money(int d, int c, char *t)
{
dollars = d;
cents = c;
type = new char[strlen(t) + 1];
strcpy(type, t);
}

Money(const Money &m)
{
type = new char[strlen(m.type) + 1];
strcpy(type, m.type);
cents = m.cents;
dollars = m.dollars;
}
void Set(int d, int c)
{ dollars = d; cents = c;}

void AddDollars(int d)
{ dollars += d; }

void DolCents(int d , int c)
{
dollars += d;
cents += c % 100;
dollars += c / 100;
}

void SubtractDolCent(int d, int c)
{
if (d > dollars)
exit(0);
else
dollars -= d;
if (c > cents){
dollars -= 1;
cents += 100;
cents -= c;
}
}

const Money operator=(const Money &m)
{
delete[]type;
type=new char[strlen(m.type)+1];
strcpy(type, m.type);
cents=m.cents;
dollars=m.dollars;
return *this;
}

Money operator+=(const Money &d)
{
dollars+=d.dollars;
return *this;
}
const Money operator+(Money dollars, const Money &d )
{
return dollars+=d;

}

void Print()
{ cout <<"Currency: " << type << " $"<< dollars << "." << cents << endl; }


};
#endif

#include <iostream>
#include "Money.h"
using namespace std;

int main()
{
Money m1(100,50,"Yen");
Money m2;
Money m3;
m3 = m2 = m1; //uses second = method
m2.Print();
m3.Print();
m3 = m3 + m2;
m3.Print();

cout << m1;


return 0;
}
Topic archived. No new replies allowed.