Question: C++ question - Classes - question 2 is part of question 1 ----------------------------------------------------- Exercise 1: Implement the Array class that is defined below and test
C++ question - Classes - question 2 is part of question 1
-----------------------------------------------------
Exercise 1: Implement the Array class that is defined below and test it in the main program. The main program must test all the functions that are defined in the class.
#ifndef array_h
#define array_h
class Array { // Class declaration
public:
Array (int =5); //Initialize the array with 0 values
Array (const Array & ); // copy constructor
~Array(); //destructor
bool setValue (int index, int value); // assign a value to an
//array element
bool getValue (int index, int &value); // get an array element
Array & increment(); /* cascade function to increment each array element by 1 */
int getSize ( ); // return the size of the array.
void print ( ) const; // print each element of the array
Array& Add(const Array ar);/*casade function to add all elements of arr to all elements of the this array (element by element) and return a reference to the resulting array */
bool Equal(const Array *ar)const;// return true if all elements in both arrays are equal, false otherwise.
Array& removeAt(int index);//cascade function to remove element at the index.( if index is within the valide range, you should allocate new memory with 1 element less, copy all elements but the element at index to new memory, delete the old one)
Array& insertAt(int index, int value);// insert value at index location, if index is greater than the size, then insert the element at the end of the array, if index is < 0 then insert the element at the beginig of the array, otherwise, insert in the exact index ( you should allocate new memory, copy element etc)
private:
int size; // size of created array
int* arr;
};
#endif
Exercise 2: sumArray ( ) is a function that sums all the elements of a given array object. Implement sumArray ( ) using the three techniques that we have learned in the lecture, namely:
As a non-member function of the class Array
int sumArray ( const Array &);
As a friend function of the class Array
friend int sumArray ( const Array &);
As a public member function of the class Array
int Array::sumArray ();
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
