Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help with this whole C++ program! I've already posted it once before :,( 1. Create a new C++ project, and inside that project a

Please help with this whole C++ program! I've already posted it once before :,(

image text in transcribedimage text in transcribedimage text in transcribed

1. Create a new C++ project, and inside that project a new String class in a file named String.h. 2. Inside the class define two private variables char *string; int size ; 3. Also inside the class define prototypes for the four functions we'll need: public: String(const char *5 = ""); String(); void print(); void change (const char *); // constructor // destructor // print the string // reassign the string 4. 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. Notice that this constructor uses two functions from the C++ Standard Library: //Constructor String:: String(const char *original) { size = strlen(original) ; string = new char[size + 1] ; strcpy(string, original) ; } 4. The change function should replace the current string or characters 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) ; } 6. The destructor should just return the character array referenced by the str instance member back to the heap. 7. The print function should simply print the String on the console. 8. 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 initialization statement. Notice that the initialization statement will use the default copy constructor, since we haven't created one yet... // Create Strings using a constructor and an initialization statement cout 4. Change the value of the first String object using the change function and print both objects again. // Use the change function on string 1 and print cout Clearly we have a problem: Without a copy constructor, the pointer in the second string object was simply pointing to the same array of characters as the pointer in 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 2. Outside the String class, implement the copy constructor: // Copy constructor which creates a new string String:: String(const String &original) { size = original.size ; string = new char[size + 1] ; strcpy(string, original.string); } 3. Run your program again and verify that it works properly (both String objects now reference different arrays of characters). This should fix the problem caused by the statement creating the second String object

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions