Need Help ?
Have a Question ?

(Solved): Define The String Class Inside The String Class, Define Two Private Variables Char *string ; Int Siz ...

Define the String class

  1. Inside the String class, define two private variables
  char *string ; 
  int size ;
  1. Also inside the class define prototypes for the five functions:
  public: 
     String(const char *s = "") ;    // constructor 
     ~String() ;                     // destructor 
     void print() ;                  // print the string 
     void change(const char *) ;     // reassign the string  
  1. Outside the String class, the constructor for this class should take a string, store in in the heap, and assign it to the string instance member:
  //Constructor
  String::String(const char *original) {  
     size = strlen(original) ; 
     string = new char[size + 1] ; 
     strcpy(string, original) ; 
 }
  1. The change function should replace the current string stored in this object with a new string.
  // Replace with another String object
  void String::change(const char *original) {  
     delete [] string ; 
     size = strlen(original) ; 
     string = new char[size + 1] ; 
     strcpy(string, original) ; 
  }
  1. The destructor should just return the character array in the string instance member back to the heap.
  1. The print function should simply print the string on the console.
  1. Make sure that your program compiles. Study the class and make sure that you understand how each function works.

Create main

  1. In main, create two String objects, one using a constructor and one using an assignment statement. Notice that the assignment statement will use the default copy constructor, since we haven't created one yet...
  // Create String using a constructor and assignment statement
  cout << "Create two strings, one using constructor, "
       << another an assignment statement" << endl ;
  String str1("MiraCosta College") ;       // constructor
  String str2 = str1 ;                     // assignment
  1. Print the value of both string on the console using the print function.
  1. Change the string stored in the second String object using the change function, then print both again.
 // Use the change method and print
  cout << "\nAfter changing string 2 using change()" << endl ;
  str2.change("Cal State San Marcos") ; 
  ...
  1. Change the value of the first String object using the change function and print both again.
  // Use the change method on string 1 and print
  cout << "\nAfter changing string 1 using change()" << endl ;
  str1.change("UCSD") ;
  ...

Clearly we have a problem:

Without a copy constructor, the pointer in the second string object was simply pointing to the same string as the first object. However, when the change function was used on the second, a new string was created in the heap, freeing the memory used by the second string. That meant that the first object now had a dangling pointer, since the object it was pointing to had just been deleted!

Create a copy constructor for the String class

  1. Inside the String class, define a fifth prototype function, this one for a copy constructor.
  String(const String &) ;   // copy constructor  
  1. Outside the String class, implement the copy constructor.
  // Copy constructor that creates a new String
  String::String(const String &original) { 
     size = original.size ; 
     string = new char[size + 1] ; 
     strcpy(string, original.string) ; 
  } 
  1. Run your program again and verify that it works properly. This should fix the problem caused by the assignment statement creating the second String object.

When you have finished both projects, submit your C++ programs and print screens (or snippets) showing the final output of your lab.

Expert Answer


//C++ program-1 #include #include<string.h> using namespace std; class String{ private: char *string ; int size ; public:
We have an Answer from Expert Buy This Answer $6