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 #include #include using namespace std; using namespace teaching_project;

// Place stand-alone function in unnamed namespace. namespace { void TestPart1() { Chain a, b; // Two empty Chains are created cout << a.size() << " " << b.size() << endl; // yields 0 0 Chain d{7}; // A chain containing 7 should be created. cout << d; // Should just print [7] a.ReadChain(); // User enters a chain, for example [4: 10 30 -1 2] cout << a; // Output should be what user entered. b.ReadChain(); // Same for b. cout << b; Chain c{a}; // Calls copy constructor for c. cout << c; cout << a; a = b; // Should call the copy assignment operator for a. cout << a; Chain e = move(c); // Move constructor for d. cout << e; cout << c; a = move(e); // Move assignment operator for a. cout << a; cout << e; }

void TestPart2() { Chain a, b; a.ReadChain(); // User provides input for Chain a. cout << a; b.ReadChain(); // User provides input for Chain b. cout << b << endl; cout << a + b << endl; // Concatenates the two Chains. Chain d = a + b; cout << d; // cout << d + "hi_there"; // Adds an element to the end. cout << a[2] << endl; // Should print the 3rd element. b[1] = "b_string"; // Should change the 2nd element to b_string. cout << b; b[0] = "a_string"; cout << b; }

} // namespace

int main(int argc, char **argv) { TestPart1(); TestPart2(); return 0; }

chain.h (Code that needs editing)

#include #include

namespace teaching_project { // Place comments that provide a brief explanation of the class, // and its sample usage. template class Chain { public: // Default "big five" -- you have to alter them for your assignment. // That means that you will remove the "= default" statement. // and you will provide an implementation.

// 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!