Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// DO NOT CHANGE THIS FILE /********************** * File: check13b.cpp *********************/ #include using namespace std; #include numberList.h void changeList(NumberList list) { // Passing the list

image text in transcribed

// DO NOT CHANGE THIS FILE

/**********************

* File: check13b.cpp

*********************/

#include

using namespace std;

#include "numberList.h"

void changeList(NumberList list)

{

// Passing the list by copy on purpose!

cout

list.displayList();

cout

list.setNumber(4, 25);

list.displayList();

cout

}

/****************************************************************

* Function: main

* Purpose: Test the NumberList class.

****************************************************************/

int main()

{

// YOU SHOULD NOT CHANGE ANYTHING IN THIS FUNCTION

cout

NumberList list(5);

cout

list.displayList();

cout

list.setNumber(0, 1);

list.setNumber(1, 10);

list.setNumber(2, 7);

list.setNumber(3, 15);

list.displayList();

cout

NumberList list2(list);

NumberList list3;

NumberList list4;

list4 = list3 = list;

list.setNumber(0, 2);

list2.setNumber(1, 2);

list3.setNumber(2, 2);

list4.setNumber(3, 2);

cout

list.displayList();

cout

list2.displayList();

cout

list3.displayList();

cout

list4.displayList();

cout

changeList(list);

cout

list.displayList();

cout

return 0;

}

/*********************************************************

numberList.cpp file

**********************************************************/

#include "numberList.h"

#include

using namespace std;

/******************************************************

* Function: getNumber

* Description: Returns the number at the given index.

******************************************************/

int NumberList::getNumber(int index) const

{

int number = -1;

if (index >= 0 && index

{

number = array[index];

}

return number;

}

/******************************************************

* Function: setNumber

* Description: Sets the value to the array at the given index.

******************************************************/

void NumberList::setNumber(int index, int value)

{

if (index >= 0 && index

{

array[index] = value;

}

}

/******************************************************

* Function: displayList

* Description: displays the list

******************************************************/

void NumberList::displayList() const

{

for (int i = 0; i

{

cout

}

cout

}

/*******************

numberList.h file

* NumberList class

*******************/

#ifndef NUMBER_LIST_H

#define NUMBER_LIST_H

#include

class NumberList

{

private:

int size;

int *array;

public:

NumberList()

{

size = 0;

array = NULL;

}

// TODO: Add your constructor and destructor

int getNumber(int index) const;

void setNumber(int index, int value);

void displayList() const;

};

#endif

****************************************************

makefile

****************************************************

a.out : numberList.o check13b.o

g++ numberList.o check13b.o

numberList.o : numberList.h numberList.cpp

g++ -c numberList.cpp

check13b.o : numberList.h check13b.cpp

g++ -c check13b.cpp

clean :

rm *.o *.out

Overview This checkpoint is intended to help you practice the syntax of copy constructors, assignment operators, and destructors. You will build on your work from Checkpoint A. The Rule of 3 for C++ states that copy constructors, assignment operators, and destructors almost always go together. Whenever you have need to implement one of them, you should also implement the other two This is true for our example from Checkpoint 13A. W allocate it in the destructor. This is correct. However, for the same reason that we needed a destructor (we had dynamically allocated memory), we will also have problems if we try to copy our object. Rather than the new NumberList getting it's own array full of numbers, it will point to the memory of the first one. So if you change one of them, it will affect both. Thus, we need to provide this copy functionality to allocate another array and copy each number over e were using a dynamically allocated array, therefore we needed to de- Instructions You are provided with the same NumberList class from Checkpoint A. However, this time, there is a different main function that tests it. Your task is to fill complete a copy constructor and an assignment operator to properly allocate and copy over the array of numbers. You should also have a destructor (that you implemented in Checkpoint A) 1. Copy the provided code mkdir check13b cd check13b cp /home/cs165new/check13b/* . 2. Then you can copy over your numberList files from Checkpoint A if you would like, so you don't have to implement the destructor again: /Assuming there is a sibling directory named "check13a" // that has the files you want. cd check13b ?/ if you are not already there cp ../check13aumberList.* . 3. Implement a copy constructor for the NumberList It should 1. Set the size of the new list to be the same as the other one 2. Dynamically allocate an array of that size 3. Copy over each value (one by one) from the array of the original NumberList to our new one 4. Implement an assignment operator. It should 1. Check to see if the array is already allocated, and if so, delete it. 2. Then, do the exact same things as the copy constructor 3. Finally return a reference to the current obiect. 5. A main function is provided for vou that will create and use some NumberList obiects and make copies of them. You should not change this function. All you need to do is add the copy constructor and assignment operator mentioned above

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

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

More Books

Students also viewed these Databases questions

Question

Conduct a needs assessment. page 283

Answered: 1 week ago