C++ #include #include class DollarAmount { public: // initialize amount from an int64_t value explicit DollarAmount(int64_t value) : amount{value} { } // add right's amount to this object's amount void add(DollarAmount right) { // can access private data of other objects of the same class amount += right.amount; } // subtract right's amount from this object's amount void subtract(DollarAmount right) { // can access private data of other objects of the same class amount -= right.amount; } // uses integer arithmetic to calculate interest amount, // then calls add with the interest amount void addInterest(int rate, int divisor) { // create DollarAmount representing the interest DollarAmount interest{ (amount * rate + divisor / 2) / divisor }; add(interest); // add interest to this object's amount } // return a string representation of a DollarAmount object std::string toString() const { std::string dollars{std::to_string(amount / 100)}; std::string cents{std::to_string(std::abs(amount % 100))}; return dollars + "." + (cents.size() == 1 ? "0" : "") + cents; } private: int64_t amount{0}; // dollar amoun
C++
#include <string>
#include <cmath>
class DollarAmount {
public:
// initialize amount from an int64_t value
explicit DollarAmount(int64_t value) : amount{value} { }
// add right's amount to this object's amount
void add(DollarAmount right) {
// can access private data of other objects of the same class
amount += right.amount;
}
// subtract right's amount from this object's amount
void subtract(DollarAmount right) {
// can access private data of other objects of the same class
amount -= right.amount;
}
// uses integer arithmetic to calculate interest amount,
// then calls add with the interest amount
void addInterest(int rate, int divisor) {
// create DollarAmount representing the interest
DollarAmount interest{
(amount * rate + divisor / 2) / divisor
};
add(interest); // add interest to this object's amount
}
// return a string representation of a DollarAmount object
std::string toString() const {
std::string dollars{std::to_string(amount / 100)};
std::string cents{std::to_string(std::abs(amount % 100))};
return dollars + "." + (cents.size() == 1 ? "0" : "") + cents;
}
private:
int64_t amount{0}; // dollar amount in pennies
};
![5.31 (DollarAmount Arithmetic) Enhance class DollarAmount
from Exercise 5.30 with a divide member function that
receives an int parameter, divides the data member amount by
that value and stores the result in the data member. Use
rounding techniques similar to the addInterest member
function. Test your new divide member function.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F660246c3-8b83-4c9b-9f9c-461d7a754b66%2F3147ebf6-b8fd-4b72-a7bb-6799688250d0%2Fpv45lht_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)