Example Class with Overloaded Operators

// Savitch display 8.8

//Program to demonstrate the class Money.

#include <iostream.h>

#include <fstream.h>

#include <stdlib.h>

#include <ctype.h>

const int FALSE = 0;

const int TRUE = 1;

//Class for amounts of money in U.S. currency.

class Money {

public:

    friend Money operator +(const Money& amount1, const Money& amount2);

    friend Money operator -(const Money& amount1, const Money& amount2);

    friend Money operator -(const Money& amount);

    friend int operator ==(const Money& amount1, const Money& amount2);

    Money(long dollars, int cents);

    Money(long dollars);

    Money( );

    double get_value( ) const;

    friend istream& operator >>(istream& ins, Money& amount);

    friend ostream& operator <<(ostream& outs, const Money& amount);

private:

    long all_cents;

};

int digit_to_int(char c);

//Precondition: c is one of the digits '0' through '9'.

//Returns the integer for the digit; e.g., digit_to_int('3') returns 3.

int main( ) {

    Money amount;

    ifstream in_stream;

    ofstream out_stream;

    in_stream.open("infile.dat");

    if (in_stream.fail( ))

    {

        cout << "Input file opening failed.\n";

        exit(1);

    }

    out_stream.open("outfile.dat");

    if (out_stream.fail( ))

    {

        cout << "Output file opening failed.\n";

        exit(1);

    }

    in_stream >> amount;

    out_stream << amount << " copied from the file infile.dat.\n";

    cout << amount << " copied from the file infile.dat.\n";

    in_stream.close( );

    out_stream.close( );

    return 0;

}

istream& operator >>(istream& ins, Money& amount) {

    char one_char, decimal_point,

         digit1, digit2; //digits for the amount of cents

    long dollars;

    int cents,

        negative;//set to TRUE if input is negative.

    ins >> one_char;

    if (one_char == '-')      

    {

        negative = TRUE;

        ins >> one_char; //read '$'      // 1. $1.30

    }

    else

        negative = FALSE;

    //if input is legal, then one_char == '$'

    ins >> dollars >> decimal_point >> digit1 >> digit2;

    if ( one_char != '$' || decimal_point != '.'

         || !isdigit(digit1) || !isdigit(digit2) )

    {

        cout << "Error illegal form for money input\n";

        exit(1);

    }

    cents = digit_to_int(digit1)*10 + digit_to_int(digit2);

    amount.all_cents = dollars*100 + cents;   // 2. 1.30 is 130 cents

    if (negative)

        amount.all_cents = -amount.all_cents;

    return ins;

}

int digit_to_int(char c) {

    return ( int(c) - int('0') );  // 3. difference in ASCII values

}

ostream& operator <<(ostream& outs, const Money& amount) {

    long positive_cents, dollars, cents;

    positive_cents = labs(amount.all_cents);

    dollars = positive_cents/100;

    cents = positive_cents%100;

    if (amount.all_cents < 0)

        outs << "-$" << dollars << '.';

    else

        outs << "$" << dollars << '.';

    if (cents < 10)

        outs << '0';

    outs << cents;

    return outs;

}

 Money operator -(const Money& amount1, const Money& amount2) {

    Money temp;

    temp.all_cents = amount1.all_cents - amount2.all_cents;

    return temp;

}

Money operator -(const Money& amount) {

    Money temp;

    temp.all_cents = -amount.all_cents;

    return temp;

}

Money operator +(const Money& amount1, const Money& amount2) {

    Money temp;

    temp.all_cents = amount1.all_cents + amount2.all_cents;

    return temp;

}

int operator ==(const Money& amount1, const Money& amount2) {

    return (amount1.all_cents == amount2.all_cents);

}

Money::Money(long dollars, int cents) {

    all_cents = dollars*100 + cents;

}

Money::Money(long dollars) {

    all_cents = dollars*100;

}

Money::Money( ) {

    all_cents = 0;

}

double Money::get_value( ) const {

    return (all_cents * 0.01);

}

Return to Index