Question
Question 4: [40 points] the following code declares a skeleton for a String class. An object of that class, would store, as a data member,
Question 4: [40 points] the following code declares a skeleton for a String class. An object of that class, would store, as a data member, the characters, including the NULL one, of a C++ character string (c-string) as well as the length of that string. An empty string object must be represented as follows:
class String {
friend String operator+ (________________________); // concatenation operator
friend bool operator= = (_______________________); // comparison operator
friend ostream & operator<< (_______________________________); // insertion
public:
String (const char * = \0); // String default Constructor. It initializes the
// string object to null or to the characters of a c-string
String (_____________________________); // copy constructor
~String ( ); // Class destructor
String & Operator= (_____________) // overloading the assignment operator
String operator ( ) (int i , int n); // returns a string object which contains the
// c-string that begins at i and of length n.
private:
int length; // number of characters in the string (without the NULL).
char * pchars; // pointer to the array that stores the string
// characters. The array of characters is terminated
// by the NULL character.
};
Complete the declaration of the String class
Implement the default constructor
Implement the copy constructor of the class
Overload the assignment operator = as a member function of the String class
Overload the operator ( ) which takes, as shown below, an index i and a length n and returns a string object that stores c-string that starts at i and of length n.
Overload the insertion operator << as a friend function for the String class
Overload the string comparison operator = = as a friend function. That is, the comparison of two String objects should return the Boolean true, if the two objects are the same, false otherwise.
Overload the string concatenation operator + as a friend function. That is, the concatenation of two String objects should result in appending the right-hand-side string object to the left-hand-side String object and storing the result in a new string object that is returned by the operator function.
Implement an appropriate driver program that test the developed String class. Your program should test all of the implemented functions. In your testing, you may use the following string objects.
S: the empty string
S1 = {ABC}
S3 = {DEFG}
As an example of testing, the following sample code can be used. Of course, more test cases are needed and should be used.
void main ( ) {
String S;
cout << S; // prints out the following statement: This string is empty.
String S1 (ABC);
cout << S1; // prints out ABC
String S2 (S1);
cout << S2; // prints out ABC
String S3 = DEFG;
cout << (S1 + S3) << endl;
}
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