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

Define a class called Money. Your class will have two member variable of type in

ID: 3571533 • Letter: D

Question

Define a class called Money. Your class will have two member variable of type int to represent dollar and cent values. Include all the following member functions: a constructor to set the dollar and cent using two integers as the arguments, a constructor to set the dollar using an integer as an argument (cent=0), a default constructor (dollars=0, cent=0), an output function that outputs the money with $ sign. Embed your class definition in a test program that asks the user to enter two Money values and compare them.

Explanation / Answer

#include <iostream>
using namespace std;

class Money
{
   private:
   int dollar;
   int cent;
   public:
   Money(int dollar,int cent) //constructors
   {
       this->dollar = dollar;
       this->cent = cent;
   }
   Money(int dollar)
   {
       this->dollar = dollar;
       this->cent = 0;
   }
   Money()
   {
       this->dollar =0;
       this->cent = 0;
   }
   bool operator >(const Money &m) //relational operator overloading function
   {
       if(dollar > m.dollar && cent > m.cent)
       return true;
       else
       return false;
   }
   int getDollar() //accessor functions
   {
       return dollar;
   }
   int getCent()
   {
       return cent;
   }
   friend ostream &operator<<( ostream &output,const Money &M ) //operator overloading function for output stream
   {
output << " $" << M.dollar << "." << M.cent;
return output;
}
};
int main()
{
   int dollars,cents;
   cout<<" Enter first money value in dollars and cents";
   cin>>dollars>>cents;
  
   Money money1(dollars,cents);
   cout<<money1;
  
   cout<<" Enter second money value and dollars and cents";
   cin>>dollars>>cents;
  
   Money money2(dollars,cents);
   cout<<money2;
  
   if(money1 > money2)
   cout<<" "<<money1.getDollar()<<" dollars and "<<money1.getCent()<<" cents is greater than "<<money2.getDollar()<<" dollars and "<<money2.getCent()<<" cents";
   else
   cout<<" "<<money2.getDollar()<<" dollars and "<<money2.getCent()<<" cents is greater than "<<money1.getDollar()<<" dollars and "<<money1.getCent()<<" cents";
   return 0;
}

output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote