CIS 22 - Data Structures
Spring 2005

Assignment #1b - A Currency Class with Overloaded Operators

Due:
February 24, 2005

Description

This assignment is to extend the Currency class of Assignment 1, by adding implementations of the copy constructor, the assignment operator, and by overloading the arithmetic and relational operators.

A description of the class is given below. The parts in red are the new parts to be done in this assignment.The function in green is optional and for extra credit. Overloading the cin operator >> can also be done for extra credit.

class Currency{

   public:
	Currency(); //default constructor 
	Currency(double amount);  
	Currency(int dollars, int cents); 

	Currency(const Currency & c); // copy constructor 

	void setValue(double amount);
	void setValue(int dollars, int cents);

	int getDollars() const; // returns dollar amount
	int getCents() const;   // returns cents

	void add_amount(double amount); //add amount, e.g. 1.50   
	void add_currency(const Currency &); //add another currency object

	void print() const;

	Currency & operator= (const Currency &c); //assignment operator 

	// there are two ways of adding to a Currency object - either add another
	// Currency object to it, e.g. x+y,  or add an amount to it e.g. x + 2.50
	Currency & operator + (const Currency &c); // to add two Currency objects
	Currency & operator + (const double amount); //add amount
	 
	// this is extra credit - not required:
	Currency & operator += (const Currency &c);
			// so x+=y is the same as x = x+y
	 
	friend ostream & operator << (ostream &, const Currency);
	
   private:
		// this is up to you - you can either use
		// a double value or two separate ints for the
		// dollars and cents
};

bool operator == (const Currency &c1, const Currency &c2);
// you add the headers of the other 5 relational operators

Your job is to complete the implementation of the class and to write a main program to fully test the class. Make sure to test the new operators!

You may find it useful to look at the code for the Checkbook class.

Testing

Convince me that your class works! Make sure to test for invalid data/ invalid operations (e.g., the value for cents cannot be greater than 99)

What to submit

Make sure to hand in all the source code as well as the output of all of the test cases.