Question
C++ Linux Need help with lab assignment Instructions and code is posted below (main.cpp, IntegerList.h, IntegerList.cpp) IntegerList.cpp #include #include #include IntegerList.h using namespace std; IntegerList::IntegerList(int
C++ Linux
Need help with lab assignment
Instructions and code is posted below (main.cpp, IntegerList.h, IntegerList.cpp)
IntegerList.cpp
#include
#include
#include "IntegerList.h"
using namespace std;
IntegerList::IntegerList(int size)
{
list = new int[size];
numElements = size;
for (int ndx = 0; ndx
list[ndx] = 0;
}
IntegerList::~IntegerList()
{
delete [] list;
}
IntegerList::IntegerList(const IntegerList &obj)
{
numElements = obj.numElements;
list = new int[numElements];
for (int i = 0; i
list[i] = obj.list[i];
}
const IntegerList IntegerList::operator=(const IntegerList &right)
{
delete [] list;
numElements = right.numElements;
list = new int[numElements];
for (int i = 0; i
list[i] = right.list[i];
return *this;
}
bool IntegerList::isValid(int element) const
{
bool status;
if (element = numElements)
status = false;
else
status = true;
return status;
}
void IntegerList::setElement(int element, int value)
{
if (isValid(element))
list[element] = value;
else
{
cout
exit(EXIT_FAILURE);
}
}
int IntegerList::getElement(int element) const
{
if (isValid(element))
return list[element];
else
{
cout
exit(EXIT_FAILURE);
}
}
void IntegerList::displayList() const
{
for (int i = 0; i
cout
cout
}
IntegerList.h
// Specification file for the the IntegerList class.
#ifndef INTEGERLIST_H
#define INTEGERLIST_H
class IntegerList
{
private:
int *list; // Pointer to the array.
int numElements; // Number of elements.
bool isValid(int) const; // Validates subscripts.
public:
IntegerList(int); // Constructor
~IntegerList(); // Destructor
void setElement(int, int); // Sets an element to a value
int getElement(int) const; // Returns an element
void displayList() const; // Display list
IntegerList(const IntegerList &obj);
const IntegerList operator=(const IntegerList &right);
};
#endif
main.cpp
// This program demonstrates the IntegerList class.
#include
#include "IntegerList.h"
using namespace std;
int main()
{
const int SIZE = 20;
IntegerList numbers(SIZE);
for (int x = 0; x
numbers.setElement(x, x);
numbers.displayList();
IntegerList numbers2 = numbers; // Copy Constructor
numbers2.displayList();
numbers.setElement(5, 24);
numbers.setElement(7, -4);
numbers.setElement(12, 12345);
numbers.displayList();
numbers2.displayList();
cout
IntegerList numbers3(4);
numbers3.displayList();
numbers3 = numbers; // Assignment
numbers3.displayList();
numbers.setElement(5, -17);
numbers3.displayList();
numbers.displayList();
cout
numbers = numbers2 = numbers3;
numbers.displayList();
numbers2.displayList();
numbers3.displayList();
cout
numbers.setElement(0, 12345);
numbers2.setElement(1, 12345);
numbers3.setElement(2, 12345);
numbers.displayList();
numbers2.displayList();
numbers3.displayList();
return 0;
}
1. This exercise is to update the IntegerList class we went over in lecture. Again you can get the source code of folder Ch14_IntegerList4 from Chapter14 at MyClasses. This contains the basic functions as well as the copy constructor and overloaded assignment operator. What you will be creating is something that is called a safe array, one that will handle access violations and is resizeable. Add in the following functions and overloads. const IntegerList operator+(const IntegerList \&right); int \&operator [] (const int \&) ; void resize(int); Here are descriptions of the functionality of these new methods. operator + The overload of the + operator is to concatenate the two lists. So if 11 , 12 , and 13 are lists, the command 13=11+12; will assign 13 the combined list of 11 and 12. operator[ ] This is already implemented but you will make some changes. This method will simply return the element in the list at the position that comes in as the parameter. Since we want to handle access violations, if the position is less than 0 return the element at position 0, if the position is larger than the array size minus 1 , return the element at the position array size minus 1. resize(int) Resizes the array and copy the contents of the array. You need use new operator to dynamically allocate memory for a new array, and use delete to release the old array memory. If the new array size is smaller the data at the end of the old array will be lost. If the new array size is larger then the extra entries are to be set to 0 . Change the functions getElement and setElement so that they do not exit the program. Specifically, for getElement, if the position is less than 0 return the element at position 0 , if the position is larger than the array size minus 1 , return the element at the position array size minus 1 . For, setElement, if the array position is valid then set the element and if the array position is invalid do nothing. Use the same makefile to compile the class and testing file attached. Compile and run the testing file, your output should look like the printout belowStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored 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