Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

example, if -489 is stored in a WholeNumber object then its value would be trans

ID: 673631 • Letter: E

Question

example, if -489 is stored in a WholeNumber object then its value would be translated into the string negative four hundred eighty nine. The class should have a single integer member variable:
int number;
The class should also have the following functionalities:
two constructors:
i. A default constructor that initializes number to 0.
ii. A constructor that accepts an integer and uses it to initialize the number. If the integer passed is greater than 1,000 then number should be initialized to 1,000. If the integer passed is less than -1,000 then number should be initialized to -1,000.

a display method that prints to the standard output the English description of the numeric value.
The following overloaded operators
i. + (addition operator): takes as an argument another WholeNumber object and adds its value to the current object’s value. The overloaded + operator should return a WholeNumber object.
ii. – (subtraction operator): takes as an argument another WholeNumber object and subtracts its value from the current object’s value. The overloaded - operator should return a WholeNumber object.
iii. ++ (only the prefix increment operator). This operator should increment the numeric value of WholeNumber object by one. The overloaded ++ operator should return the contents of the object (using this pointer) after it is incremented.
iv. -- (only the prefix decrement operator). This operator should decrement the numeric value of WholeNumber object by one. The overloaded -- operator should return the contents of the object (using this pointer) after it is decremented.
v. << operator. This operator should display both the numeric and English description of number.
Whenever one of the arithmetic operators (+, -, ++ or --) causes number to be less than -1000 or greater than 1000 then number should be set to -1000 and 1000 respectively. The class should also have the following string arrays as static class variables:
const static string lessThan20[ ] ={"zero", "one", … ,"eighteen", "nineteen"} ; const static string tens[ ] = {"twenty", "thirty", … } ;
These static variables should be helpful while translating the number. Make sure to make use of a loop in the translation process. The main( ) method should demonstrate the capabilities of the WholeNumber class. First, main should create an instance of this class and then should make sure that each member function is working properly. For example. The following code:
int main()
{
WholeNumber n1(777);
WholeNumber n2(345);
n1.display();
++n1;
--n2;
cout << n1 - n2 << endl;
return 0;
}
Will result in the following output:
seven hundred seventy seven
(434, four hundred thirty four)

Explanation / Answer

#include <iostream>
using namespace std;

class WholeNumber{
public:
   const static string lessThan20[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen" ,"eighteen", "nineteen"};
   const static string tens[] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred"};
   int number;
   WholeNumber(){
       number = 0;
   }
   WholeNumber(int n){
       number = n;
       if(number > 1000) number = 1000;
       if(number < -1000) number = -1000;
   }
   void display(){
       int temp = number;
       for(int i = 1000; i >= 10; i /= 10){
           int q = temp / i;
           temp %= i;
           if(q > 0){
               cout << lessThan20[q] << " ";
               if(i == 1000) cout << "thousand ";
               if(i == 100) cout << "hundred ";
               if(i == 10) cout << "thousand ";
           }
       }
       cout << " ";
   }
   WholeNumber operator+(const WholeNumber& w){
       WholeNumber w1;
       w1.number = this->number + w.number;
       return w1;
   }
   WholeNumber operator-(const WholeNumber& w){
       WholeNumber w1;
       w1.number = this->number - w.number;
       return w1;
   }
   WholeNumber operator++(int){
       this->number++;
       if(this->number > 1000) this->number = 1000;
       return *this;
   }
   WholeNumber operator++(int){
       this->number--;
       if(this->number < -1000) this->number = -1000;
       return *this;
   }
   friend ostream &operator<<(ostream &o, WholeNumber w){
       o << "(" << number << ", ";
       int temp = number;
       for(int i = 1000; i >= 10; i /= 10){
           int q = temp / i;
           temp %= i;
           if(q > 0){
               cout << lessThan20[q] << " ";
               if(i == 1000) cout << "thousand ";
               if(i == 100) cout << "hundred ";
               if(i == 10) cout << "thousand ";
           }
       }
       o << ") ";
       return o;
   }
};