I need help with a C++ exercise, Here is what you need to do for the code below:
ID: 3752677 • Letter: I
Question
I need help with a C++ exercise, Here is what you need to do for the code below:
Overload the % operator such that every time you use it, it takes two objects of type AltMoney as its arguments and returns:
a) 5% of the difference between the income and expenditure, if income is larger than the expenditure
b) -2% if the the expenditure is larger than the income.
c) 0 if the expenditure is the same as income
Note that, by doing this, you are required to overload the greater than sign (>), the smaller than sign (<), and the == sign.
#include<iostream>
#include<cstdlib>
using namespace std;
class AltMoney
{
public:
AltMoney();
AltMoney(int d, int c);
friend AltMoney add(AltMoney m1, AltMoney m2);
void display_money( );
private:
int dollars;
int cents;
};
void read_money(int& d, int& c);
int main( )
{
int d, c;
AltMoney m1, m2, sum;
sum = AltMoney(0,0);
read_money(d, c);
m1 = AltMoney(d,c);
cout << "The first money is:";
m1.display_money();
read_money(d, c);
m2 = AltMoney(d,c);
cout << "The second money is:";
m2.display_money();
sum = add(m1,m2);
cout << "The sum is:";
sum.display_money();
return 0;
}
AltMoney::AltMoney()
{
}
AltMoney::AltMoney(int d, int c)
{
dollars = d;
cents = c;
}
void AltMoney::display_money()
{
cout << "$" << dollars << ".";
if(cents <= 9)
cout << "0"; //to display a 0 on the left for numbers less than 10
cout << cents << endl;
}
AltMoney add(AltMoney m1, AltMoney m2)
{
AltMoney temp;
int extra = 0;
temp.cents = m1.cents + m2.cents;
if(temp.cents >=100){
temp.cents = temp.cents - 100;
extra = 1;
}
temp.dollars = m1.dollars + m2.dollars + extra;
return temp;
}
void read_money(int& d, int& c)
{
cout << "Enter dollar ";
cin >> d;
cout << "Enter cents ";
cin >> c;
if( d < 0 || c < 0)
{
cout << "Invalid dollars and cents, negative values ";
exit(1);
}
}
Explanation / Answer
// Bold letter codes are added codes
#include<iostream>
#include<cstdlib>
using namespace std;
// Class AltMoney definition
class AltMoney
{
public:
// Prototype of member functions
AltMoney();
AltMoney(int d, int c);
friend AltMoney add(AltMoney m1, AltMoney m2);
friend AltMoney sub(AltMoney m1, AltMoney m2);
void display_money( );
friend AltMoney operator %(AltMoney m1, AltMoney m2);
friend bool operator >(AltMoney m1, AltMoney m2);
friend bool operator <(AltMoney m1, AltMoney m2);
friend bool operator ==(AltMoney m1, AltMoney m2);
private:
// Data member to store data
int dollars;
int cents;
};// End of class
// Prototype of function to read data
void read_money(int& d, int& c);
// main function definition
int main( )
{
// To store dollar and cent data entered by the user
int d, c;
// Declares the object of the class AltMoney
AltMoney m1, m2, sum, result;
// Call the parameterized constructor to initialize object sum
sum = AltMoney(0,0);
// Call the parameterized constructor to initialize object result
result = AltMoney(0,0);
// Calls the method to accept data
read_money(d, c);
// Call the parameterized constructor to initialize object m1
m1 = AltMoney(d,c);
// Calls the function to display data of object m1
cout << "The first money is:";
m1.display_money();
// Calls the method to accept data
read_money(d, c);
// Call the parameterized constructor to initialize object m2
m2 = AltMoney(d,c);
// Calls the function to display data of object m2
cout << "The second money is:";
m2.display_money();
// Calls the function to add object m1 and m2 data
// and stores the return object in sum
sum = add(m1,m2);
// Calls the function to display data of object sum
cout << " The sum is:";
sum.display_money();
// Calls the operator overloading function
// and stores the return object in result
result = m1 % m2;
// Calls the function to display data of object result
cout << " The result of m1 % m2:";
result.display_money();
return 0;
}// End of main function
// Default constructor definition
AltMoney::AltMoney()
{
}// End of default constructor constructor
// Parameterized constructor to assign parameter values to data members
AltMoney::AltMoney(int d, int c)
{
dollars = d;
cents = c;
}// End of parameterized constructor
// Function to display data
void AltMoney::display_money()
{
// Displays dollar
cout << "$" << dollars << ".";
// Checks if cents is less than or equals to 9 then display zero then display cent value
if(cents <= 9)
cout << "0"; //to display a 0 on the left for numbers less than 10
cout << cents << endl;
}// End of function
// Function to calculate difference between two object passed as parameter
// Returns the result object
AltMoney sub(AltMoney m1, AltMoney m2)
{
// Declares a temporary object
AltMoney temp;
// Assigns borrow to 0
int borrow = 0;
// Subtracts the cent of object m2 from object m1 and stores it in temporary object cent
temp.cents = m1.cents - m2.cents;
// Checks if temporary object cent is less than zero i.e., negative
if(temp.cents < 0)
{
// Subtract absolute value from the temporary object cent
temp.cents = 100 - abs(temp.cents);
// Assigns 1 to borrow
borrow = 1;
}// End of if condition
// Subtracts borrow from object m1 and then subtract m2 object dollar
temp.dollars = (m1.dollars - borrow) - m2.dollars;
// Returns the result object
return temp;
}// End of function
// Function to calculate addition of two object passed as parameter
// Returns the result object
AltMoney add(AltMoney m1, AltMoney m2)
{
// Declares a temporary object
AltMoney temp;
// Assigns carry to 0
int extra = 0;
// Adds the cent of object m1 with object m2 and stores it in temporary object cent
temp.cents = m1.cents + m2.cents;
// Checks if temporary object cent is greater than or equals to 100
if(temp.cents >= 100)
{
// Subtract 100 from the temporary object cent
temp.cents = temp.cents - 100;
// Assigns 1 to carry
extra = 1;
}// End of if condition
// Adds object m1 dollar with object m2 dollar and carry
temp.dollars = m1.dollars + m2.dollars + extra;
// Returns the result object
return temp;
}// End of function
// Function to read data
void read_money(int& d, int& c)
{
// Accept dollar and cent
cout << "Enter dollar ";
cin >> d;
cout << "Enter cents ";
cin >> c;
// Checks for negative value
if( d < 0 || c < 0)
{
cout << "Invalid dollars and cents, negative values ";
exit(1);
}// End of if condition
}// End of function
// Overloading % operator
AltMoney operator %(AltMoney m1, AltMoney m2)
{
// Declares a temporary object using parameterized constructor
AltMoney temp(0,0);
// Checks if object m1 is greater than m2
// by calling operator overloading function >
if(m1 > m2)
{
// Calls the function to subtract m2 from m1
// Stores the return object in temporary object
temp = sub(m1, m2);
// Calculate 5% of dollar
temp.dollars = (temp.dollars * 5.0) / 100.0;
// Calculate 5% of cent
temp.cents = (temp.cents * 5.0) / 100.0;
}// End of if condition
// Checks if object m1 is less than m2
// by calling operator overloading function <
else if(m1 < m2)
{
// Calculate -2% of dollar
temp.dollars = (temp.dollars * -2.0) / 100.0;
// Calculate -2% of cent
temp.cents = (temp.cents * -2.0) / 100.0;
}// End of else if
// Returns the result object
return temp;
}// End of function
// Overloading operator >
// Returns true if first parameter object is greater than the second parameter object
// Otherwise returns false
bool operator >(AltMoney m1, AltMoney m2)
{
// Checks if object m1 dollar is equals to object m2 dollar
if(m1.dollars == m2.dollars)
{
// Then checks if object m1 cent is greater than the object m2 cent return true
if(m1.cents > m2.cents)
return true;
// Otherwise returns false
else
return false;
}// End of if condition
// Otherwise checks if object m1 dollar is greater than object m2 dollar
// then return true
else if(m1.dollars > m2.dollars)
return true;
// Otherwise return false
else
return false;
}// End of function
// Overloading operator <
// Returns true if first parameter object is less than the second parameter object
// Otherwise returns false
bool operator <(AltMoney m1, AltMoney m2)
{
// Checks if object m1 dollar is equals to object m2 dollar
if(m1.dollars == m2.dollars)
{
// Then checks if object m1 cent is less than the object m2 cent return true
if(m1.cents < m2.cents)
return true;
// Otherwise returns false
else
return false;
}// End of if condition
// Otherwise checks if object m1 dollar is less than object m2 dollar
// then return true
else if(m1.dollars < m2.dollars)
return true;
// Otherwise return false
else
return false;
}// End of function
// Overloading operator ==
// Returns true if first parameter object is equals to the second parameter object
// Otherwise returns false
bool operator ==(AltMoney m1, AltMoney m2)
{
// Checks if both the dollar and cent of object m1 is equals to object m2
// Returns true
if(m1.dollars == m2.dollars && m1.cents == m2.cents)
return true;
// Otherwise return false
else
return false;
}// End of function
Sample Output:
Enter dollar
18
Enter cents
70
The first money is:$18.70
Enter dollar
12
Enter cents
89
The second money is:$12.89
The sum is:$31.59
The result of m1 % m2:$0.04
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.