Define a class named PositiveInteger to represent positive i

Define a class named PositiveInteger to represent positive integer. Your class should have the

following:
1. A default constructor to initialize the data field to zero.
2. A non default constructor to initialize a specified value.
3. An accessor and a mutator function
4. A getLength( ) function to return the length of the integer.
5. A data field to store the integer value.
6. Overload the subscript operator [] such that the index i will return the digit in position i, with
i=0 being the position of least significant digit. The operator should return -1 if the index is
out of bound. For example, if num is an object of PositiveInteger and the integer represented is
123, then num[0] will return 3, num[1] will return 2, num[2] will return 1 and num[3] is out of
bound will return -1. Negative index value such as num[-1] will also return -1.

Use the main method below to test your program:
int main()
{
PositiveInteger num;
int intNum, len;
cout << \"Please enter an integer: \";
cin >> intNum;
num.setInt(intNum);
cout << \"getInt returns: \" << num.getInt() << endl;
len = num.getLength();
cout<<\"Length of integer is: \"<< len< cout << endl;
for (int i = 0; i < len; i++)
{
cout << num[i] << endl;
}
cout << endl;
cout << num[len] << endl;
cout << num[-1] << endl;
return 0;
}
Sample output 1:
Please enter an integer: 0
getInt returns: 0
Length of integer is: 1
0
Illegal index value: 1
-1
Illegal index value: -1
-1
Sample output 2:
Please enter an integer: 98765
getInt returns: 98765
Length of integer is: 5
5
6
7
8
9
Illegal index value: 5
-1
Illegal index value: -1
-1

HI,

I need help to complete this QUESTION . Please provide me an answer under C++ object oriented way. java or another programming language can not use . I need answer under C++ language. Please need as soon as possible. Please follow all the assignment instruction

Solution

#include<iostream>
#include<cmath>
using namespace std;

class PositiveInteger
{
   int IntNum;
public:
   PositiveInteger()
   {
       IntNum = 0;
   }
   void setInt(int num)
   {
       IntNum = num;
   }
   int getInt()
   {
       return IntNum;
   }
   int getLength()
   {
       int i=1,cnt=0;
       if( IntNum == 0 )
           return 1;
       while( IntNum /i > 0 )
       {
           i*=10;
           cnt++;
       }
       return cnt;
   }
   int operator[]( int n)
   {
       int len = getLength();
       static int num=IntNum;
       int rem=0;

       if( n >= len )
       {
           cout<<\"Illegal index value: \"<<n<<endl;
           return -1;
       }
       if( n < 0 )
       {
           cout<<\"Illegal index value: \"<<n<<endl;
           return -1;
       }
       rem = num %10;
       num = num / 10;
      
       return rem;
   }
};


int main()
{
   PositiveInteger num;
   int intNum, len;
   cout << \"Please enter an integer: \";
   cin >> intNum;
   num.setInt(intNum);
   cout << \"getInt returns: \" << num.getInt() << endl;
   len = num.getLength();
   cout<<\"Length of integer is: \"<< len<<endl;
   cout << endl;
   for (int i = 0; i < len; i++)
   {
       cout << num[i] << endl;
      
   }
   cout << endl;
   cout << num[len] << endl;
   cout << num[-1] << endl;
  
return 0;
  
}

Define a class named PositiveInteger to represent positive integer. Your class should have the following: 1. A default constructor to initialize the data field
Define a class named PositiveInteger to represent positive integer. Your class should have the following: 1. A default constructor to initialize the data field
Define a class named PositiveInteger to represent positive integer. Your class should have the following: 1. A default constructor to initialize the data field

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site