Question: Create and test a class called Chain. A chain is just a series of items, e.g. [2 7 -1 43] is a chain containing four
Create and test a class called Chain. A chain is just a series of items, e.g. [2 7 -1 43] is a chain containing four integers. A Chain can have any size. An empty Chain has size 0. The purpose of this assignment is to have you create a Chain class from scratch without using the STL. Since Chain can have arbitrary size, you should use pointers. The private data members should be: size_t size_; Object *array_; Object is the template type parameter.
Included are the two files (chain.h and test_chain.cpp). The cpp file is already completed and only the chain.h code must be altered since it's not complete. ALL the instructions are within the header file on what to change in the comments. This must be done on a g++ compiler. Thank you
test_chain.cpp
#include
// Place stand-alone function in unnamed namespace. namespace { void TestPart1() { Chain
void TestPart2() { Chain
} // namespace
int main(int argc, char **argv) { TestPart1(); TestPart2(); return 0; }
chain.h (Code that needs editing)
#include
namespace teaching_project { // Place comments that provide a brief explanation of the class, // and its sample usage. template
// Zero-parameter constructor. Chain() = default;
// Copy-constructor. Chain(const Chain &rhs) = default;
// Copy-assignment. If you have already written // the copy-constructor and the move-constructor // you can just use: // { // Chain copy = rhs; // std::swap(*this, copy); // return *this; // } Chain& operator=(const Chain &rhs) = default;
// Move-constructor. Chain(Chain &&rhs) = default;
// Move-assignment. // Just use std::swap() for all variables. Chain& operator=(Chain &&rhs) = default;
~Chain() { // Provide destructor. }
// End of big-five.
// One parameter constructor. Chain(const Object& item) { // Write something. }
// Read a chain from standard input. void ReadChain() { // Write something. }
size_t size() const { // Write something }
// @location: an index to a location in the chain. // @returns the Object at @location. // const version. // abort() if out-of-range. const Object& operator[](size_t location) const { // Write something }
// @location: an index to a location in the range. // @returns the Object at @location. // non-const version. // abort() if out-of-range. Object& operator[](size_t location) { // Write something (will be the same as above) }
// @c1: A chain. // @c2: A second chain. // @return the concatenation of the two chains. friend Chain operator+(const Chain &c1, const Chain &c2) { // Write something. }
// Overloading the << operator. friend std::ostream &operator<<(std::ostream &out, const Chain &a_chain) { // Print the chain. return out; }
private: size_t size_; Object *array_; };
} // namespace teaching_project
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
