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

Question 2: (50 points) Implement a class called WiboleNumber. This class will b

ID: 3792295 • Letter: Q

Question

Question 2: (50 points) Implement a class called WiboleNumber. This class will be used to store any integer from -1,000 to 1,000. The class also has the functionality to translate the value into an English description of the number. For example, if -489 is stored in a WhalNumber object then its value would be translated into the string ngativ four bundnd 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 nwmber should be initialized to 1,000 If the integer passed is less than -1,000 then number should be initialized to -1,000.

Explanation / Answer

// http://ideone.com/UPzW3I

#include <iostream>
using namespace std;

class WholeNumber
{
private:
int number;
   const static string lessThan20[];
   const static string tens[];
   const static string h;
   const static string t;

public:
WholeNumber() {
       number=0;
   }
WholeNumber(int i){
       if(i<=-1000)
           number=-1000;
       else if(i>1000)
           number=1000;
       else
           number=i;
   }
WholeNumber* operator ++() {
       ++number; return this;
   }
WholeNumber* operator --(){
   --number; return this;
   }
   WholeNumber operator+ (const WholeNumber& other)
   {
   return WholeNumber(number+other.number);
   }
   WholeNumber operator- (const WholeNumber& other)
   {
   return WholeNumber(number-other.number);
   }
friend ostream &operator<<(ostream &out, WholeNumber w) //output
   {
out<<"("<<w.number<<","<<w.inWords()<<")";
return out;
}
void display(){ cout << inWords() << endl;
   }
   string inWords(){
           string temp="";
           int i=number,n=0;
           if (i<0) i=i*-1;
          
           n=i/1000;
           if (n>0) temp=lessThan20[n]+" "+t +" ";
           i=i%1000;
           n=i/100;
           if (n>0) temp+=lessThan20[n]+" "+h+" ";
           i=i%100;
           if (i<=19) {
            temp+=lessThan20[i];
            return temp;
           }
           else {
               n=i/10;
               if (n>1) temp+=tens[n-2]+" ";
               i=i%10;
               temp+=lessThan20[i];
               return temp;
           }
          
   }
};

string const WholeNumber::lessThan20[]={"zero","one","two","three","four","five","six","seven","eight","nine","ten"
,"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
string const WholeNumber::tens[]={"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
string const WholeNumber::h="hundred";
string const WholeNumber::t="thousand";

int main()
{
   int in1,in2;
cout<<"Enter the two numbers :";
   cin>>in1>>in2;
   WholeNumber n1(in1);
   WholeNumber n2(in2);
n1.display();
   n2.display();
   ++n1;
   --n2;
   cout<<n1-n2<<endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote