Question

In: Computer Science

c++ problem: Implement a class money. You must have friend functions for I/O that overload <<...

c++ problem:

Implement a class money. You must have friend functions for I/O that overload << and >>. You must have multiple constructors, You must implement a percentage method,+,-, <,>. There is a starting money class in your text.

Example output:

Enter an amount of money: $14.33

Your amount is $14.33

My amount is $10.09

One of us is richer.

You have more money than me.

10% of your money is: $$1.43

$14.33 + $10.09 equals $24.42

$14.33 - $10.09 equals $4.24

Press any key to continue . . .

Solutions

Expert Solution

Here is the code:

#include <iostream>
using namespace std;

class Money
{
    private:
        double money;
    public:
        Money(){}
        Money(double a)
        {
            money = a;
        }
        friend ostream &operator << (ostream &out, const Money &m);
        friend istream &operator >> (istream &in, Money &m);
        friend Money operator %(const Money &m1, const Money &m2);
        friend Money operator +(const Money &m1, const Money &m2);
        friend Money operator -(const Money &m1, const Money &m2);
        friend bool operator <(const Money &m1, const Money &m2);
        friend bool operator >(const Money &m1, const Money &m2);
        double getMoney() const { return money;}
};
ostream &operator <<(ostream &out, const Money &m)
{
    out << m.getMoney();
    return out;
}
istream &operator >> (istream &in, Money &m)
{
    in >> m.money;
    return in;
}
Money operator%(const Money &m1, const Money &k)
{
    return Money(m1.money*k.money/100);
}
Money operator+(const Money &m1, const Money &m2)
{
    return Money(m1.money + m2.money);
}
Money operator-(const Money &m1, const Money &m2)
{
    return Money(m1.money - m2.money);
}
bool operator<(const Money &m1, const Money &m2)
{
    if(m1.money < m2.money) return true;
    else return false;
}
bool operator>(const Money &m1, const Money &m2)
{
    if(m1.money > m2.money) return true;
    else return false;
}
int main()
{
    Money yourmoney, mymoney;
    cout << "Enter amount of your money: ";
    cin >> yourmoney;
    cout << "Enter amount of my money: ";
    cin >> mymoney;

    //Implementation of > operator
    cout<< "One of us is richer." << endl;
    Money m1(yourmoney), m2(mymoney);
    if(m1>m2)
    {
        cout<<"You have more money than me"<<endl;
    }
    else
    {
        cout<<"You have less money than me"<<endl;
    }
    //Implementation of % operator
    //k% of your money is:
    Money k(10);
    cout<< k.getMoney()<< "% of your money is $$"<<(m1%k).getMoney() <<endl;
    //implementation of addition operator
    Money sum = m1 + m2;
    cout<< "$"<<m1.getMoney()<<" + "<< "$"<< m2.getMoney() <<" equals "<<sum.getMoney()<<"$"<<endl;
    //implementation of subtraction operator
    Money sub = m1 - m2;
    cout<< "$"<<m1.getMoney()<<" - "<< "$"<< m2.getMoney() <<" equals "<<sub.getMoney()<<"$"<<endl;
    return 0;
}

//here is the output sample:

//Another sample output:


Related Solutions

C++ I will thumb it up if its accurate. Learning Objective: To implement friend functions, and...
C++ I will thumb it up if its accurate. Learning Objective: To implement friend functions, and overload operators as friend functions and member functions Instructions: Create a class called BankAccount Declare the following member variable: double balance Include the necessary constructors, accessors and mutators Be sure to use the const keyword where necessary Part 1 Overload the +, -,* and /, >=, and <= as friend Functions Apply the overloaded the following operators as friend functions: +, -, *, /,...
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED Write a C...
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED Write a C program using system call I/O to determine how many lines there are in a text file.
Write a C program using system call I/O to ****NOTE YOU MUST USE SYSTEM CALL I/O,...
Write a C program using system call I/O to ****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED a) open an existing text file passed to your program as a command-line argument, then b) display the content of the file, c) ask the user what information he/she wants to append d) receive the info from the user via keyboard e) append the info received in d) to the end of the file f) display the updated...
Please in C++ I don't have the binarySearchTree (Carrano) Implement the code of the BinarySeachTree Class...
Please in C++ I don't have the binarySearchTree (Carrano) Implement the code of the BinarySeachTree Class to solve: (Gaddis) Programming Challenger 3. Car Class p. 802 Ch. 13 • Implement a menu of options • Add a motor vehicle to the tree • Remove a motor vehicle to the tree • Search by vehicle model • Vehicle Inventory Updates • Take one of the tours to verify the contents of the tree. Car Class Write a class named Car that...
I have a quick question about this C ++ problem in my intro class. I have...
I have a quick question about this C ++ problem in my intro class. I have been able to get the first answer consistently when I test it, but my second seems to either be high or low no matter how I change it. Can you show me how you would do this problem with the setprecision(2) Paula and Danny want to plant evergreen trees along the back side of their yard. They do not want to have an excessive...
Problem: Write a C++ program that will implement and test the five functions described below that...
Problem: Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions: You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness. 1. minimum: takes an int array and the array's size as arguments. It should return the minimum value of the array elements. Do not use square brackets anywhere in the function, not even the parameter...
C++ For this assignment, you will implement the MyString class. Like the string class in C++’s...
C++ For this assignment, you will implement the MyString class. Like the string class in C++’s standard library, this class uses C-strings as the underlying data structure. Recall that a C-string is a null-terminated array of type char. See section 8.1 in Savitch for more information about C-strings; in particular, you will find it helpful to take advantage of several of the C-string functions mentioned in that section. What To Do. In the hw8 directory that is created, you will...
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class...
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class to use is probably your first step.(Please do not use collections like .push() . pop(), etc.) and instead create the implementation A templated stack class that stores type T with the following public functions: - void Push(T t) - adds t to the top of the stack - T Pop() - asserts that the stack is not empty then removes and returns the item...
1. Define a class counterType to implement a counter. Your class must have a private data...
1. Define a class counterType to implement a counter. Your class must have a private data member counter of type int and functions to set counter to the value specified by the user, initialize counter to 0, retrieve the value of counter, and increment and decrement counter by one. The value of counter must be nonnegative. 2. Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and year of publication. Design a class bookType that...
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED THIS MEANS THAT...
****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED THIS MEANS THAT YOU CANT USE fopen, fclose , etc *******You are ONLY ALLOWED TO USE SYSTEM CALL I/O such as read, write, open and close (System I/O functions) Write a C program using system call I/O to determine how many lines there are in a text file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT