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

C++ A class BCheckString that is derived from the C++ Standard Library string cl

ID: 3919586 • Letter: C

Question

C++

A class BCheckString that is derived from the C++ Standard Library string class performs bounds checking and throws an exception. The BCheckString class will have two member functions:

1) A BCheckString(string s) constructor that receives a string object passed by value and passes it on to the base class constructor. It just calls the base class constructor using its constructor initialization list.

2) A overloaded operator function, char operator[ ](int k), throws an exception, a BoundsException object (i.e., StrExcept()) if its parameter, k is out bounds (i.e., negative or greater than or equal to the length of the string), otherwise it will return the character at position k in the string with calling operator [] in the string base class.

main function that attempts to access characters that are within and outside the bounds of a suitably initialized BCheckString objects

Explanation / Answer

Screenshot

-----------------------------------------------------

Program

//Header files for I/O and string operation
#include <iostream>
#include <string>
using namespace std;
//String exception generation class
class StrExcept {
public:
   //Default constructor generate exception message
   StrExcept()
   {
      cout << "Out of range." << endl;
   }
};
//String check class
class BCheckString
{
   //Member variables for size of string and pointer for the string
   int size;
   char *ch;
public:
   //Constructor set a string value and size and pointer
   BCheckString(string str) {
       size = str.length();
       ch = new char[size];
       for (int i = 0; i < size; i++) {
           ch[i] = str.at(i);
       }
   }
   //Overload operator'[]'
   char operator[ ](int k) {
       //If k value(index) greater than range call exception
       if (k < 0 || k >= size) {
           StrExcept s;
           return -1;
       }
       //Otherwise return character at that index
       else {
           return ch[k];
       }
   }
};
//Test method
int main()
{
   //Explain program to user
   cout << "This program demonstrates bounds checking on string object.";
   //Get string from user and create boundCheck string object
   cout << " Enter a string: ";
   string str;
   getline(cin, str);
   BCheckString h(str);
   //Let user access characters at specified positions in the string
   //COMPLETE
   cout << "Legitimate string positions are: " << 0 << ".." << str.length() - 1 << endl;
   for (int k = 1; k <= 5; k++)
   {
       cout << "Enter an integer describing a position inside or outside the string: ";
       int pos;
       cin >> pos;
       if (h[pos] != -1) {
           cout << "The character at position " << pos << " is " << h[pos] << endl;
       }
      
   }
return 0;
}

-----------------------------------------------------

Output

This program demonstrates bounds checking on string object.
Enter a string: Hello world
Legitimate string positions are: 0..10
Enter an integer describing a position inside or outside the string: 1
The character at position 1 is e
Enter an integer describing a position inside or outside the string: 4
The character at position 4 is o
Enter an integer describing a position inside or outside the string: 11
Out of range.
Enter an integer describing a position inside or outside the string: 12
Out of range.
Enter an integer describing a position inside or outside the string: 8
The character at position 8 is r
Press any key to continue . . .