26.0k views
5 votes
Consider the class Money declared below. Write the member functions declared below and the definition of the overloaded +. Modify the class declaration and write the definitions that overload the stream extraction and stream insertion operators >> and << to handle Money objects like $250.99

Write a short driver main() program to test the overloaded operators +, << and >> class Money { public: friend Money operator +(const Money& amountl, const Money& amount2) Money(); // constructors Money( int dollars, int cents); // set dollars, cents double get_value() const; void printamount();// print dollars and cents like $12.25 private: int all_cents; // all amount in cents };

1 Answer

3 votes

Answer: Provided in the explanation section

Step-by-step explanation:

#include<iostream>

using namespace std;

class Money{

private:

int all_cents;

public:

Money();

double get_value() const;

Money(int,int);

void printamount();

friend Money operator+(const Money&, const Money&);

friend ostream& operator<<(ostream&, const Money&);

friend istream& operator>>(istream&, Money&);

};

Money::Money(){all_cents=0;}

double Money::get_value() const{return all_cents/100.0;}

Money::Money(int dollar ,int cents){

all_cents = dollar*100+cents;

}

void Money::printamount(){

cout<<"$"<<get_value()<<endl;

}

Money operator+(const Money& m1, const Money& m2){

int total_cents = m1.all_cents + m2.all_cents;

int dollars = total_cents/100;

total_cents %=100;

return Money(dollars,total_cents);

}

ostream& operator<<(ostream& out, const Money& m1){

out<<"$"<<m1.get_value()<<endl;

return out;

}

istream& operator>>(istream& input, Money& m1){

input>>m1.all_cents;

return input;

}

int main(){

Money m1;

cout<<"Enter total cents: ";

cin>>m1;

cout<<"Total Amount: "<<m1;

Money m2 = Money(5,60);

Money m3 = Money(4,60);

cout<<"m2 = ";m2.printamount();

cout<<"m3 = ";m3.printamount();

Money sum = m2+m3;

cout<<"sum = "<<sum;

}

cheers i hope this helped !!

Consider the class Money declared below. Write the member functions declared below-example-1
User PalFS
by
4.1k points