Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I know that that only one question must be asked at a time but these questions a

ID: 3628541 • Letter: I

Question

I know that that only one question must be asked at a time but these questions are all based on the same program so i'll be happy with any help you can provide :D

This is the program:
#include <iostream>
using namespace std;
class Money
{
public:
Money(); // default constructor
Money(int r, int c); // constructor
~Money(); // destructor
int theRands() const;
int theCents() const;
Money Plus(Money m);
Money operator+ (Money & m);
bool GreaterThan(Money m);
private:
int rands;
int cents;
};
int main()
{
Money m1;
Money m2(15,90);
Money m3(5,15);
m1 = m2.Plus(m3);
cout << m1 << " + " << m2 << " gives " << m1.Plus(m2) << endl;
m1 = m2 + m3;
cout << m2 << " + " << m3 << " gives " << m1 << endl;
if (m2.GreaterThan(m1))
cout << m2 << " is greater than " << m1 << endl;
else
cout << m2 << " is less than " << m1 << endl;
return 0;
}

Questions:
(a)Give implementations of the default constructor and the second constructor, as well as the destructor.

(b) Implement the theRands and the theCents member functions. theRands returns the number of rands
and theCents returns the number of cents.

(c) Implement the Plus()member function. The Plus() member function should add a Money object to the
existing Money object, and return the sum as a Money object.

(d) Implement the overloaded operator+ that adds a Money object to the existing Money object, and returns
the sum as a Money object.

(e) The member function greaterThan() is used to compare two Money objects with each other. Give an
implementation for this member function.

(f)Overload the stream insertion operator as a friend function. It should use the member functions the theRands
and the theCents to write the value of the Money object to the given output stream.

Explanation / Answer

#include <iostream>

using namespace std;

class Money
{
      public:
            Money();    // default constructor
            Money(int r, int c);    // constructor
            ~Money();   // destructor
            int theRands() const;
            int theCents() const;
            Money Plus(Money m);
            Money operator+ (Money &m);
            bool GreaterThan (Money m);
            friend ostream& operator << (ostream &outs, Money &m);
      private:
            int rands;
            int cents;
};

// definition
Money :: Money()
{
      rands = 0;
      cents = 0;
}

Money :: Money(int r, int c)
{
      rands = r;
      cents = c;
}

Money :: ~Money()
{
      // it doesn't have anything to do
      // because no memory is allocated
}

int Money :: theRands() const
{
      return rands;
}

int Money :: theCents() const
{
      return cents;
}

Money Money :: Plus(Money m)
{
      return Money(theRands() + m.theRands(), theCents() + m.theCents());
}

Money Money :: operator+ (Money &m)
{
      return Money(theRands() + m.theRands(), theCents() + m.theCents());
}

bool Money :: GreaterThan (Money m)
{
      // if the rand of existing Money object > the rand of m object
      // no need to compare cents
      if (theRands() > m.theRands())
            return true;
      else if (theRands() < m.theRands())
            return false;
      else
      {
            // if rands are equal, then we need to compare the cents
            if (theCents() > m.theCents())
                  return true;
            else
                  return false;
      }
}

ostream& operator << (ostream &outs, Money &m)
{
      outs << m.theRands() << " rands " << m.theCents() << " cents";
      return outs;
}

int main()
{
      Money m1;
      Money m2(15, 90);
      Money m3(5, 15);
      m1 = m2.Plus(m3);
      cout << m1 << " + " << m2 << " gives " << m1.Plus(m2) << endl;
      m1 = m2 + m3;
      cout << m2 << " + " << m3 << " gives " << m1 << endl;
      if (m2.GreaterThan(m1))
            cout << m2 << "is greater than " << m1 << endl;
      else
            cout << m2 << "is less than " << m1 << endl;
      return 0;
}