Files that you must write and turn in (Please do not turn in other files!!):
sequence2.h: The header file for the new sequence class. Actually, you don't have to write much
of this file. If some of your member functions are implemented as inline functions, then you may
put those implementations in this file too. By the way, you might want to compare this header file
with your first sequence header file sequence1.h, the new version no longer has a CAPACITY
constant because the items are stored in a dynamic array that grows as needed. But there is a
DEFAULT_CAPACITY constant, which provides the initial size of the array for a sequence created
by the default constructor.
sequence2.cpp: The implementation file for the new sequence class. You will write all of this file,
which will have the implementations of all the sequence's member functions
sequence2.h
#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include <cstdlib> // Provides size_t
namespace main_savitch_4
{
class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef double value_type;
typedef std::size_t size_type;
static const size_type DEFAULT_CAPACITY = 30;
// CONSTRUCTORS and DESTRUCTOR
sequence(size_type initial_capacity = DEFAULT_CAPACITY);
sequence(const sequence& source);
~sequence( );
// MODIFICATION MEMBER FUNCTIONS
void resize(size_type new_capacity);
void start( );
void advance( );
void insert(const value_type& entry);
void attach(const value_type& entry);
void remove_current( );
void operator =(const sequence& source);
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const;
bool is_item( ) const;
value_type current( ) const;
void operator +=(const sequence& source);
void operator +(const sequence& source);
value_type operator[](size_type index) const;
private:
value_type* data;
size_type used;
size_type current_index;
size_type capacity;
};
}
#endif
sequence2.cpp
#include "sequence2.h"
#include <algorithm>
#include <cassert>
namespace main_savitch_4 {
// CONSTRUCTORS AND DESTRUCTOR
sequence::sequence(size_type initial_capacity) {
data = new value_type[initial_capacity];
capacity = initial_capacity;
used = 0;
current_index = 0;
}
sequence::sequence(const sequence& source) {
data = new value_type[source.capacity];
capacity = source.capacity;
used = source.used;
current_index = source.current_index;
std::copy(source.data, source.data + used, data);
}
sequence::~sequence() {
delete[] data;
}
// MODIFICATION MEMBER FUNCTIONS
void sequence::resize(size_type new_capacity) {
if (new_capacity == capacity)
return;
if (new_capacity < used)
new_capacity = used;
value_type* new_data = new value_type[new_capacity];
std::copy(data, data + used, new_data);
delete[] data;
data = new_data;
capacity = new_capacity;
}
void sequence::start() {
if (used > 0)
current_index = 0;
}
void sequence::advance() {
if (is_item())
++current_index;
}
void sequence::insert(const value_type& entry) {
if (size() == capacity)
resize(capacity * 2);
if (!is_item()) // No current item, so insert.
Trauma-Informed Leadership - Five Practical Principles
Files that you must write and turn in Please do not turn in.pdf
1. Files that you must write and turn in (Please do not turn in other files!!):
sequence2.h: The header file for the new sequence class. Actually, you don't have to write much
of this file. If some of your member functions are implemented as inline functions, then you may
put those implementations in this file too. By the way, you might want to compare this header file
with your first sequence header file sequence1.h, the new version no longer has a CAPACITY
constant because the items are stored in a dynamic array that grows as needed. But there is a
DEFAULT_CAPACITY constant, which provides the initial size of the array for a sequence created
by the default constructor.
sequence2.cpp: The implementation file for the new sequence class. You will write all of this file,
which will have the implementations of all the sequence's member functions
sequence2.h
#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include <cstdlib> // Provides size_t
namespace main_savitch_4
{
class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef double value_type;
typedef std::size_t size_type;
static const size_type DEFAULT_CAPACITY = 30;
// CONSTRUCTORS and DESTRUCTOR
sequence(size_type initial_capacity = DEFAULT_CAPACITY);
sequence(const sequence& source);
~sequence( );
// MODIFICATION MEMBER FUNCTIONS
void resize(size_type new_capacity);
void start( );
void advance( );
void insert(const value_type& entry);
void attach(const value_type& entry);
void remove_current( );
void operator =(const sequence& source);
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const;
bool is_item( ) const;
value_type current( ) const;
void operator +=(const sequence& source);
void operator +(const sequence& source);
value_type operator[](size_type index) const;
2. private:
value_type* data;
size_type used;
size_type current_index;
size_type capacity;
};
}
#endif
sequence2.cpp
#include "sequence2.h"
#include <algorithm>
#include <cassert>
namespace main_savitch_4 {
// CONSTRUCTORS AND DESTRUCTOR
sequence::sequence(size_type initial_capacity) {
data = new value_type[initial_capacity];
capacity = initial_capacity;
used = 0;
current_index = 0;
}
sequence::sequence(const sequence& source) {
data = new value_type[source.capacity];
capacity = source.capacity;
used = source.used;
current_index = source.current_index;
std::copy(source.data, source.data + used, data);
}
sequence::~sequence() {
delete[] data;
}
// MODIFICATION MEMBER FUNCTIONS
void sequence::resize(size_type new_capacity) {
if (new_capacity == capacity)
return;
if (new_capacity < used)
new_capacity = used;
value_type* new_data = new value_type[new_capacity];
std::copy(data, data + used, new_data);
delete[] data;
data = new_data;
capacity = new_capacity;
}
3. void sequence::start() {
if (used > 0)
current_index = 0;
}
void sequence::advance() {
if (is_item())
++current_index;
}
void sequence::insert(const value_type& entry) {
if (size() == capacity)
resize(capacity * 2);
if (!is_item()) // No current item, so insert at start
current_index = 0;
for (size_type i = used; i > current_index; --i)
data[i] = data[i - 1];
data[current_index] = entry;
++used;
}
void sequence::attach(const value_type& entry) {
if (size() == capacity)
resize(capacity * 2);
if (!is_item()) // No current item, so attach at end
current_index = used - 1;
++current_index;
for (size_type i = used; i > current_index; --i)
data[i] = data[i - 1];
data[current_index] = entry;
++used;
}
void sequence::remove_current() {
assert(is_item());
for (size_type i = current_index; i < used - 1; ++i)
data[i] = data[i + 1];
--used;
}
void sequence::operator=(const sequence& source) {
if (this == &source)
return;
value_type* new_data = new value_type[source.capacity];
std::copy(source.data, source.data + source.used, new_data);
delete[] data;
data = new_data;
4. capacity = source.capacity;
used = source.used;
current_index = source.current_index;
}
void sequence::operator+=(const sequence& source) {
if (size() + source.size() > capacity)
resize(size() + source.size());
std::copy(source.data, source.data + source.used, data + used);
used += source.used;
}
sequence sequence::operator+(const sequence& source) const {
sequence result(size() + source.size());
std::copy(data, data + used, result.data);
std::copy(source.data, source.data + source.used, result.data + used);
result.used = used + source.used;
return result;
}
sequence::value_type sequence::operator[](size_type index) const {
assert(index < used);
}
}
Error:
./sequence2.cpp:111:24: error: out-of-line definition of 'operator+' does not match any declaration
in 'main_savitch_4::sequence'
sequence sequence::operator+(const sequence& source) const {
^~~~~~~~
./sequence2.h:128:8: note: member declaration does not match because it is not const qualified
void operator +(const sequence& source);
^ ~
1 error generated.
make: *** [Makefile:10: main] Error 1