Thread: class design and operation overload

  1. #1
    Unregistered
    Guest

    Unhappy class design and operation overload

    I have to design a class that represents units of money. There have to be 8 methods or functions within the class. Methods and arguments should be const when it is possible, and the class data should be private. I am supposed to use either 2 integers or a single float to represent units of money. He supplied a header file and it contains this:

    cout << "Enter two money amounts as doubles: ";
    cin >> m1 >> m2;

    cout << "Money object m1 is " << m1 << endl;
    cout << "Money object m2 is " << m2 << endl << endl;

    if (m1 == m2)
    cout << m1 << " equals " << m2 << endl;


    if (m1 != m2)
    cout << m1 << " does not equal " << m2 << endl;


    if (m1 < m2)
    cout << m1 << " is less than " << m2 << endl;

    if (m1 > m2)
    cout << m1 << " is greater than " << m2 << endl;

    cout << endl;


    cout << "The sum of " << m1 << " and " << m2 << " is " << m1+m2 << endl;

    money m3 = 5.95 + m2;
    cout << "Adding $5.95 to " << m2 << " yields " << m3 << endl;

    These are the 8 functions I believe I need to include, but I am not sure.

    If anyone knows anything about this, or can help me at all, I'd really appreciate it. Thanks.

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Here is a quick break down.
    Code:
    #ifndef MONEY_HPP
    #define MONEY_HPP
    
    class Money
    {
    public:
        friend ostream &operator<<(ostream Out, const Money &myMoney);
        friend istream &operator>>(istream &In, const Money &myMoney);
        friend Money &operator+(const double &RH, const Money &LH);
    
        Money();
        virtual ~Money();
    
        bool operator==(const Money &myMoney) const;
        bool operator!=(const Money &myMoney) const;
        bool operator>(const Money &myMoney) const;
        bool operator<(const Money &myMoney) const;
        Money &operator=(const Money &myMoney);
        Money &operator+(const Money &myMoney);
    
    private:
        float ftMyMoney;
    
    };
    
    #endif        // MONEY_HPP
    You should be able to build from there.

    By the way, if you plan on doing addition with a double as the left-hand operand, then you will need at least nine overloaded functions as I have shown. I assume you have at least a basic understanding of operator overloading. If you do not understand this code then go back to the books, there are plenty of tutorials out there that you can reference.

    David
    Last edited by Strider; 12-03-2001 at 11:02 PM.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need the code of few programs in c++.plzzzzz help...plzzz
    By NAVINKR20 in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2009, 09:13 AM
  2. question about class design and overloading
    By l2u in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2007, 02:02 PM
  3. Inventory tracking of dynamically allocated items
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 07-23-2006, 05:39 PM
  4. Access to methods of another (external) class
    By praul in forum C++ Programming
    Replies: 12
    Last Post: 04-19-2006, 11:42 AM
  5. C++ Library Design
    By CornedBee in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2004, 10:40 AM