Money classs

This is a discussion on Money classs within the C++ Programming forums, part of the General Programming Boards category; I am suppose to creat a Money class that has the following methos with operators for: Addition, Assignment(+=), Substraction, Minus ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    Angry Money classs

    I am suppose to creat a Money class that has the following methos with operators for:
    Addition, Assignment(+=), Substraction, Minus equal, Multiplication, Times equal, division, divide equal, Negation, Equality, Greater Than, Less than, Greater and Less than or equal to, File input and output.

    The public interface for Money is as follows: (it has to be done this way)
    Money()
    Money(long, int) // first parameter is dollars, second is cents
    Money(long) // long is number of dollars, cents is .00
    double getValue() // returns dollar amount of Money, ie: 2.27
    void displayValue() // displays as $2.27 or -$2.27
    int getValueInCents() //returns amount in cents, ie: $2.27 would be 227

    I am only confusing myself more and more, I had my program down to one error. Now I have a ton of errors. Also, I need help getting started on my Negation overloading and the comparison operators like <, >=, etc.

    here is what I have already and the test file it must work with.

    1
    5 #include<iostream>
    6
    7 #include<iostream>
    8 #include<fstream>
    9 #include<iomanip>
    10
    11 using namespace std;
    12
    13 #ifndef MONEY
    14 #define MONEY
    15
    16 class Money
    17 {
    18 friend Money operator* (Money, Money); //Overloads * to implement this-Money * value
    19 //post: this-Money is unchanged
    20 //returns: this-Money * valu
    21 friend Money operator/ (Money, Money); //Overloads / to implement this-Money / value
    22 //post: this-Money is unchanged
    23 //returns: this-Money / value
    24 /*friend Money operator> (Money, Money);
    25 friend Money operator< (Money, Money);
    26 friend Money operator>= (Money, Money);
    27 friend Money operator<= (Money, Money);*/
    28 friend Money& operator- (Money, Money);
    29 friend Money& operator*= (Money&, Money);
    30 friend Money& operator/= (Money&, Money);
    31 friend Money& operator-= (Money&, Money);
    32 friend ostream& operator<< (ostream&, Money);
    33 friend ostream& operator>> (ostream&, Money);
    34
    35
    36 public:
    37
    38 //Class constructors
    39
    40 Money(); //Default Constructor
    43 Money(long d, int c); //Constructor
    49 Money(long dol); //Constructor
    56
    59 Money& operator= (const Money&);
    60 Money& operator+= (const Money&);
    61
    62 double getValue();
    63 void displayValue();
    64 int getValueInCents();
    65
    66 private:
    67
    68 //Data members
    -- int total; //integer representation of money
    70
    71 };
    72
    73 Money::Money();
    -- {
    -- total = 0;
    -- }
    --
    -- Money::Money(long d, int c)
    -- {
    -- int dollars = d;
    -- int cents = c;
    -- total = (dollars * 100) + cents;
    -- }
    --
    -- Money::Money(long dol)
    -- {
    -- total = dol * 100;
    -- }
    74
    -- //return data from private member
    -- double Money::getValue()
    99 {
    100 return total;
    101 }
    102
    103 void Money::displayValue()
    104 {
    105 if(total >= 0)
    106 {
    107 cout<<'$'<<(total/100);
    108 cout<<setw(2)<<setfill('0');
    109 }
    110 else
    111 {
    112 cout<<"-$"<<(total/100);
    113 cout<<setw(2)<<setfill('0');
    114 }
    115 }
    116
    117 int Money::getValueInCents()
    118 {
    119 return total;
    120 }
    121
    122 Money& Money:perator=(const Money& rightSide)
    123 {
    124 if (&rightSide != this) //using the this pointer to make sure the object is not copied over itself
    125 {
    126 this->total = rightSide.total;
    127 }
    128
    129 return *this;
    130 }
    131
    132 Money& Money:perator+=(const Money& rightSide)
    133 {
    134 this->total += rightSide.total;
    135
    136 return *this;
    137 }
    138
    139
    140 /*Money operator> ( Money first, Money second )
    141 {
    142 bool test;
    143 test = first.total > second.total;
    144 return test;
    145 }*/
    146 //Overload + relative to Money class
    147 Money operator+ ( Money f, Money e )
    148 {
    149 Money temp;
    150 temp = e.total + f.total;
    151 return temp;
    152
    153 }
    154
    155 //Overload - relative to Money class
    156 Money& operator- ( Money &a, Money b )
    157 {
    158 Money temp;
    159 temp = a.total - b.total;
    160 a = temp;
    161 return a;
    162 }
    163
    164 //Overload * for Money
    165 Money operator* ( Money c, Money d )
    166 {
    167 Money temp;
    168 temp.total = c.total * d.total;
    169 return temp;
    170 }
    171
    172 //Overload / for Money divided by double
    173 Money operator/ ( Money one, Money two)
    174 {
    175 Money temp;
    176 temp.total = one.total / two.total;
    177 return temp;
    178 }
    179
    180 Money& operator/= ( Money &c, Money d ) //passes Count object by reference
    181 {
    182 c.total /= d.total;
    183 return c;
    184 }
    185
    186 Money& operator*= ( Money &p, Money q )
    187 {
    188 p.total *= q.total;
    189 return p;
    190 }
    191
    192 Money& operator-= ( Money &u, Money y )
    193 {
    194 u.total -= y.total;
    195 return u;
    196 }
    197
    198 //Overload insertion operator to output Money object
    199 ostream& operator << (ostream& outs, Money& value)
    200 {
    201 if(value.total >= 0)
    202 {
    203 outs<<'$'<<(total/100);
    204 outs<<setw(2)<<setfill('0');
    205 }
    206 else
    207 {
    208 outs<<"-$"<<(total/100);
    209 outs<<setw(2)<<setfill('0');
    210 }
    211
    212 return outs;
    213 }
    214
    215 //Overload insertion operator to input Money objects
    216 istream& operator >> (istream& ins, Money& value)
    217 {
    218 long d=0; //holds dollar values
    219 int c=0; //holds cent values
    220 char ch; //holds '.' character
    221
    222 cin>>d;
    223 cin>>ch;
    224 cin>>c;
    225
    226 total = (d * 100) + c;
    227
    228 return ins;
    229 }
    230
    231 # endif


    *************************************************
    here is the test file.
    #include <iostream>
    #include "Money.h"


    int main()
    {
    Money m(2,27);
    Money a(-3,8), b;

    m.displayValue();

    cout << a << endl;

    cout << "Enter Money: ";
    cin >> b; //Enter -33.07
    b.displayValue();
    cout << b.getValueInCents() << endl;

    Money c;
    c = b + a;

    cout << "c is equal to " << c << endl;

    if (a == b) cout << "a equals b\n";
    else cout << "a does not equal b\n";

    a = b;

    if (a == b) cout << "a equals b\n";
    else cout << "a does not equal b\n";

    a = -c;

    a.displayValue();
    c.displayValue();

    Money x(10), y(5);

    cout << x * y << endl;
    cout << x / y << endl;
    x *= y;
    x.displayValue();

    return 0;
    }

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    OK, I have some questions.

    - What errors are you getting?

    - Why so many friend functions? I assume because " it has to be done this way"

    - Why are your logical operations returning Money objects and not true or false?

    At first glance, all of your friend functions are not specified as public or private and by default they become private. This could be a problem. Rethink some of these things and then post any errors you may still have so that it is easier for others to pinpoint the problem.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    Money class

    When I compile my money class, I get an error message for each overloaded operator saying 'long int Money::total' is private within this context. Am I not representing my money object right?

    Thanks for your help, its greatly appreciated.

    here is what i have:

    #include<iostream>

    #include<iostream>
    #include<fstream>
    #include<iomanip>

    using namespace std;

    #ifndef MONEY
    #define MONEY

    class Money
    {


    public:

    //Class constructors

    Money(); //Default Constructor
    Money(long d, int c); //Constructor
    Money(long dol); //Constructor

    Money& operator= (const Money&);
    Money& operator+= (const Money&);
    friend Money operator* (Money, Money); //Overloads * to implement this-Money * value
    friend Money operator/ (Money, Money); //Overloads / to implement this-Money / value
    friend Money operator> (Money, Money);
    friend Money operator< (Money, Money);
    friend Money operator>= (Money, Money);
    friend Money operator<= (Money, Money);
    friend Money& operator- (Money, Money);
    friend Money& operator*= (Money&, Money);
    friend Money& operator/= (Money&, Money);
    friend Money& operator-= (Money&, Money);
    friend ostream& operator<< (ostream&, Money);
    friend ostream& operator>> (ostream&, Money);

    double getValue();
    void displayValue();
    int getValueInCents();

    private:

    //Data members
    long total; //integer representation of money

    };

    Money::Money()
    {
    total = 0;
    }

    Money::Money(long d, int c)
    {

    total = d + (c/100);
    }

    Money::Money(long dol)
    {
    total = dol;
    }

    //return data from private member
    double Money::getValue()
    {
    return total;
    }

    void Money::displayValue()
    {
    if(total >= 0)
    {
    cout<<'$'<<(total/100);
    cout<<setw(2)<<setfill('0');
    }
    else
    {
    cout<<"-$"<<(total/100);
    cout<<setw(2)<<setfill('0');
    }
    }

    int Money::getValueInCents()
    {
    return total * 100;
    }

    Money& Money:perator=(const Money& rightSide)
    {
    if (&rightSide != this) //using the this pointer to make sure the object is not copied over itself
    {
    this->total = rightSide.total;
    }

    return *this;
    }

    Money& Money:perator+=(const Money& rightSide)
    {
    this->total += rightSide.total;

    return *this;
    }

    //Overload + relative to Money class
    Money operator+ ( Money f, Money e )
    {

    long temp = e.total + f.total;
    return temp;

    }

    //Overload - relative to Money class
    Money& operator- ( Money &a, Money b )
    {

    long temp = a.total - b.total;

    return temp;
    }

    //Overload * for Money
    Money operator* ( Money c, Money d )
    {

    long temp = c.total * d.total;
    return temp;
    }

    //Overload / for Money divided by double
    Money operator/ ( Money one, Money two)
    {
    long temp = one.total / two.total;
    return temp;
    }

    Money& operator/= ( Money &c, Money d ) //passes Count object by reference
    {
    c.total /= d.total;
    return c;
    }

    Money& operator*= ( Money &p, Money q )
    {
    p.total *= q.total;
    return p;
    }

    Money& operator-= ( Money &u, Money y )
    {
    u.total -= y.total;
    return u;
    }

    int operator < (Money& m, Money mm)
    {
    return m.total < mm.total;
    }
    int operator > ( Money& m, Money mm)
    {
    return m.total > mm.total;
    }
    int operator <= ( Money& m, Money mm)
    {
    return m.total <= mm.total;
    }
    int operator >= ( Money& m, Money mm)
    {
    return m.total >= mm.total;
    }

    int operator == (Money m, Money& mm)
    {
    return (mm == m);
    }

    //Overload insertion operator to output Money object
    ostream& operator << (ostream& outs, Money& value)
    {
    if(value.total >= 0)
    {
    outs<<'$'<<(total/100);
    outs<<setw(2)<<setfill('0');
    }
    else
    {
    outs<<"-$"<<(total/100);
    outs<<setw(2)<<setfill('0');
    }

    return outs;
    }

    //Overload insertion operator to input Money objects
    istream& operator >> (istream& ins, Money& value)
    {
    long d=0; //holds dollar values
    int c=0; //holds cent values
    char ch; //holds '.' character

    cin>>d;
    cin>>ch;
    cin>>c;

    total = (d * 100) + c;

    return ins;
    }

    # endif

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I've merged your two threads. Please try to keep to the same thread rather than starting a new one when continuing a discussion.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Since total is a private data member you can't reference it directly from an object of the class; you will need to use a separate accessor and mutator function.

    Example
    Code:
    void voSetTotal(const long int value);
    long int GetTotal() const;
    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    from a logic standpoint you have declared total to be of type long, which is an integer value. Then, in the 2 argument constructor you assign the value of cents (defined as type int) divided by 100 to total. Unfortunately this means that the cent portion of total is going to be dropped and total will only represent the dollar portion. You should either change total to type double or type float---or you could multiply dollars by 100 and work with total cents, rather than working with "partial" dollars. Whichever way you elect to go, be consistent throughout the program.

    As an alternative, I would consider using two variables called dollars and cents instead of one variable called total.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Money problem
    By Tesnik in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 08:12 AM
  2. How the rich get rich [Long]
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 05-05-2005, 10:36 PM
  3. Another Coin Counting Program
    By MipZhaP in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2005, 01:02 AM
  4. Dirty Money.
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 10-01-2002, 05:33 PM
  5. the value of money
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 03-23-2002, 02:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21