Int and IntArray

Complete the code of the following two classes (Int and IntArray)…

 

Complete the code of the following two classes (Int and IntArray) so the intended output is produced when the program is executed.

The Class Int encapsulates a validated positive integer value with acceptable values between 0 and the max_value constant global variable. Any attempt to set the value of the class to an invalid amount should result in setting the value to -1. 

Add an attribute to the Int class to be able to hold an integer value. [1 mark]

All of the modifiers and the constructor of the class will set the value of the Int class to -1 if the provided value is not between 0 and max_value. [1 mark]

When constructed the Int class will set the value of the object using a one-argument constructor. If the argument is not provided, the value should be set to zero. [3 marks]

An object of type Int should be able to be assigned to an integer value using the assignment operator. [ 3 marks]

Casting an object of type Int should result in returning the integer value of the Int object. [3 marks]

An Int object should be printable using the cout object and insertion operator, and the result should be printing the integer value inside the object. [5 marks]

(Insertion operator overload for cout and Int)

An Int object should be readable from the console by receiving a validated integer. [10 marks]

This read should be foolproof:

If the integer is not readable (alphabetic instead of digits), the message: "Bad Int, re-enter: " should be printed and the entry should be tried again.
If the integer is valid but the value is invalid, the message: "Invalid Value, re-enter: " should be printed and the entry should be tried again. 

IntArray class is a safe array of Ints.
Create an attribute to hold a dynamic array of Ints and create another attribute to hold the size of the array (number of dynamic elements) [2 marks]

Create one-argument constructor receiving a size to dynamically allocate an array of Int object and hold it in the IntArray object. If the size is not provided or if it is less than one, then the size is set to 1 instead. [6 marks]

Create index operator overloads:
1- A modifier that returns the reference of the Int object in the array at the provided index. If the index is out of bound (goes beyond size) then the dynamic array should be resized (enlarged) to accommodate the requested index.  [10 marks]
2- A query. This constant overload will return a constant reference to the Int object in the IntArray at the provided index. If the index goes beyond size, it should loop back to the beginning. (for example, if the array has 3 elements, then element with index 3 should be the same as element with index 0) [3 marks]

Casting the IntArray to an integer should result in returning the size of the IntArray. [ 2 marks]

Memory cleanup [3 marks]

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
const int max_value = 100;
class Int {
   // the attribute: 
public:
   // the one agument constructor: 

   // the assignment operator

   // the int cast overload (type converstion overload)

};
// operator<< overaload (cout << ...)


// foolproof operator>> overlaod ( cin >> ...)


class IntArray {
   // the attributes

public:
   // one argument constructor

   // modifier index operator

   // qurey index operator

   // integer cast overlaod

   // memory cleanup

};
void prnIntArray(const IntArray& I) {
   int i = 0;
   // looping 3 times (size of I) 
   cout << "I has " << int(I) << " elements: ";
   while (i < I) {  // I will be casted to an integer by compiler
      cout << "I[" << i << "]= " << I[i++] << ", ";
   }
   // By going out of bound since I is const, 
   // operator[] should loop back instead of resizing
   cout << "I[" << i << "]: " << I[i++] << endl;
}
int main() {
   Int A = 300, B = 55;
   cout << "Invalid A has the value " << A << endl;
   cout << "Valid B has the value " << B << endl;
   IntArray I(2);
   prnIntArray(I);
   I[0] = 10;
   I[1] = 20;
   I[2] = 300;  // going out of bound to resize and also invalid Int
   prnIntArray(I);
   cout << "Enter an Int: ";
   cin >> I[2];
   prnIntArray(I);
   return 0;
}
/*
* Execution sample

Invalid A has the value -1
Valid B has the value 55
I has 2 elements: I[0]= 0, I[1]= 0, I[2]: 0
I has 3 elements: I[0]= 10, I[1]= 20, I[2]= -1, I[3]: 10
Enter an Int: abc
Bad Int, re-enter: -10
Invalid Value, re-enter: 300
Invalid Value, re-enter: 30
I has 3 elements: I[0]= 10, I[1]= 20, I[2]= 30, I[3]: 10
*/

Looking for a similar assignment? Get help from our qualified experts!

Our specialized Assignment Writers can help you with your custom paper today. 100% written from scratch

Order a Similar Paper Order a Different Paper