Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming problem in C++ Included are two files which are the test_chain cpp file and the chain.h header file. The header file must be edited

Programming problem in C++

Included are two files which are the test_chain cpp file and the chain.h header file. The header file must be edited to compile with the main file. The main file is NOT to be edited and there are instructions within the comments of the header file for what needs to be edited for the program to work correctly.

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.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_;

test+chain cpp code

#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 header file

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management With Website Development Applications

Authors: Greg Riccardi

1st Edition

0201743876, 978-0201743876

More Books

Students also viewed these Databases questions