I have some problems with a class in my program, I have a header file and a .cxx file which I can change that actually contain my class, and a .cxx file that has my main which I cannot touch. Here is my code, help if you can.
Code:
#include <iostream>
#include "money.h"
using namespace std;
// this file should not change just because you change the money class
int main()
{
money mine, yours(123.45);
cout << "mine is " << mine << "\nYours is " << yours;
cout << "\nEnter a double ";
cin >> mine;
cout << "\nEnter a double ";
cin >> yours;
cout << "mine is " << mine << "\nYours is " << yours;
if ( mine < yours )
cout << "\n\nmine " << mine << " is less than " << yours << " yours\n";
else
cout << "\n\nmine " << mine << " is >= to " << yours << " yours\n";
return (0);
}
Code:
#include "money.h"
money::money() : Dollars(0), Cents(0)
{}
money::money(long in)
{
Dollars = (long) (in * 100);
}
money::money(int in1)
{
Cents = (int) (in1*1);
}
ostream& operator <<(ostream& p1, const money& p2)
{
p1 << "$" << (p2.Dollars / 100) << "." << (p2.Cents / 100) << "." << p2.Dollars % 100 << "." <<p2.Cents;
return p1;
} // overloaded output
istream& operator >>(istream& p1, money &p2)
{
long in;
int in1;
p1 >> in;
p1 >> in1;
p2.Dollars = (long) (in * 100);
p2.Cents = (long) (in1*.1);
return p1;
} // overloaded input
bool operator <(money p1, money p2)
{
return( (p1.Dollars + p1.Cents) < (p2.Dollars + p2.Cents) );
}
Code:
#ifndef MONEYH
#define MONEYH
#include <iostream>
using namespace std;
class money
{
public:
money();
money(long in, int in1);
friend ostream& operator <<(ostream &p1, const money& p2);
friend istream& operator >>(istream& p1, money &p2);
friend bool operator <(money p1, money p2); // true p1 < p2
private:
long Dollars;
int Cents;
}; // class money
#endif // MONEYH