Question
Converting a class to use a dynamic array The version of the class that you must implement is in words2.h. This class redefines the basic
Converting a class to use a dynamic array
The version of the class that you must implement is in words2.h.
This class redefines the basic default constructor to include a parameter to specify the containers initial capacity. As this parameter has a default value, it can be used with either zero or one argument. Your implementation will use the value of this argument to set the capacity instance variable. Also, you should use the new operator to allocate sufficient memory to store the number of strings specified by this parameter, and store a pointer to this memory in the data variable.
The new version defines a copy constructor that must make a deep copy of the data from the source Words object. This copy constructor must also insure that it sets both used and capacity to equal the corresponding values in the source, and that it copies all of the data items from the source into the new data array.
You must implement the destructor to deallocate the dynamic memory.
The other major part you must implement is the new assignment operator. This function will be invoked whenever an existing Words object appears on the left side of an assignment, and whenever a Words object is a value parameter or is returned by value from a function. Like the copy constructor, it must make a deep copy of the data from the source object, but first it should: (a) verify the source is not the same as the object on which it is invoked (just return *this if this == &source); (b) if the capacity of the source differs from the capacity of the object on which it is invoked, then you must (b.1) allocate new memory to match the source capacity, (b.2) use delete to deallocate the memory used by the existing data, and (b.3) point the data instance variable at this new memory; and finally (c) copy all of the strings from the source data into this new memory. The function must return the object *this.
The append function will have no precondition anymore - so the assertion must be removed. Instead it must check whether or not the used portion of the array is at capacity though, and if it is, then this function must work to resize the array before appending the new item. We suggest you make the new capacity equal to twice the number of current items (used * 2). This process will require many of the same steps as the assignment operator to copy the existing data to new memory and deallocate the original data array.
It should not be necessary to change the implementations of any of the other functions
Start by copying the implementation of the first version words1.cpp to a new file words2.cpp
The existing constructor must take one unsigned int argument for the initial capacity (remember not to specify a default argument value in the implementation). Set the capacity instance variable to the value of this argument, and use the new operator to allocate sufficient memory for that many strings. Point data at this new memory. Implement the copy constructor and the assignment operator as discussed above. For the destructor, just copy the following and paste it into your implementation:
Words::~Words() {
delete [] data;
}
// words1.cpp - implements class Words
// (fixed size array version)
#include
#include
#include "words1.h"
using std::string;
namespace lab03_1
{
Words::Words() {
used = 0;
capacity = 10;
}
void Words::append(string word) {
assert(used < capacity);
data[used] = word;
++used;
}
string& Words::operator[] (unsigned int index) {
assert(index < used);
return data[index];
}
unsigned int Words::size() const {
return used;
}
unsigned int Words::get_capacity() const {
return capacity;
}
string Words::operator[] (unsigned int index) const {
assert(index < used);
return data[index];
}
}
// words1.h - version 1 of class Words
#include
#ifndef WORDS1_H
#define WORDS1_H
namespace lab03_1
{
class Words
{
public:
Words();
void append(std::string word);
std::string& operator[] (unsigned int index);
unsigned int size() const;
unsigned int get_capacity() const;
std::string operator[] (unsigned int index) const;
private:
std::string data[10]; // max words is 10 in ver. 1
unsigned int used;
unsigned int capacity;
};
}
#endif
// words2.h - version 2 of class Words
#include
#ifndef WORDS2_H
#define WORDS2_H
namespace lab03_2
{
class Words
{
public:
Words(unsigned int initial_capacity = 10); // revised
Words(const Words &source); // new
~Words(); // new
void append(std::string word);
std::string& operator[] (unsigned int index);
Words& operator=(const Words &source); // new
unsigned int size() const;
unsigned int get_capacity() const;
std::string operator[] (unsigned int index) const;
private:
std::string *data; // now a pointer
unsigned int used;
unsigned int capacity;
};
}
#endif
// words2.cpp - implements class Words
// (dynamic array version)
#include
#include
#include "words2.h"
using std::string;
namespace lab03_2
{
Words::Words() {
used = 0;
capacity = 10;
}
void Words::append(string word) {
assert(used < capacity);
data[used] = word;
++used;
}
string& Words::operator[] (unsigned int index) {
assert(index < used);
return data[index];
}
unsigned int Words::size() const {
return used;
}
unsigned int Words::get_capacity() const {
return capacity;
}
string Words::operator[] (unsigned int index) const {
assert(index < used);
return data[index];
}
}
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