Hey looking to do the following with this programFill in the
Hey, looking to do the following with this programFill in the multiplication, and division , modulus, <=, >=, -, * With division being a friend function
1.) Have Division be a friend function
2.) Have multiplication, divison, and modulus <=, >=, -, * with the program filling them.
I tried doing something along the lines of this....
friend money operator <<(const money& lhs);
friend money operator >>( const money& rhs);
but I\'m note quite sure if this is correct. Any help will be helpful. I don\'t want just answers, I want good explanations so I can understand this material.
Whole code posted below.
#include <iostream>
using namespace std;
class money
{
private:
int dollars;
int cents;
public:
money():dollars(0), cents(0){}
money(int dollars_in):dollars(dollars_in), cents(0){}
money(int dollars_in, int cents_in):dollars(dollars_in), cents(cents_in){}
int get_dollars() const;
int get_cents() const;
void set_dollars(int d_in);
void set_cents(int c_in);
money operator ++();
money operator ++(int x);
money operator +(const money& rhs);
money operator -();
friend const money& operator +(const money& lhs, const money& rhs);
money operator --();
money operator --(int x);
bool operator <=();
bool operator >=();
};
// do this as a general function
money operator *(const money& lhs, const money& rhs)
{
return money(0, 0);
}
// do this as a friend function
money operator -(const money& lhs, const money& rhs)
{
return money(0, 0);
}
money money::operator -()
{
return money(get_dollars(), get_cents());
}
money money::operator ++(int blah)
{
int tmp_d = dollars;
int tmp_c = cents;
dollars++;
return money(tmp_d, tmp_c);
}
money money::operator ++()
{
int tmp_d = dollars;
tmp_d += 1;
return money(dollars, cents);
}
int money::get_dollars() const
{
return dollars;
}
int money::get_cents() const
{
return cents;
}
void money::set_dollars(int d_in)
{
dollars = d_in;
}
void money::set_cents(int c_in)
{
cents = c_in;
}
ostream& operator <<(ostream& os, const money& rhs)
{
os << \"$\" << rhs.get_dollars() << \".\";
if(rhs.get_cents() < 10)
{
os << \"0\";
}
os << rhs.get_cents();
return os;
}
money money::operator +(const money& rhs)
{
int tmp_d = 0;
int tmp_c = 0;
int hold_c = 0;
int hold_d = 0;
tmp_d = get_dollars() + rhs.get_dollars();
tmp_c = get_cents() + rhs.get_cents();
if(tmp_c > 99)
{
hold_c = tmp_c % 100;
cout << \"hold_c = \" << hold_c << endl;
hold_d = tmp_c / 100;
cout << \"hold_d = \" << hold_d << endl;
for(int i = 0; i < hold_d; i++)
{
tmp_d++;
}
tmp_c = hold_c;
}
return money(tmp_d, tmp_c);
}
const money& operator +(const money& lhs, const money& rhs)
{
int tmp_d = 0;
int tmp_c = 0;
int hold_c = 0;
int hold_d = 0;
tmp_d = lhs.dollars + rhs.dollars;
tmp_c = lhs.cents + rhs.cents;
if(tmp_c > 99)
{
hold_c = tmp_c % 100;
cout << \"hold_c = \" << hold_c << endl;
hold_d = tmp_c / 100;
cout << \"hold_d = \" << hold_d << endl;
for(int i = 0; i < hold_d; i++)
{
tmp_d++;
}
tmp_c = hold_c;
}
return money(tmp_d, tmp_c);
}
bool operator ==(const money& lhs, const money& rhs)
{
return ((lhs.get_dollars() == rhs.get_dollars()) && (lhs.get_cents() == rhs.get_cents()));
}
int main()
{
money m1;
money m2(100, 99);
money m3(250, 5);
cout << \"m1 = \" << m1 << endl;
cout << \"m2 = \" << m2 << endl;
cout << \"m3 = \" << m3 << endl;
m1 = m2 + m3;
cout << \"m1 = \" << m1 << endl;
m1 = m1 + 200;
cout << \"m1 + 200 \" << endl;
cout << \"m1 = \" << m1 << endl;
cout << \"m2 + m3\" << endl;
cout << \"m1 = \" << m1 << endl;
cout << \"m2 == m1 \" << endl;
cout << \"m1 = \" << m1 << endl;
cout << \"m2 = \" << m2 << endl;
cout << (m2 == m1) << endl;
m1 = money();
m2 = money();
cout << \"m1 = \" << m1 << endl;
cout << \"m2 = \" << m2 << endl;
cout << (m2 == m1) << endl;
cout << \"++m1 \" << endl;
++m1;
cout << \"m1 = \" << m1 << endl;
cout << \"m1++\" << endl;
cout << m1++ << endl;
cout << \"m1 = \" << m1 << endl;
cout << \"-m1 = \" << -m1 << endl;
m2 = money(1, 0);
m3 = money(2, 0);
money tm;
tm = (m3 + -m2);
cout << \"m3 = \" << m3 << endl;
cout << \"m2 = \" << m2 << endl;
cout << \"(m3 + -m2) = \" << tm << endl;
m1 = money;
m2 = money(101, 78);
m3 = money(202, 10);
m2 <= m3?cout << \"m2 <= m3\" << endl; : cout << \"m2 is not <= m3\" << endl;
m2 >= m3?cout << \"m2 >= m3\" << endl; : cout << \"m2 is not >= m3\" << endl;
m1 = m3 * m2;
cout << \"m3 * m2 = \" << m1 << endl;
m1 = m3 - m2;
cout << \"m3 - m2 = \" << m1 << endl;
return 0;
}
Solution
#include <iostream>
using namespace std;
class money
{
private:
int dollars;
int cents;
public:
money():dollars(0), cents(0){}
money(int dollars_in):dollars(dollars_in), cents(0){}
money(int dollars_in, int cents_in):dollars(dollars_in), cents(cents_in){}
int get_dollars() const;
int get_cents() const;
void set_dollars(int d_in);
void set_cents(int c_in);
money operator ++();
money operator ++(int x);
money operator +(const money& rhs);
money operator *(const money& rhs);
friend money operator -(const money& lhs, const money& rhs);
friend money operator /(const money& lhs,const money& rhs);
money operator -();
friend const money& operator +(const money& lhs, const money& rhs);
money operator --();
money operator --(int x);
bool operator ==(const money& n);
bool operator <=(money n);
bool operator >=(money m);
};
// do this as a general function
money money::operator *(const money& rhs)
{
int d= dollars*rhs.get_dollars();
int c= cents*rhs.get_cents();
int c1 = c/100;
d=d+c1;
int c2=c%100;
return money(d, c2);
}
// friend function
money operator -(const money& lhs, const money& rhs)
{
int n = lhs.get_dollars()-rhs.get_dollars();
int c= lhs.get_cents()-rhs.get_cents();
if(c<0)
{
n--;
c=c*-1;
}
return money(n,c);
}
// friend function division
money operator /(const money& lhs, const money& rhs)
{
int n = lhs.get_dollars()/rhs.get_dollars();
int d= lhs.get_dollars()% rhs.get_dollars();
int c= lhs.get_cents()/rhs.get_cents();
c=c+d;
return money(n,c);
}
money money::operator -()
{
return money(-get_dollars(), -get_cents());
}
money money::operator ++(int blah)
{
this->dollars=this->dollars+blah;
return money(dollars,cents);
}
money money::operator ++()
{
dollars++;
return money(dollars,cents);
}
int money::get_dollars() const
{
return dollars;
}
int money::get_cents() const
{
return cents;
}
void money::set_dollars(int d_in)
{
dollars = d_in;
}
void money::set_cents(int c_in)
{
cents = c_in;
}
ostream& operator <<(ostream& os, const money& rhs)
{
os << \"$\" << rhs.get_dollars() << \".\";
if(rhs.get_cents() < 10)
{
os << \"0\";
}
os << rhs.get_cents();
return os;
}
money money::operator +(const money& rhs)
{
int tmp_d = 0;
int tmp_c = 0;
int hold_c = 0;
int hold_d = 0;
tmp_d = get_dollars() + rhs.get_dollars();
tmp_c = get_cents() + rhs.get_cents();
if(tmp_c > 99)
{
hold_c = tmp_c % 100;
cout << \"hold_c = \" << hold_c << endl;
hold_d = tmp_c / 100;
cout << \"hold_d = \" << hold_d << endl;
for(int i = 0; i < hold_d; i++)
{
tmp_d++;
}
tmp_c = hold_c;
}
return money(tmp_d, tmp_c);
}
const money& operator +(const money& lhs, const money& rhs)
{
int tmp_d = 0;
int tmp_c = 0;
int hold_c = 0;
int hold_d = 0;
tmp_d = lhs.dollars + rhs.dollars;
tmp_c = lhs.cents + rhs.cents;
if(tmp_c > 99)
{
hold_c = tmp_c % 100;
cout << \"hold_c = \" << hold_c << endl;
hold_d = tmp_c / 100;
cout << \"hold_d = \" << hold_d << endl;
for(int i = 0; i < hold_d; i++)
{
tmp_d++;
}
tmp_c = hold_c;
}
return money(tmp_d, tmp_c);
}
bool money::operator ==(const money& rhs)
{
return ((get_dollars() == rhs.get_dollars()) && (get_cents() == rhs.get_cents()));
}
bool money::operator <=(money m)
{
if(dollars< m.dollars)
return true;
else if(dollars==m.dollars)
{
if(cents < m.cents)
return true;
else if(cents > m.cents) return false;
}
else return false;
}
bool money::operator >=(money m)
{
if(dollars > m.dollars)
return true;
else if(dollars==m.dollars)
{
if(cents > m.cents)
return true;
else if(cents < m.cents) return false;
}
else return false;
}
int main()
{
money m1;
money m2(100, 99);
money m3(250, 5);
cout << \"m1 = \" << m1 << endl;
cout << \"m2 = \" << m2 << endl;
cout << \"m3 = \" << m3 << endl;
m1 = m2 + m3;
cout << \"m1 = \" << m1 << endl;
m1 = m1 + 200;
cout << \"m1 + 200 \" << endl;
cout << \"m1 = \" << m1 << endl;
cout << \"m2 + m3\" << endl;
cout << \"m1 = \" << m1 << endl;
cout << \"m2 == m1 \" << endl;
cout << \"m1 = \" << m1 << endl;
cout << \"m2 = \" << m2 << endl;
cout << (m2 == m1) << endl;
m1 = money();
m2 = money();
cout << \"m1 = \" << m1 << endl;
cout << \"m2 = \" << m2 << endl;
cout << (m2 == m1) << endl;
cout << \"++m1 \" << endl;
++m1;
cout << \"m1 = \" << m1 << endl;
cout << \"m1++\" << endl;
cout << m1++ << endl;
cout << \"m1 = \" << m1 << endl;
cout << \"-m1 = \" << -m1 << endl;
m2 = money(1, 0);
m3 = money(2, 0);
money tm;
tm = (m3 + -m2);
cout << \"m3 = \" << m3 << endl;
cout << \"m2 = \" << m2 << endl;
cout << \"(m3 + -m2) = \" << tm << endl;
m1 = money();
m2 = money(101, 78);
m3 = money(202, 10);
if(m2 <= m3)
cout << \"m2 <= m3\" << endl;
else cout << \"m2 is not <= m3\" << endl;
if(m2 >= m3)
cout << \"m2 >= m3\" << endl;
else cout << \"m2 is not >= m3\" << endl;
m1 = m3 * m2;
cout << \"m3 * m2 = \" << m1 << endl;
m1 = m3 - m2;
cout << \"m3 - m2 = \" << m1 << endl;
return 0;
}








