You will need to write one class for this assignment. A main program to test you
ID: 3806569 • Letter: Y
Question
You will need to write one class for this assignment. A main program to test your class has been provided.
The NIUString class
The NIUString class represents a string of characters. You can think of it as a "wrapper" we can put around an array of char that provides additional functionality (like the ability to assign one string to another, compare strings using the relational operators, etc.). Like the other classes we've written this semester, this class should be implemented as two separate files.
The class declaration should be placed in a header file called NIUString.h. Include header guards to prevent it from accidentally being #included more than once in the same source code file.
Data Members
The NIUString class should contain the following private data members:
a pointer to a char. I'll refer to this data member as the string array pointer. It will be used to dynamically allocate an array of char (the string array).
an unsigned integer or size_t variable used to keep track of the number of elements in the string array. I'll refer to this data member as the string capacity.
an unsigned integer or size_t variable used to store the current length of the C string stored in the string array. I'll refer to this data member as the string size. The string size must always be less than or equal to the string capacity.
In addition to the changes to the data members described above, your class declaration will need prototypes for methods described below.
Methods
The implementations of the class methods should be placed in a separate source code file called NIUString.cpp. Make sure to #include "NIUString.h" at the top of this file.
The NIUString class should have the following methods (most of which are quite small):
* NIUString::NIUString()
This "default" constructor for the NIUString class should initialize a new NIUString object to an empty string with a capacity of 0. The required logic is:
1. < >Set the string size for the new object to 0.
2. Set the string capacity for the new object to 0.
3. Set the string array pointer for the new object to the special value nullptr.
* NIUString::NIUString(const char* other)
This constructor for the NIUString class should initialize a new NIUString object to the C string other. The required logic is:
< >Set the string size for the new object to the length of the C string other.
Set the string capacity for the new object to the string size.
If the string capacity is 0, set the string array pointer for the new object to nullptr. Otherwise, use the string array pointer for the new object to allocate an array of char. The number of elements in the new string array should be equal to the string capacity.
Copy the characters of the C string other (up to, but not including the null character) into the string array.
NIUString::NIUString(const NIUString& other)
This "copy constructor" for the NIUString class should initialize a new NIUString object to the same string as the existing NIUString object other. The required logic is:
< >Set the string size for the new object to the string size of other.
Set the string capacity for the new object to the string capacity of other.
If the string capacity is 0, set the string array pointer for the new object to nullptr. Otherwise, use the string array pointer for the new object to allocate an array of char. The number of elements in the new string array should be equal to the string capacity.
Copy the contents of the string array of other into the string array of the new object. If other has a string size of 0, this loop will exit immediately.
NIUString::~NIUString()
The destructor for the NIUString class can simply call the clear() method described below.
NIUString& NIUString::operator=(const NIUString& other)
This overloaded assignment operator should assign one NIUString object (the object other) to another (the object that called the method, which is pointed to by this). The required logic is:
< >Check for self-assignment. If the address stored in the pointer this is the same as the address of the object other, then skip to the final step.
Delete the string array for the object pointed to by this.
Set the string size for the object pointed to by this to the string size of other.
Set the string capacity for the object pointed to by this to the string capacity of other.
If the string capacity is 0, set the string array pointer for the object pointed to by this to nullptr. Otherwise, use the string array pointer to allocate an array of char. The number of elements in the new string array should be equal to the string capacity.
Copy the contents of the string array of other into the string array of the object pointed to by this.
Return *this.
NIUString& NIUString::operator=(const char* other)
This overloaded assignment operator should assign a C string (the string other) to an NIUString object (the object that called the method, which is pointed to by this). The required logic is:
< >Delete the string array.
Set the string size for the object pointed to by this to the length of the C string other.
Set the string capacity for the object pointed to by this to the string size.
If the string capacity is 0, set the string array pointer for the object pointed to by this to nullptr. Otherwise, use the string array pointer to allocate an array of char. The number of elements in the new string array should be equal to the string capacity.
Copy the contents of the C string other (up to, but not including the null character) into the string array of the object pointed to by this.
Return *this.
size_t NIUString::capacity() const
This method should return the string capacity.
size_t NIUString::size() const
This method should return the string size.
bool NIUString::empty() const
This method should return true if the string size is 0. Otherwise, it should return false.
void NIUString::clear()
This method should set the string size and string capacity to 0. It should should then use the delete[] operator to delete the string array. Finally, the string array pointer should be set to nullptr.
void NIUString::reserve(size_t n)
This method modifies an object's string capacity without changing its size or the contents of the string array. The required logic is:
< >If n is less than the string size or n is equal to the current string capacity, simply return.
Set the string capacity to n.
Declare a temporary array pointer (a pointer to a char).
If the string capacity is 0, set the temporary array pointer to nullptr. Otherwise, use the temporary array pointer to allocate an array of char. The number of elements in the new temporary array should be equal to the string capacity.
Copy the contents of the string array into the temporary array.
Delete the string array.
Set the string array pointer to the temporary array pointer.
bool NIUString::operator==(const NIUString& rhs) const
This method should return true if the characters stored in the string array of the object that called the method are identical to the characters stored in the string array of the NIUString object passed in as rhs.
The logic for this method is less difficult that it might initially appear to be. The first step is to compare the string sizes of the two strings. If they are different, return false (two strings of different lengths can not be equal).
Otherwise, loop through the elements of both string arrays, starting at 0 and stopping when you reach the string size. (The size of which string doesn't matter, since you know they're the same by this point.) Compare the current element from each string array. If characters are different, return false. If the characters are the same, don't return true; do nothing and let the loop continue.
Once the loop ends and you've reached the end of both strings, return true.
bool NIUString::operator==(const char* rhs) const
This method should return true if the characters stored in the string array of the object that called the method are identical to the characters of the C string passed in as rhs (up to, but not including the null character).
The logic for this method is similar to that of the previous method.
const char& NIUString::operator[](size_t pos) const
This method should return element pos of the string array.
char& NIUString::operator[](size_t pos)
This method should return element pos of the string array.
In addition to the methods described above, you will need to write a couple of standalone functions. These functions are not (and can not be) methods. You should
Include a friend declaration for each of these functions in the NIUString class definition.
Put the definitions for these functions in NIUString.cpp.
ostream& operator<<(ostream& lhs, const NIUString& rhs)
This method should loop through the characters of the string array of the NIUString object passed in as rhs and print them one at a time using the stream object passed in as lhs.
bool operator==(const char* lhs, const NIUString& rhs)
This method should return true if the characters of the C string passed in as lhs (up to, but not including the null character) are identical to the characters stored in the string array of the NIUString object passed in as rhs.
The logic for this function is similar to that of the two relational operator methods.
Driver Program
A short main program to test your class is given below. Either type in this program or copy it to your UNIX account from the pathname ~t90kjm1/CS241/Code/Spring2017/Assign5/assign5.cpp.
const char& operator [] (size_t sub) const;
char& operator [] (size_t sub);
//const char* c_str() const;
bool empty() const;
int size() const;
void clear();
private:
//data memebers
char* StringArray; //to store string
size_t StringSize; //current size of the string
size_t StringCapacity; //maximum size
};
Explanation / Answer
#include"NIUStrinh.h";
using namespace std;
int my_strlen(const char * ptr ){
int len = 0;
char * p = ptr;
while( *p != '' ) {
len++;
p++;
}
return len;
}
void my_strncpy( char * ptr_dest, char * ptr_src, int n ){
for( int i=0; i < n; i++ )
ptr_dest[i] = ptr_src[i];
}
ostream& operator<< ( ostream& os, const NIUString& s )
{
// print char after char from buff
for( int i=0; i < s.length; i++ )
os.put( s.buff[i] );
return os; // this is to allow multiple <<, as in cout << a << b;
}
ostream& operator <<(ostream& leftOp, NIUString& rightOp)
{
leftOp << rightOp.StringArray;
return leftOp;
}
bool operator == (const char* leftOp, const NIUString & rightOp)
{
if(my_strlen(leftOp) == rightOp.StringSize)
{
short counter1 = rightOp.StringSize;
int counter2 = 0;
int i = 0;
while(i != counter1)
{
if(leftOp[i] != S2.nstring[i]){counter2++;}
i++;
}
cout << i << " " << counter2 << endl;
if(counter2 != 0) {return false;}
else{return true;}
}
else {return false;}
}
NIUString::NIUString()
{
}
NIUString::NIUString(const char* s):
{
StringSize = my_strlen(s);
StringArray = new char[StringSize];
my_strncpy( StringArray, s, StringSize );
}
NIUString::NIUString(const NIUString& other)
{
StringSize = other.StringSize;
StringArray = other.StringArray;
StringCapacity = other.StringCapacity;
}
NIUString()::~NIUString()
{
delete[] StringArray;
}
NIUString& NIUString::operator=(const NIUString& other)
{
if( this != &other ){ // guard against a = a;
delete [] StringArray; // release old memory & then
StringSize = other.StringSize; // allocate new memory
StringCapacity = other.StringCapacity;
StringArray = new char[StringSize];
my_strncpy( StringArray, other.StringArray, StringSize );
}
return *this;
}
NIUString& NIUString::operator=(const char* other)
{
StringSize = my_strlen(other);
StringArray = new char[StringSize];
my_strncpy( StringArray, other, StringSize );'
return *this;
}
size_t NIUString::capacity() const
{
return StringCapacity;
}
void NIUString::reserve(size_t n)
{
StringCapacity = n;
StringArray = new char[n];
}
bool NIUString::operator == (const NIUString& rightOp)const
{
if(this == rightOp)
return true;
else
return false;
}
bool NIUString::operator == (const char* rightOp) const
{
if(this.StringSize == rightOp.StringSize)
{
short counter1 = S1.StringSize;
int counter2 = 0;
int i = 0;
while(i != counter1)
{
if(S1.StringArray[i] != S2.StringArray[i]){counter2++;}
i++;
}
cout << i << " " << counter2 << endl;
if(counter2 != 0) {return false;}
else{return true;}
}
else {return false;}
}
const char& NIUString::operator [] (size_t sub) const
{
if( sub < 0 || sub > StringSize ) {
cerr << "Invalid index in myString::operator[]. Aborting... ";
exit(-1);
}
return StringArray[sub];
}
char& NIUString::operator [] (size_t sub)
{
if( sub < 0 || sub > StringSize ) {
cerr << "Invalid index in myString::operator[]. Aborting... ";
exit(-1);
}
return StringArray[sub];
}
//const char* c_str() const;
bool NIUString::empty() const
{
if(StringArray[0] == NULL && StringSize == 1){return true;}
else {return false;}
}
int NIUString::size() const
{
return StringSize;
}
void NIUString::clear()
{
delete[] StringArray;
StringSize =0;
StringCapacity =0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.