Question: C++ Part 1. Implement the big five. Add the output stream < < operator. Part 2. Overload the + and [] operators for your Points2D

C++

Part 1. Implement the big five. Add the output stream << operator.

Part 2. Overload the + and [] operators for your Points2D class. Test with the following code.

points2d.h

#ifndef CSCI

#define CSCI

#include

#include

#include

#include

#include

namespace teaching_project {

// Place comments that provide a brief explanation of this class,

// and its sample usage.

template

class Points2D {

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 for it.

// Zero-parameter constructor.

// Set size to 0.

Points2D() = default;

// Copy-constructor.

Points2D(const Points2D &rhs) = default;

// Copy-assignment. If you have already written

// the copy-constructor and the move-constructor

// you can just use:

// {

// Points2D copy = rhs;

// std::swap(*this, copy);

// return *this;

// }

Points2D& operator=(const Points2D &rhs) = default;

// Move-constructor.

Points2D(Points2D &&rhs) = default;

// Move-assignment.

// Just use std::swap() for all variables.

Points2D& operator=(Points2D &&rhs) = default;

~Points2D() = default;

// End of big-five.

// One parameter constructor.

Points2D(const std::array& item) {

// Provide code.

}

// Read a chain from standard input.

void ReadPoints2D() {

// Part of code included (without error checking).

std::string input_line;

std::getline(std::cin, input_line);

std::stringstream input_stream(input_line);

if (input_line.empty()) return;

// Read size of sequence (an integer).

int size_of_sequence;

input_stream >> size_of_sequence;

// Allocate space for sequence.

// Add code here.

Object token;

for (int i = 0 ;input_stream >> token; ++i) {

// Read coordinates.

// Fill sequence_ here.

}

}

size_t size() const {

// Code missing.

}

// @location: an index to a location in the given sequence.

// @returns the point at @location.

// const version.

// abort() if out-of-range.

const std::array& operator[](size_t location) const {

// Code missing.

}

// @c1: A sequence.

// @c2: A second sequence.

// @return their sum. If the sequences are not of the same size, append the

// result with the remaining part of the larger sequence.

friend Points2D operator+(const Points2D &c1, const Points2D &c2) {

// Code missing.

}

// Overloading the << operator.

friend std::ostream &operator<<(std::ostream &out, const Points2D &some_points2d) {

// Code missing.

}

private:

// Sequence of points.

std::array *sequence_;

// Size of the sequence.

size_t size_;

};

} // namespace teaching_project

#endif // CSCI

test_points2d.cc

// Do not change this file other than adding header files

#include

#include

#include

#include

using namespace std;

using namespace teaching_project;

// Place stand-alone function in unnamed namespace.

namespace {

void TestPart1() {

Points2D a, b; // Two empty Points2D are created.

cout << a.size() << " " << b.size() << endl; // yields 0 0.

const array a_point2d{{7, 10}};

Points2D d{a_point2d}; // A Points2D containing (7, 10) should be created.

cout << d; // Should just print (7, 10).

cout << "Enter a sequence of points (integer)" << endl;

a.ReadPoints2D(); // User enters a set of points in the form:

// 3 7 4 3 2 1 10

// 3 specifies number of points. Points are the pairs

// (7, 4) (3, 2) and (1, 10).

cout << "Output1: " << endl;

cout << a; // Output should be what user entered.

cout << "Enter a sequence of points (integer)" << endl;

b.ReadPoints2D(); // Enter another sequence.

cout << "Output2: " << endl;

cout << b;

Points2D c{a}; // Calls copy constructor for c.

cout << "After copy constructor1 c{a}: " << endl;

cout << c;

cout << a;

a = b; // Should call the copy assignment operator for a.

cout << "After assignment a = b" << endl;

cout << a;

Points2D e = move(c); // Move constructor for d.

cout << "After e = move(c) " << endl;

cout << e;

cout << c;

cout << "After a = move(e) " << endl;

a = move(e); // Move assignment operator for a.

cout << a;

cout << e;

}

void TestPart2() {

Points2D a, b;

cout << "Enter a sequence of points (double)" << endl;

a.ReadPoints2D(); // User provides input for Points2D a.

cout << a;

cout << "Enter a sequence of points (double)" << endl;

b.ReadPoints2D(); // User provides input for Points2D b.

cout << b << endl;

cout << "Result of a + b" << endl;

cout << a + b << endl;

Points2D d = a + b;

cout << "Result of d = a + b" << endl;

cout << d;

cout << "Second element in a: " << endl;

cout << a[1][0] << ", " << a[1][1] << endl; // Should print the 2nd element.

}

} // namespace

int

main(int argc, char **argv) {

TestPart1();

TestPart2();

return 0;

}

Test input

3 7 4 3 5 19 71 4 100 101 2 3 40 50 11 33 3 2.1 20.3 11.11 12.45 13.1 14.2 2 1.1 100.0 20.1 30.2

Expected output

0 0 (7, 10) Enter a sequence of points (integer)

Output1: (7, 4) (3, 5) (19, 71) Enter a sequence of points (integer)

Output2: (100, 101) (2, 3) (40, 50) (11, 33) After copy constructor1 c{a}: (7, 4) (3, 5) (19, 71) (7, 4) (3, 5) (19, 71) After assignment a = b (100, 101) (2, 3) (40, 50) (11, 33) After e = move(c) (7, 4) (3, 5) (19, 71) () After a = move(e) (7, 4) (3, 5) (19, 71) (100, 101) (2, 3) (40, 50) (11, 33) Enter a sequence of points (double)

(2.1, 20.3) (11.11, 12.45) (13.1, 14.2) Enter a sequence of points (double)

(1.1, 100) (20.1, 30.2)

Result of a + b (3.2, 120.3) (31.21, 42.65) (13.1, 14.2)

Result of d = a + b (3.2, 120.3) (31.21, 42.65) (13.1, 14.2) Second element in a: 11.11, 12.45

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!