Simulating a basic hotdog stand in C++. I need sperate header and source files f
ID: 3907973 • Letter: S
Question
Simulating a basic hotdog stand in C++. I need sperate header and source files for each of the reqiurements. I'm really stuggling on understanding on overloading operators and friend operators. You can make your own main file to see if it works and what the output would look like. Money Class Requirements • Negative amounts of Money shall be stored by making both the dollars and cents negative • The Money class shall have four (4) constructors – A default constructor that initializes your Money object to $0.00 – A constructor that takes two integers, the first for the dollars and the second for the cents – A constructor that takes one integer, for an clean dollar amount – A constructor that takes one double, and can assign dollars and cents accordingly • int getPennies() const – Returns the equivalent amount of money as a number of pennies • bool isNegative() const – Returns true if your amount of money is negative • The following operators shall be overloaded as member operators – Unary minus (negative sign) – Prefix & Postfix increment (Increment dollars by one) – Prefix & Postfix decrement (Decrement dollars by one) • The following operators shall be overloaded as friend operators – Addition of two Money objects – Multiplication of Money and int -Multiplication of int and Money – Multiplication of Money and double -Multiplication of double and Money – Less than – Less than or equal to – Greater than – Greater than or equal to – Equality – Inequality * NOTE: All comparative operators should be comparing two Money objects – Insertion – Extraction • The following operator shall be overloaded as a non-member, non-friend operator – Subtraction between two Money objects • The Money class shall have two private data members – An integer that represents how many dollars you have. – An integer that represents how many cents you have.
Explanation / Answer
money.h
#include<iostream>
using namespace std;
class Money {
private:
int dollars;
int cents;
public:
Money();
Money(int d, int c);
Money(int d);
Money(double d);
bool isNegative();
bool isEqual( const Money &m);
void print() const;
int getPennies();
int getDollars();
int getCents();
Money operator +(Money a);
Money operator -(Money a);
Money operator *(int a);
Money operator *(double a);
bool operator ==(Money a);
bool operator >(Money a);
bool operator <(Money a);
};
money.cpp
#include "money.h"
using namespace std;
Money::Money(){
dollars = 0;
cents = 0;
}
Money::Money(int d, int c){
dollars = d;
cents = c;
}
Money::Money(int d){
dollars = d;
cents = 0;
}
Money::Money(double d){
dollars = (int)d;
cents = (d - dollars) * 100;
}
bool Money::isNegative(){
if (dollars < 0 && cents < 0){
return true;
}
else
return false;
}
bool Money::isEqual( const Money &m){
if (dollars = m.dollars && cents == m.cents)
return true;
else
return false;
}
void Money::print() const{
if (dollars < 0)
cout << "-$" << dollars << "." << cents << endl;
else
cout << "$" << dollars << "." << cents << endl;
}
int Money::getPennies(){
return dollars + cents;
}
int Money::getDollars(){
return dollars;
}
int Money::getCents(){
return cents;
}
Money Money::operator +(Money a){
Money temp;
temp.dollars = dollars + a.dollars + (cents + a.cents)/100;
temp.cents = (cents + a.cents) % 100;
return temp;
}
Money Money::operator -(Money a){
Money temp;
temp.dollars = dollars - a.dollars ;
temp.cents = (cents - a.cents) % 100;
return temp;
}
Money Money::operator *(int a){
Money temp;
temp.dollars = a * dollars;
temp.cents = cents;
return temp;
}
Money Money::operator *(double a){
Money temp;
double b = dollars * a;
int c = b;
double d = (b - c) * 100;
temp.dollars = c;
temp.cents = cents + d;
return temp;
}
bool Money::operator ==(Money a){
if (dollars == a.dollars && cents == a.cents)
return true;
else
return false;
}
bool Money::operator >(Money a){
if (getPennies() > a.getPennies())
return true;
else
return false;
}
bool Money::operator <(Money a){
if (getPennies() < a.getPennies())
return true;
else
return false;
}