These are C Computer Science II programming problem Please

These are C++ ( Computer Science II ) programming problem. Please provide the detailed answer the following problems.

class Money {public: Money(); Money(int dollars, int cents); void Output(); Money operator- (const Money & amount2); private: int m_dollars; int m_cents;}; Write the implementation of overloaded subtraction operation for Money class

Solution

#include<bits/stdc++.h>
using namespace std;

class Money
{
public:
   Money()//Constructor
   {
       m_dollars=0;
       m_cents=0;
   }
   Money(int d,int c)// Parameter Constructor
   {
       m_dollars=d;
       m_cents=c;
   }
   void Output()
   {
       cout<<m_dollars<<\" dollars \"<<m_cents<<\" cents\"<<endl;
   }
   Money operator-(const Money& amount2)//Overload - operator
   {
       Money temp;
       temp.m_dollars=m_dollars-amount2.m_dollars;
       temp.m_cents=m_cents-amount2.m_cents;
       return temp;
   }

   private :
   int m_cents;
   int m_dollars;  
};


int main(int argc, char const *argv[])
{
   /* code */
   Money m1(100,20);
   Money m2(10,20);
   Money m3=m1-m2; //subtract objects
   m3.Output();
   return 0;
}

=================================

Output:

90 dollars 0 cents

These are C++ ( Computer Science II ) programming problem. Please provide the detailed answer the following problems. class Money {public: Money(); Money(int do

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site