Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Implement the Functions in the C++ code below 1) No other headers are allowed except 2) Must have no memory leak 3) The file implements

Implement the Functions in the C++ code below

1) No other headers are allowed except

2) Must have no memory leak

3) The file implements sorted array of singned char. Assume char has range [-128,127]

4) May modified any part of the .hpp file. (Modification are not allowed for the .cpp file)

Goal: Implement copy constructor and assignment operater to provide deep-copy funtionality. Functioninsert()'s job is to take a signed char insert it in the array and make sure array is sorted in the ascending order when done.

/----------------------------------------------------------------------------------------------------------------------------

#include

class sorted_sc_array { public: sorted_sc_array() : size_(0), ptr_(nullptr) { }

~sorted_sc_array() { delete[] ptr_; }

// IMPLEMENT sorted_sc_array(const sorted_sc_array& A);

// IMPLEMENT sorted_sc_array& operator=(const sorted_sc_array& A);

// RETURNS SIZE OF THE ARRAY int size() const { return size_; }

// RETURNS RAW POINTER TO THE ACTUAL DATA, CAN BE INVOKED AT ANY TIME const signed char* data() const { return ptr_; }

// IMPLEMENT: AFTER INSERT COMPLETES THE ARRAY MUST BE IN ASCENDING ORDER void insert(signed char c);

private: int size_; // size of the array signed char* ptr_; // pointer to the array

};

/----------------------the following code if for testing only, output should be 'pass' if the code works.---this file cannot be modified----------------

/*** * File: a2.cpp // DO NOT EDIT THIS FILE !!!

// YOUR CODE MUST BE CONTAINED IN a2.hpp ONLY

#include #include "a2.hpp"

int main(int argc, char* argv[]) { sorted_sc_array A;

{ sorted_sc_array T; for (signed char c = -128; c < 127; ++c) T.insert(c);

T = T;

sorted_sc_array V = T; A = V; }

const auto first = A.data(); const auto last = first + A.size();

auto size = A.size(); bool res = std::is_sorted(first, last);

if (!res || (A.size() != 255)) std::cout << "fail"; else std::cout << "pass";

std::cout << std::endl;

return 0; } // main

Please attach a Vargrind report at the end with the code.

Thanks

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_2

Step: 3

blur-text-image_3

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

Case Studies In Business Data Bases

Authors: James Bradley

1st Edition

0030141346, 978-0030141348

More Books

Students explore these related Databases questions

Question

add a total row to the workshops datasheet

Answered: 3 weeks ago