Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write the code For a class named BinaryString which contains a character array and an unsigned integer. The class stores a string in the array
Write the code For a class named BinaryString which contains a character array and an unsigned integer. The class stores a string in the array that represents (as a string} a binary number containing the digits '1' or '6' and the value that that binary number represents as an unsigned integer. The class declaration is: typedeF unsigned int ui; #deFine BYTES 8 class BinaryString { private: char digits[BYTES + 1]; ui value, public: BinaryString(const char *5); void show{ ); ui convertToValue( )5 ui operator~( ); ui changeDigitAtIndex(int idx, char d); }3 BinaryString object has the Following PUBLIC Features: a) a BinaryString may only be created by accepting a constant string 5 that will only contain the characters '1' or '6'. "his Function copies a maximum oF BYTES number oF characters From 5 and stores them in the 'digits' array starting at index 9. LF '5' contains more that BY"ES characters, then the remaining characters are ignored. LF '5' contains less than BY"ES characters, then the remaining characters In the 'digits' array are initialized to the character '9'. Do not Forget to add the null byte! "he 'value' member is set to the actual numeric value that the digits array represents in binary. For example, iF the digits array contains '6666111', then the value would contain the number 7 {i.e. 2\"2 + 2'1 + 2\"6). See the convertToValue( ) Function below For more inFormation on this (4 marks). b} The show( ) member function uses cout to display the text string:"J followed by the digit arrayJ followed by a spaceJI followed by the text ' by the value member followed by a newline. The output must use cout only and NOT printf{ ) (2 marks). 'value: J followed c} The convertToValue( } function returns an unsigned integer that represents the numeric value of the string that is being stored as if it were a real binary number! This may be accomplished by looping through the digits array and determining which indexes contain the character '1' and then adding the value of the base 2 number which that index represents {i.e. 128JI 64, 32, 16J etc.) to a running total that starts initially at 6. The function returns the value as an unsigned integer (8 marks). For example: digits string: value: "66969611" 3 "88961111" 15 "66169699" 32 d} The operator~{ ) function simply flips all the characters in the digits array from '1' to '6' or '6' to '1'. This function returns the converted numeric value of the new digits array as an unsigned integer (3 marks). e} The changeDigitAtIndex(int idx, char d) function sets the character at index 1idx' to the character 'd' but only if idx is within the boundaries of 6 to BYTES 1 inclusive. If 'idx' is outside this rangeJI then no changes occur. This function returns a 1 if the digits array is changed or 6 otherwise {3 marks}. For example, the following program: #include using namespace std; #include typedef unsigned int ui; #define BYTES 8 class BinaryString { private: char digits [BYTES + 1]; ui value public: BinaryString(const char *$ ) ; void show( ); ui convertToValue( ); ui operators( ); ui changeDigitAtIndex(int idx, char d) ; int main ( ) { //indexes 01234567 8 01234567 8 // should store 09110090 \\ 09011101 \\ BinaryString seq1("0011"), seq2 ("06011101011001061100101101") ; seq1 . show( ); seq2. show ( ) ; cout
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started