Need help with this program with overloading operators. I am having trouble gras
ID: 664004 • Letter: N
Question
Need help with this program with overloading operators. I am having trouble grasping the concept. Thanks
For this program, we're going to write a simple class representing currency, and overload some operators to go with it. This will give us a chance to see how overloaded operators allow us to produce code that's easier to write and easier to read, thus simplifying programming and documentation.
Consider the properties of currency. It has a numeric value, but it's not a number; it has a unit associated with it. It is displayed to 2 decimal places; calculations involving it should be rounded to 2 decimal places. It has a symbol associated with it—the ASCII character set has a dollar, and Unicode has symbols for other currencies. We'll want to display that when outputting a currency value and It's possible to add or subtract 2 currency values and get a new currency, but not to add or subtract a pure number from Currency. Likewise, it is possible to multiply or divide currency by a pure number, but not to multiply or divide 2 Currency values with each other.
Our main program will be a simple data-in, data-out program demonstrating correct operation of each of the operators we overload. To deal with the limited-decimal place problem, we'll store the currency internally as an integer, representing the currency in cents. This does limit the range of possible values we can represent (to about +/- $21,400,000.00), but that will be enough to demonstrate correct functioning. (If we wanted to express a wider range, we could use the 64-bit integer type long long; other implementation details would be unchanged.) Obviously, we'll want to display the currency as a decimal amount.
Write a Currency class. It should have the following public methods or friend functions: • A constructor that takes an amount (a double, default 0) and a character for the currency's symbol (default '$'). The double amount should be taken as a regular currency (decimal) amount; the constructor should multiply it by 100 and round to the nearest integer to get the amount to be stored. • getAmount() and setAmount(double NewAmount). The getter (a void function) returns a double, the setter takes a double. Currency can be positive or negative. • A getSymbol() and setSymbol(char NewSymbol) method. • Overloaded input and output stream operators. These will be friend functions. The output operator should print the currency symbol, immediately followed by the amount. If the currency amount is negative, the minus sign appears after the symbol (so a negative $2.38 would print as $-2.38). The input operator should read currency in the same format. If the input currency symbol is different than the currency's current symbol (say, ¥), the symbol should be updated to reflect what was read in. • Overloaded operators for + and -. These can be member methods or nonmember functions. It is only possible to add or subtract two Currency objects, not a Currency and a number. Furthermore, the Currencies must be of the same type (have the same symbol). The operator can assume the symbols are the same; the main program must check this before attempting the operation. Each of these operators returns a new const Currency object. • Overloaded operators for * and /. It is possible to multiply a Currency times a number (with the number on either side of the operator) and to divide a Currency by a pure number (but NOT to divide a number by a Currency). The result of each of these operations is a new constant currency. Your main program will read an input file containing several expressions, each on a line by itself. Each will preceded by an integer code telling you what the line contains (so you'll know how to read the line): 1. A currency followed by either + or -, then another currency. If the currencies can be added or subtracted, print the result of the operation to the screen; if not, print an error message. 2. A currency followed by either * or /, then a number (use a double in your program). Print the result to the screen. 3. A number followed by * followed by a currency amount. Print the result to the screen. All input and output of Currencies should be done using your overloaded operators. Development notes: • Simply dividing the integer amount by 100.0 and displaying the result won't be adequate; trailing zeros are dropped, so 20.50 will display as $20.5. Print the amount / 100 (integer division), then print a decimal point, then the amount % 100. (This still leaves the problem of an even dollar amount, as $10.00 will display as $10.0. We'll accept that limitation for now, and come back to it when we discuss output formatting later on.) • Most of the work of this project will be in writing the class and overloading the operators; the main program itself should be pretty straightforward. • The Currency constructor can take a number; this can lead to unexpected issues in this case. For example, $8.28 + 3.14 should be impossible; but since there's a constructor that can convert a number into a currency, it would execute. To prevent this implicit type conversion, the constructor should be made explicit. This will require a specific call to the constructor ($8.28 + Currency(3.14)) to work.
Explanation / Answer
#include <iostream>
#include<math.h>
using namespace std;
class Currency {
private:
double amount;
char symbol;
public:
Currency(){
amount=0.0;
symbol=' ';
}
Currency(double amount,char symbol){
this->amount=round(amount*100);
this->symbol=symbol;
}
double getAmount() {
return amount;
}
void setAmount(double NewAmount) {
this->amount = NewAmount;
}
char getSymbol() {
return symbol;
}
void setSymbol(char NewSymbol) {
this->symbol = NewSymbol;
}
friend ostream &operator<<( ostream &output,const Currency &c )
{
if(c.amount<0){
output<<c.symbol<<c.amount;
}else{
output<<c.amount<<c.symbol;
}
return output;
}
friend istream &operator>>( istream &input, Currency &c )
{
input >> c.amount >> c.symbol;
return input;
}
Currency operator+(const Currency& c)
{
Currency currency;
if(this->symbol==c.symbol){
currency.amount = this->amount + c.amount;
}
return currency;
}
Currency operator-(const Currency& c)
{
Currency currency;
if(this->symbol==c.symbol){
currency.amount = this->amount - c.amount;
}
return currency;
}
Currency operator*(double x)
{
Currency currency;
currency.amount = this->amount*x;
return currency;
}
Currency operator/(double x)
{
Currency currency;
currency.amount = this->amount/x;
return currency;
}
};
int main(){
Currency c1;
Currency c2;
Currency c3;
Currency c4;
Currency c5;
Currency c6;
c1.setSymbol('$');
c1.setAmount(4.5);
c2.setSymbol('$');
c2.setAmount(2.5);
c3 = c1 + c2;
c4 = c1 - c2;
c5 = c1 * 4;
c6 = c2 / 5;
cout<<"c3="<<c3<<endl;
cout<<"c4="<<c4<<endl;
cout<<"c5="<<c5<<endl;
cout<<"c6="<<c6<<endl;
return 0;
}