Question
I am working on a c++ problem where I try to fill in the program so it passes all the unit tests. There are requirements
I am working on a c++ problem where I try to fill in the program so it passes all the unit tests.
There are requirements that need to be met:
1. The default capacity of the vector should be 10 elements, and the default size should be 0.
2. When a push_back() is performed, the vector is increased by 5 if the vector is already full.
3. The pop_back() should change the size by -1, but should not affect the capacity.
4. The getElementAt() should ensure the subscript is within range, however, the [] operator should not.
I finish filling in the program. When I tried to run it I get this error.
Is there any way to fix this and pass all the unit tests? I put the problem and my code below
The C++ program
--main.cpp--
#include "SimpleVectorOfInts.h" #include
using namespace std;
int main() { // test default constructor SimpleVectorOfInts emptyVector; // NOTE: These asserts will fail until the default constructor is working properly. assert(emptyVector.size() == 0); assert(emptyVector.capacity() == 10); /* NOTE: You can put whatever testing code you want in this file. This file will not be used as a part of the evaluation. This assignment is evalutated by unit-tesing the SimpleVectorOfInts interface and implmentation. */ cout
---SimpleVectorOfInts.h---
#ifndef SimpleVectorOfInts_SimpleVectorOfInts_h #define SimpleVectorOfInts_SimpleVectorOfInts_h
#include
class SimpleVectorOfInts { private: int *dynamicArrayPtr; // To point to the allocated array int numberOfElements; // Number of elements in the array int arrayCapacity; // capacity of the dynamic array void memError(); // Handles memory allocation errors void subError(); // Handles subscripts out of range public: // Default constructor SimpleVectorOfInts();
// Constructor declaration SimpleVectorOfInts(int); // Copy constructor declaration SimpleVectorOfInts(const SimpleVectorOfInts &); // Destructor declaration ~SimpleVectorOfInts(); // Accessor to return the array size int size() const { return numberOfElements; } // Accesssor to return the capacity of the vector int capacity() const { return arrayCapacity; } // Accessor to return a specific element int getElementAt(int position); // Overloaded [] operator declaration int &operator[](const int &); void push_back(int); // New push_back member int pop_back(); // New pop_back member };
#endif
#include "SimpleVectorOfInts.h"
//*********************************************************** // Default Constructor for SimpleVectorOfInts class. Sets * // the default size and capacity for the vector and * // allocates memory for the the internal array. * //***********************************************************
SimpleVectorOfInts::SimpleVectorOfInts() { // put your code here }
//*********************************************************** // Constructor for SimpleVectorOfInts class. Sets the size * // of the array and allocates memory for it. * //***********************************************************
SimpleVectorOfInts::SimpleVectorOfInts(int s) { // put your code here }
//************************************************* // Copy Constructor for SimpleVectorOfInts class. * //*************************************************
SimpleVectorOfInts::SimpleVectorOfInts(const SimpleVectorOfInts &obj) { // put your code here }
//******************************************** // Destructor for SimpleVectorOfInts class. * //********************************************
SimpleVectorOfInts::~SimpleVectorOfInts() { // put your code here }
//******************************************************* // memError function. Displays an error message and * // terminates the program when memory allocation fails. * //*******************************************************
void SimpleVectorOfInts::memError() { cout
//*********************************************************** // subError function. Displays an error message and * // terminates the program when a subscript is out of range. * //***********************************************************
void SimpleVectorOfInts::subError() { cout
//******************************************************* // getElementAt function. The argument is a subscript. * // This function returns the value stored at the sub- * // cript in the array. * //*******************************************************
int SimpleVectorOfInts::getElementAt(int sub) { // put your code here return dynamicArrayPtr[sub]; }
//******************************************************* // Overloaded [] operator. The argument is a subscript. * // This function returns a reference to the element * // in the array indexed by the subscript. * //*******************************************************
int &SimpleVectorOfInts::operator[](const int &sub) { return dynamicArrayPtr[sub]; }
//*********************************************************** // The push_back function pushes its argument onto the back * // of the vector. * //***********************************************************
void SimpleVectorOfInts::push_back(int val) { // put your code here }
//*********************************************************** // The pop_back function removes the last element * // of the vector. It also returns that value. * //***********************************************************
int SimpleVectorOfInts::pop_back() { // put your code here & fix return return 0; }
---my code---
-----SimpleVectorOfInts.cpp----
#include "SimpleVectorOfInts.h"
//*********************************************************** // Default Constructor for SimpleVectorOfInts class. Sets * // the default size and capacity for the vector and * // allocates memory for the the internal array. * //***********************************************************
SimpleVectorOfInts::SimpleVectorOfInts() { // put your code here vector
//*********************************************************** // Constructor for SimpleVectorOfInts class. Sets the size * // of the array and allocates memory for it. * //***********************************************************
SimpleVectorOfInts::SimpleVectorOfInts(int s) { // put your code here vector
//************************************************* // Copy Constructor for SimpleVectorOfInts class. * //*************************************************
SimpleVectorOfInts::SimpleVectorOfInts(const SimpleVectorOfInts &obj) { // put your code here vector
}
//******************************************** // Destructor for SimpleVectorOfInts class. * //********************************************
SimpleVectorOfInts::~SimpleVectorOfInts() { // put your code here emptyVector.~vector(); }
//******************************************************* // memError function. Displays an error message and * // terminates the program when memory allocation fails. * //*******************************************************
void SimpleVectorOfInts::memError() { cout
//*********************************************************** // subError function. Displays an error message and * // terminates the program when a subscript is out of range. * //***********************************************************
void SimpleVectorOfInts::subError() { cout
//******************************************************* // getElementAt function. The argument is a subscript. * // This function returns the value stored at the sub- * // cript in the array. * //*******************************************************
int SimpleVectorOfInts::getElementAt(int sub) { // put your code here this -> dynamicArrayPtr = dynamicArrayPtr;
return dynamicArrayPtr[sub]; }
//******************************************************* // Overloaded [] operator. The argument is a subscript. * // This function returns a reference to the element * // in the array indexed by the subscript. * //*******************************************************
int &SimpleVectorOfInts::operator[](const int &sub) { return dynamicArrayPtr[sub]; }
//*********************************************************** // The push_back function pushes its argument onto the back * // of the vector. * //***********************************************************
void SimpleVectorOfInts::push_back(int val) { // put your code here for (int i = 0; i
}
//*********************************************************** // The pop_back function removes the last element * // of the vector. It also returns that value. * //***********************************************************
int SimpleVectorOfInts::pop_back() { // put your code here & fix return
emptyVector.pop_back();
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started