Part 1) [10 pts] Create a class List. The class has the following data members:
ID: 3858226 • Letter: P
Question
Part 1) [10 pts] Create a class List. The class has the following data members: 1.) numItems as an integer 2.) list as a pointer to an integer array 3.) objectCount as a static integer, to keep track of the number of objects created.
The class has following function members:
1- [5 pts] A constructor that takes in one parameter of type integer to set the numItems. In the constructor, use the pointer list to create an array dynamically of size numItems and initialize it to a DEFAULT_VALUE (0). 2- [5 pts] A function readList to fill the List object with integer values from the user. 3- [5 pts] A function print to print the list items separated by spaces. 4- [5 pts] A function getAverge to return the average of the list items. 5- [5 pts] A function getObjectCount to return the objectCount. 6- [5 pts] A copy constructor that copies all the data members and list values to the newly created List object (copy the values in the array not the address). 7- Overload the operators +, -, =, ==, ++ such that: a. [5 pts] The statement list1 + list2 will return a List object that each element of it is the sum of the corresponding elements in list1 and list2. b. [5 pts] The statement list1 - list2 will return a List object that each elements of it is the result of subtracting the elements of list2 from the corresponding elements in list1. c. [5 pts] The statement list1 = list2, will copy the elements in list2 to the corresponding elements in list1. d. [5 pts] The statement list1 == list2, will compare the corresponding elements in list1 and list2. It returns true if all corresponding elements are similar and false otherwise. e. [5 pts] The statement list1++ increments all the elements of list1 by one.
Explanation / Answer
#include<iostream>
using namespace std;
class intArray
{
private:
int numItems;
int* list;
static int objectCount;
public:
intArray(int itemCount){
int i =0 ;
numItems = itemCount;
list = new int[numItems];
for(i=0; i<numItems ;i++)
list[i] = 0;
objectCount++; //Incrementing the number of objects created
}
intArray(intArray &obj){
int i=0;
numItems = obj.numItems;
if(numItems > 0){
list = new int[numItems];
for(i=0; i<numItems ; i++){
list[i] = obj.list[i];
}
}
objectCount++; //Incrementing the number of objects created
}
void readList(void){
int i =0;
cout<< "Please enter the values for the list :" <<endl;
for(i=1 ; i <= numItems ;i++){
cout<<"Value " <<i <<" :";
cin>>list[i-1];
}
}
void printList(void){
int i =0 ;
cout<<"Values : ";
for(i=0; i<numItems ;i++)
cout << list[i] <<" ";
cout<<endl;
}
double getAverage(void){
double sum = 0.0f;
int i=0;
for(i=0; i<numItems ;i++){
sum += list[i];
}
return (sum / numItems);
}
static int getObjectCount(void){
return objectCount;
}
intArray operator+(const intArray& other);
intArray operator-(const intArray& other);
void operator=(const intArray& other);
bool operator==(const intArray& other);
intArray operator++(int);
};
int intArray::objectCount = 0;
intArray intArray::operator+(const intArray& other)
{
int len1 = numItems;
int len2 = other.numItems;
int i=0;
int max = (len1 > len2) ? len1 : len2;
intArray newList(max);
newList.numItems = max;
while( i<len1 && i < len2){
newList.list[i] = list[i] + other.list[i];
i++;
}
while(i < len1){
newList.list[i] = list[i];
i++;
}
while(i < len2){
newList.list[i] = other.list[i];
i++;
}
return newList;
}
intArray intArray::operator-(const intArray& other)
{
int len1 = numItems;
int len2 = other.numItems;
int i=0;
int max = (len1 > len2) ? len1 : len2;
intArray newList(max);
newList.numItems = max;
while( i<len1 && i < len2){
newList.list[i] = list[i] - other.list[i];
i++;
}
while(i < len1){
newList.list[i] = list[i];
i++;
}
while(i < len2){
newList.list[i] = -1 * other.list[i];
i++;
}
return newList;
}
void intArray::operator=(const intArray& other)
{
int i = 0;
while(i < numItems){
list[i] = other.list[i];
i++;
}
}
bool intArray::operator==(const intArray& other){
int i =0;
bool flag = true;
if(numItems != other.numItems)
return false;
while( i <numItems){
if(list[i] != other.list[i]){
flag= false;
break;
}
i++;
}
return flag;
}
intArray intArray::operator++(int)
{
int i = 0;
for(i=0 ;i<numItems ;i++){
list[i]++;
}
return *this;
}
// Function main() for your testing
int main()
{
intArray obj1(2);
obj1.readList();
intArray obj2(obj1);
cout<< (obj1 == obj2) <<endl;
obj2.readList();
cout<< (obj1 == obj2) <<endl;
obj1.printList();
obj2.printList();
(obj1 + obj2).printList();
(obj1 - obj2).printList();
cout << "Object count : " <<intArray::getObjectCount()<<endl;
obj1++;
obj1.printList();
cout<< "Avg : " << obj1.getAverage() << endl;
cout<< "Avg : " << obj2.getAverage() << endl;
cout <<"**************" <<endl;
obj2 = obj1;
obj1.printList();
obj2.printList();
return 0;
}