Hi all. I am really struggling with this class and I need some homework help. I
ID: 3546539 • Letter: H
Question
Hi all. I am really struggling with this class and I need some homework help. I need to rewrite a program so that has a dynamically allocated array and I need a test program. I'm posting the correct header and implementation that I need for this, and the test program is well.... just an idea that I have but it's not an actual program that works, just to show what it needs to do (it's very bad programming I know.... I'm having a hard time). I'm not even sure what all is supposed to go into a test file/application file. So this is what I need the class to do:
1.) Replace the internal fixed array with a dynamically allocated array
2.) Class cannot get full
//Header File
#include <iostream>
using namespace std;
const int MAX_SIZE = 50;
class ListDynamic
{
ListDynamic();
bool full();
int get_size();
void add_value(double value);
double get_value(int index);
double get_last();
void delete_last();
friend ostream& operator <<(ostream& out, const List& thisList);
private:
double list_values[MAX_SIZE];
int size;
};
//Application File
#include <iostream>
#include "List.h"
using namespace std;
int main ()
{
cout << "Enter Value: " << char*array << endl;
cout << "There are 10 values on the list. "
<< "The first value on the list is " << value1;
<< "The last value on the list is " << lastvalue;
<< "After deleting last value, there are " << valueleft << "values left.";
<< " The new list is: " << newlist << endl;
cout << "Added items on the list up through item " << totalvalue << endl;
return 0;
}
//Implementation File
#include <iostream>
#include "List.h"
using namespace std;
// These are based on Display 11.10 and 11.11
ListDynamic::ListDynamic()
{
size = 0;
}
// MAX_SIZE is defined in the header file
bool ListDynamic::full()
{
return (size == MAX_SIZE);
}
int ListDynamic::get_size()
{
return size;
}
void ListDynamic::add_value(double value)
{
if (size < MAX_SIZE)
{
list_values[size] = value;
size++;
}
else
cout << " *** Error in List Class: Attempting to add value past max limit.";
}
double ListDynamic::get_value(int index)
{
if (index < size)
return list_values[index];
else
cout << " *** Error in List Class: Attempting to retrieve value past current size.";
}
double ListDynamic::get_last()
{
if (size > 0)
return get_value(size - 1);
else
cout << " *** Error in List Class: Call to getLast in Empty List.";
}
void ListDynamic::delete_last()
{
if (size > 0)
size--;
else
cout << " *** Error in List Class: Call to deleteLast in Empty List.";
}
ostream& operator <<(ostream& out, const List& thisList)
{
for (int i = 0; i < thisList.size; i++)
out << thisList.listValues[i] << endl;
return out;
}
Explanation / Answer
mSize = copy.mSize; mArray = new int[copy.mSize]; // Don't need to worry about copying integers. // But if the object has a copy constructor then // it would also need to worry about throws from the copy constructor. std::copy(©.mArray[0],©.mArray[c.mSize],mArray); } // Define assignment operator in terms of the copy constructor // Modified: There is a slight twist to the copy swap idium, that you can // Remove the manual copy made by passing the rhs by value thus // providing an implicit copy generated by the compiler. A& operator=(A rhs) // Pass by value (thus generating a copy) { rhs.swap(*this); // Now swap data with the copy. // The rhs parameter will delete the array when it // goes out of scope at the end of the function return *this; } void swap(A& s) throws () { std::swap(this.mArray,s.mArray); std::swap(this.mSize ,s.mSize); } // C++11 A(A&& src) noexcept : mSize(0) , mArray(NULL) { (*this) = std::move(src); // Implements in terms of assignment } A& operator=(A&& src) noexcept { src.swap(*this); // You are moving the state of the src object // into this one. The state of the src object // after the move must be valid but indeterminate. // // The easiest way to do this is to swap the states // states of the two objects. // // Note: Doing any operation on src after a move // is risky (apart from destroy) until you put it // into a specific state. Your object shoudl have // appropriate methods for this. // // Example: Assignment (operator = should work). // std::vector() has clear() which sets // a specific state without needing to // know the current state. } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.