Question #1: Consider the following class declaration when answering the parts o
ID: 3670583 • Letter: Q
Question
Question #1: Consider the following class declaration when answering the parts of this question:
class List
{
public:
List(); //Default constructor;
//The list will have an initial size equal to 1 and count equal to 0. Initialize the values in all the cells of the list to 0.
List(const List &); //copy constructor
~List(); //destructor
bool IsEmpty(){return count ==0;}; //returns true if List is empty.
bool IsFull(){return count == size;}; //returns true is List is full.
void Add(const int &);
//adds an item, inorder, into the List. Items may be shifted if necessary. Also if the array is full its size of tripled.
int Subtract(const List & L2);
//subtracts the values in L2 from their corresponding values in L and
//stores the results (difference) in the corresponding
//cells in L. The sum of the values stored in L is then returned.
//Assume L and L2 have the same count and capacity.
int Find(const int &);
// returns the location of item in the list; otherwise -1;
friend ostream & operator<<(ostream & out, const List & Org);
//Overload the operator<< with chaining. This function will print
//all the values stored in L.
private:
int *L; //dynamic array
int size; //capacity of the dynamic array, L
int count; //number of items stored in the dynamic array, L
};
a. Implement the default constructor.
b. Implement the copy constructor.
c. Implement Add.
d. Implement the code necessary to overload “operator<< as a friend function with chaining.
e. Implement Subtract.
Explanation / Answer
Here is the code for you:
#include <iostream>
using namespace std;
class List
{
public:
List(); //Default constructor;
//The list will have an initial size equal to 1 and count equal to 0. Initialize the values in all the cells of the list to 0.
List(const List &); //copy constructor
~List(); //destructor
bool IsEmpty(){return count ==0;}; //returns true if List is empty.
bool IsFull(){return count == size;}; //returns true is List is full.
void Add(const int &);
//adds an item, inorder, into the List. Items may be shifted if necessary. Also if the array is full its size of tripled.
int Subtract(const List & L2);
//subtracts the values in L2 from their corresponding values in L and
//stores the results (difference) in the corresponding
//cells in L. The sum of the values stored in L is then returned.
//Assume L and L2 have the same count and capacity.
int Find(const int &);
// returns the location of item in the list; otherwise -1;
friend ostream & operator<<(ostream & out, const List & Org);
//Overload the operator<< with chaining. This function will print
//all the values stored in L.
private:
int *L; //dynamic array
int size; //capacity of the dynamic array, L
int count; //number of items stored in the dynamic array, L
};
List::List()
{
size = 1;
count = 0;
L = (int *)malloc(sizeof(int));
}
List::List(const List &l)
{
size = l.size;
count = l.count;
L = l.L;
}
void List::Add(const int &l)
{
if(count == size)
L = (int *)realloc(L, size*3);
int i;
for(i = count-1; i >= 0; i--)
if(*(L+i) > l)
*(L+i+1) = *(L+i);
else
break;
*(L+i+1) = l;
}
ostream & operator<<( ostream &out, const List & Org )
{
for(int i = 0; i < Org.count; i++)
out << *(Org.L+i)<<" ";
out<<endl;
return out;
}
int List::Subtract(const List & L2)
{
int sum = 0;
for(int i = 0; i < count; i++)
{
*(L + i) -= *(L2.L +i);
sum += *(L+i);
}
return sum;
}
If you need any further refinements, just get back to me.