Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/******************************************************************/ /* IntList

ID: 3775618 • Letter: #

Question

/******************************************************************/
/* IntList (constructors destructor member function definitions) **/
/******************************************************************/
// Write brief summary for purpose and what it's doing
bool IntList::isValid(const int index) const{
    return(index>=0 && index<size());
}

// Write brief summary for purpose and what it's doing
IntList::IntList() {
    list = new int[DEFAULT_SIZE];
    capacity = DEFAULT_SIZE;
    count=0;
}

// Write brief summary for purpose and what it's doing
IntList::IntList(const IntList& source) {
    list = new int[source.capacity];
    capacity = source.capacity;
    count=source.count;
    for(int i=0; i<count; i++) {
        list[i] = source.list[i];
    }
    for(int i=count; i<capacity; i++){
        list[i]=0;
    }
}

// Write brief summary for purpose and what it's doing
IntList::IntList(const int cap) {
    list = new int[cap];
    capacity = cap;
    count=0;
    for(int i=0; i<cap; i++) {
        list[i]=0;
    }
}

// Write brief summary for purpose and what it's doing
IntList::~IntList() {
    cout << "**destructor** ~IntList ";
    if(list!=NULL) {
        delete [] list;
        list=NULL;
    }
    capacity=count=0;
}

// Write brief summary for purpose and what it's doing
IntList& IntList::operator=(const IntList &source) {
    if(this == &source){
        return(*this);
    }

    if(list!=NULL) {
        delete [] list;
        list=NULL;
    }

    list = new int[source.capacity];
    capacity = source.capacity;
    count=source.count;
    for(int i=0; i<count; i++) {
        list[i] = source.list[i];
    }
    for(int i=count; i<capacity; i++){
        list[i]=0;
    }

    return(*this);
}

// Write brief summary for purpose and what it's doing
void IntList::setElement(const int index, const int value) {
    if(isValid(index)){
        list[index]=value;
    }
    else {
        cout << "Invalid Index @ " << index << endl;
    }
}

// Write brief summary for purpose and what it's doing
const int IntList::getElement(const int index) const {
    if(isValid(index)){
        return(list[index]);
    }
    else return(-1);
}

// Write brief summary for purpose and what it's doing
void IntList::append(const int elem) {

    if(size()==capacity){  
        int* tmp = list;      
        capacity = capacity*2;
        list = new int[capacity];
      
        for(int i=0; i<size(); i++){
            list[i] = tmp[i];
        }
        delete [] tmp;        
    }
    list[size()] = elem;
    count++;
}

// Write brief summary for purpose and what it's doing
const int IntList::size() const {
    return(count);
}
     
// Write brief summary for purpose and what it's doing
void IntList::printList() const {
    cout << "list: ";
    for(int i=0; i<size(); i++) {
        cout << list[i] << " ";
    }
    cout << endl;
}

// Write brief summary for purpose and what it's doing
void IntList::debug() const {
    cout << "**Debug**: ";
    for(int i=0; i<capacity; i++) {
        cout << list[i] << " ";
    }
    cout << " count: " << size() << " cap: " << capacity << endl;
}
/******************************************************************/

Explanation / Answer

/* IntList (class definition) *************************************/
/******************************************************************/
#define DEFAULT_SIZE 10
class IntList {
private:
int *list; //pointer to array
int capacity; //capacity of array
int count; //num of elems in array
bool isValid(const int) const; //validates that index is w/in range
public:
IntList(); //constructor
IntList(const IntList&); //copy constructor
IntList(const int); //constructor to int
~IntList(); //destructor
IntList& operator=(const IntList&); // overloaded assignment operator

void setElement(const int, const int); //
const int getElement(const int) const; //
void append(const int); //
const int size() const; //
void printList() const; //
void debug() const; //
};
/******************************************************************/

/******************************************************************/
/* IntList (constructors destructor member function definitions) **/
/******************************************************************/
// As the program is to create a arrray.This function checks When a number and index is given to add that number at particularr index of arrray.This function returns true if given index is greater than 0 and less than the size of array.

bool IntList::isValid(const int index) const{
return(index>=0 && index<size());
}
// This is a constructor .This function creates pointer 'list" to point int type array of size "DEFAULT_SIZE".
//Sets capacity to "DEFAULT_SIZE".This capacity variable is created to store capacity of array.
//Sets count=0. Count here is number of elements of element in array.Which is for now is 0.
IntList::IntList() {
list = new int[DEFAULT_SIZE];
capacity = DEFAULT_SIZE;
count=0;
}
// This is a copy constructor .a copy function is used to declare and initialise an object from another object. Here we will pass another object of IntList class ,which is already created to copy its variable values to object calling this function
//This function creates pointer 'list" to point int type array of size ""of the array present in source.source is other object of IntList passes in this function .
//Sets capacity to souce object array capacity.This capacity variable is created to store capacity of array.
//Sets count to source object array count value. Count here will be set to the number of elemments present in source object arrray.
//Now if source object has elements .Each element will be copied using for loop.After copying all elements from source. If still capacity is left. Value is set to be "0" in other places

IntList::IntList(const IntList& source) {
list = new int[source.capacity];
capacity = source.capacity;
count=source.count;
for(int i=0; i<count; i++) {
list[i] = source.list[i];
}
for(int i=count; i<capacity; i++){
list[i]=0;
}
}
// This is a constructor with a parameter of contant int type.Which is capacity. Function will create a array of capacity passed in function.
//For loop is applied to set all elements of aray to '0'
//This function creates pointer 'list" to point int type array of size ""of the array present in source.source is other object of IntList passes in this function .
//Sets capacity to parameter passes in function.This capacity variable is created to store capacity of array.
///initially count is set to 0.
//For loop is applied to set all elements of aray to '0'
IntList::IntList(const int cap) {
list = new int[cap];
capacity = cap;
count=0;
for(int i=0; i<cap; i++) {
list[i]=0;
}
}
// This is a destructor .This doesnot take any argument. It is called as progam is exit.
//Here as we exit program.It will delete the object List array If list is not empty and will set count and capacity to 0.
IntList::~IntList() {
cout << "**destructor** ~IntList ";
if(list!=NULL) {
delete [] list;
list=NULL;
}
capacity=count=0;
}
// Here we are using operator overloading in which we are overloading "=" operator to compare two objects because norrmal '=' operator can only compare primitives values.
//In this function we are first comparing calling object and passed object and if they are equal then we return else if calling object list is not null we are deleting
//list. Then populting list with passed object list data and in the end returning calling object.
IntList& IntList::operator=(const IntList &source) {
if(this == &source){
return(*this);
}
if(list!=NULL) {
delete [] list;
list=NULL;
}
list = new int[source.capacity];
capacity = source.capacity;
count=source.count;
for(int i=0; i<count; i++) {
list[i] = source.list[i];
}
for(int i=count; i<capacity; i++){
list[i]=0;
}
return(*this);
}
// In this function we are setting value at particular index in list. First we check whether index is valid index or not if Yes then add element at that index else print error statement.
void IntList::setElement(const int index, const int value) {
if(isValid(index)){
list[index]=value;
}
else {
cout << "Invalid Index @ " << index << endl;
}
}
// In this function we are fetching element at given index. First we check If given index is valid or not If yes then fetch the element at given index else return -1.
const int IntList::getElement(const int index) const {
if(isValid(index)){
return(list[index]);
}
else return(-1);
}
//This function will aappend given item at end of list. First it check whether List has enough capacity to hold new element. If not it doubles the size of list and add given element next to previous last element.
void IntList::append(const int elem) {
if(size()==capacity){
int* tmp = list;
capacity = capacity*2;
list = new int[capacity];
  
for(int i=0; i<size(); i++){
list[i] = tmp[i];
}
delete [] tmp;
}
list[size()] = elem;
count++;
}
// This function return the number of elements present in list.
const int IntList::size() const {
return(count);
}

// This function will prrint all elements present in the list.
void IntList::printList() const {
cout << "list: ";
for(int i=0; i<size(); i++) {
cout << list[i] << " ";
}
cout << endl;
}
// This function is used for debugg purpose to print number of elements present in the list and actual size of list.
void IntList::debug() const {
cout << "**Debug**: ";
for(int i=0; i<capacity; i++) {
cout << list[i] << " ";
}
cout << " count: " << size() << " cap: " << capacity << endl;
}
/******************************************************************/