The codes are as below: 1. // DynString.cpp // // completed by: // lab section:
ID: 3903302 • Letter: T
Question
The codes are as below:
1.
// DynString.cpp
//
// completed by:
// lab section:
#include <assert.h>
#include <string.h>
#include "DynString.h"
DynString::DynString()
: lengthM(0), storageM(new char[1])
{
storageM[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 < length());
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 <= length());
char *smaller_storage = new char[new_length + 1];
assert(smaller_storage != NULL);
for (int i = 0; i < new_length; i++)
smaller_storage[i] = storageM[i];
smaller_storage[new_length] = '';
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 < length()
// 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 <= length()
// PROMISES:
// Length of string is reduced to new_length.
private:
int lengthM;
char *storageM;
};
#endif
3.
// part2.cpp
#include <iostream>
using namespace std;
#include "DynString.h"
int main()
{
DynString x("foot");
DynString y;
DynString z("ball");
x.truncate(3);
cout << "Contents of x: "" << x.c_str() << "" (expected "foo")." << endl;
cout << "Length of x: " << x.length() << " (expected 3). " << endl;
x.truncate(0);
cout << "Contents of x: "" << x.c_str() << "" (expected "")." << endl;
cout << "Length of x: " << x.length() << " (expected 0). " << endl;
#if 0
x.append("foot");
cout << "Contents of x: "" << x.c_str() << "" (expected "foot")." << endl;
cout << "Length of x: " << x.length() << " (expected 4). " << endl;
x.append(y);
cout << "Contents of x: "" << x.c_str() << "" (expected "foot")." << endl;
cout << "Length of x: " << x.length() << " (expected 4). " << endl;
x.append(z);
cout << "Contents of x: "" << x.c_str() << "" (expected "football")." << endl;
cout << "Length of x: " << x.length() << " (expected 8). " << endl;;
#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 outputExplanation / Answer
Given below is the modified code for the question.
Please do rate the answer if it was helpful. Thank you
DynString.cpp
========
// DynString.cpp
//
// completed by:
// lab section:
#include <assert.h>
#include <string.h>
#include "DynString.h"
DynString::DynString()
: lengthM(0), storageM(new char[1])
{
storageM[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 < length());
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.
if(tail.length() == 0)
return;
lengthM = length() + tail.length();
char *newStorage = new char[lengthM + 1];
strcpy(newStorage, storageM);
strcat(newStorage, tail.storageM);
delete []storageM;
storageM = newStorage;
}
void DynString::truncate(int new_length)
{
assert(new_length <= length());
char *smaller_storage = new char[new_length + 1];
assert(smaller_storage != NULL);
for (int i = 0; i < new_length; i++)
smaller_storage[i] = storageM[i];
smaller_storage[new_length] = '';
delete [] storageM;
storageM = smaller_storage;
lengthM = new_length;
// Point one
}
part2.cpp
-=======
// part2.cpp
#include <iostream>
using namespace std;
#include "DynString.h"
int main()
{
DynString x("foot");
DynString y;
DynString z("ball");
x.truncate(3);
cout << "Contents of x: "" << x.c_str() << "" (expected "foo")." << endl;
cout << "Length of x: " << x.length() << " (expected 3). " << endl;
x.truncate(0);
cout << "Contents of x: "" << x.c_str() << "" (expected "")." << endl;
cout << "Length of x: " << x.length() << " (expected 0). " << endl;
#if 1
x.append("foot");
cout << "Contents of x: "" << x.c_str() << "" (expected "foot")." << endl;
cout << "Length of x: " << x.length() << " (expected 4). " << endl;
x.append(y);
cout << "Contents of x: "" << x.c_str() << "" (expected "foot")." << endl;
cout << "Length of x: " << x.length() << " (expected 4). " << endl;
x.append(z);
cout << "Contents of x: "" << x.c_str() << "" (expected "football")." << endl;
cout << "Length of x: " << x.length() << " (expected 8). " << endl;;
#endif
return 0;
}
output
====
Contents of x: "foo" (expected "foo").
Length of x: 3 (expected 3).
Contents of x: "" (expected "").
Length of x: 0 (expected 0).
Contents of x: "foot" (expected "foot").
Length of x: 4 (expected 4).
Contents of x: "foot" (expected "foot").
Length of x: 4 (expected 4).
Contents of x: "football" (expected "football").
Length of x: 8 (expected 8).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.