Question: This is an exercise about C++ programming, please add some explanations with answer,Thanks a lot. The codes are as below: 1. // DynString.cpp // //
This is an exercise about C++ programming, please add some explanations with answer,Thanks a lot.
The codes are as below:
1.
// DynString.cpp
//
// completed by:
// lab section:
#include
#include
#include "DynString.h"
DynString::DynString()
: lengthM(0), storageM(new char[1])
{
storageM[0] = '\0';
}
DynString::DynString(const char *s)
: lengthM ((int)strlen(s))
{
storageM = new char[lengthM + 1];
strcpy(storageM, s);
}
DynString::~DynString()
{
delete [] storageM;
}
int DynString::length() const
{
return lengthM;
}
const char DynString::at(int pos)const
{
assert(pos >= 0 && pos
return storageM[pos];;
}
const char * DynString::c_str() const
{
return storageM;
}
void DynString::append(const DynString& tail)
{
// Students will complete the definition of this function.
}
void DynString::truncate(int new_length)
{
assert(new_length
char *smaller_storage = new char[new_length + 1];
assert(smaller_storage != NULL);
for (int i = 0; i
smaller_storage[i] = storageM[i];
smaller_storage[new_length] = '\0';
delete [] storageM;
storageM = smaller_storage;
lengthM = new_length;
// Point one
}
2.
// DynString.h
//
#ifndef DYNSTRING_H
#define DYNSTRING_H
// WARNING
// The DynString class has a major defect. If you try to
// copy a DynString object, bad things will happen.
class DynString {
public:
DynString();
// PROMISES: Empty string object is created.
DynString(const char *s);
// PROMISES: s points to first char of a built-in string.
// REQUIRES: String object is created by copying chars from s.
~DynString();
int length() const;
// PROMISES: Return value is number of chars in string.
const char at(int pos)const;
// REQUIRES: pos >= 0 && pos
// PROMISES:
// Return value is char at position pos.
// (The first char in the string is at position 0.)
const char * c_str() const;
// PROMISES:
// Return value points to first char in built-in string
// containing the chars of the string object.
void append(const DynString& tail);
// PROMISES: chars are copied from tail to the end of the
// string object.
void truncate(int new_length);
// REQUIRES: new_length >= 0 && new_length
// PROMISES:
// Length of string is reduced to new_length.
private:
int lengthM;
char *storageM;
};
#endif
3.
// part2.cpp
#include
using namespace std;
#include "DynString.h"
int main()
{
DynString x("foot");
DynString y;
DynString z("ball");
x.truncate(3);
cout
cout
x.truncate(0);
cout
cout
#if 0
x.append("foot");
cout
cout
x.append(y);
cout
cout
x.append(z);
cout
cout
#endif
return 0;
}
The codes are as above.
Please note that there are three files above.
The definitions of the append member functions is missing from the file DynString.cpp. This function is supposed to change the length of the string, so the function requires the following approach: Allocate a new array of the right length. Copy whatever characters need to be copied into the new array. Deallocate the old array Adjust the value of the lengthM variable. Your task is to write the function's implementation. To check that it works download part2.cpp, change the #1 f 0 to #if 1 n this file, compile and run your program, and make sure the program output is as it is expected. Pay attention to the following points: .The memory management strategy for DynString says that the dynamic array should be exactly the right length to hold its contents. Make sure that your definitions of append is consistent with this strategy. The program in part2.cpp is not a very thorough test harness. It's not hard to get the correct output even if some of your code is defective. Read your function definition carefully to make sure that it properly handles dynamically allocated memory. What to Submit: Submit your DynString.cpp, and the program output
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
