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

C++ programming Assume you have a Money class that has two private attributes: u

ID: 3783772 • Letter: C

Question

C++ programming

Assume you have a Money class that has two private attributes:

unsigned int cent_;

unsigned int dollars_;

And the following public methods:

Money(unsigned int cents = 0, unsigned int dollars = 0);

unsigned int getCents() const;

unsigned int getDollars() const;

Overload the following operators. You can decide whether you want the operators to be functions or methods. You may NOT create new methods (other than the operator methods, if you choose to do it that way).

Just copy your functions into this word document, convert it to PDF, and upload it.

Operators to overload (3pts each):

== (assume both operands are Money objects)

+   (assume both operands are Money objects)

<<

> (assume both operands are Money objects)

Explanation / Answer

Please refer below code

1)Money_class.h

#include<iostream>

using namespace std;

class Money
{
private:
unsigned int cent_;
unsigned int dollars_;

public:
Money(unsigned int cents = 0, unsigned int dollars = 0);
unsigned int getCents() const
{
return cent_;
}
unsigned int getDollars() const
{
return dollars_;
}

};
bool operator==(const Money &m1, const Money &m2);
Money operator+(const Money &m1, const Money &m2);
ostream& operator<<(ostream &out, const Money &m1);
bool operator<(const Money &m1, const Money &m2);

2) Money_class.cpp

#include "Money_class.h"

using namespace std;
Money::Money(unsigned int cents, unsigned int dollars)
{
cent_ = cents;
dollars_ = dollars;
}
bool operator ==(const Money& m1, const Money &m2)
{
return(m1.getCents() == m2.getCents() && m1.getDollars() == m2.getDollars());
}

Money operator+(const Money& m1, const Money &m2)
{
return Money(m1.getCents() + m2.getCents(),m1.getDollars() + m2.getDollars());
}

ostream& operator<<(ostream &out, const Money &m1)
{
out<<m1.getCents()<<" "<<m1.getDollars()<<endl;
return out;
}
bool operator<(const Money &m1, const Money &m2)
{
return(m1.getCents() < m2.getCents() && m1.getDollars() < m2.getDollars());

}

3) main.cpp

#include "Money_class.h"

using namespace std;

int main()
{
Money m3;
Money m1(5,76);
Money m2(5,76);

if(m1 == m2)
cout<<"Equal"<<endl;
else
cout<<"Not Equal"<<endl;

m3 = m1 + m2;

cout<<m1;
cout<<m2;
cout<<m3;

if(m1 < m2)
cout<<"True"<<endl;
else
cout<<"False"<<endl;

return 0;
}

Please refer below output for reference after running above program

Equal
5 76
5 76
10 152
False

Process returned 0 (0x0) execution time : 0.003 s
Press any key to continue.

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