Help finish the program please. You need to create a base Array class with virtual foreach () addition and elementwise processing methods and the Sort Array and XorArray classes derived from it. In SortArray, the addition operation is implemented as the intersection of sets, and elementwise processing by extracting the root of each element of the array. In XorArray, addition is either exclusive or, and elementwise processing is array sorting. I have already set foreach () for both classes (for the second I am not sure what is true). I can not implement addition, the compiler complains back in the base class. Below is the ghost code. Error in SortArray :: add - error: cannot allocate an object of abstract type 'Array' (not the only one)
#ifndef ARRAY #define ARRAY #include <iostream> #include <iomanip> #include <cstring> #include <stdexcept> using namespace std; class Array{ public: double *arr; size_t sz; public: Array () { arr = nullptr; sz = 0; } Array (size_t _sz){ arr = new double[_sz]; sz = _sz; } Array (double *a, size_t s){ sz = s; arr = new double [s]; for (size_t i = 0; i < sz; i++) arr[i] = a[i]; } Array (const Array& other){ sz = other.sz; arr = new double [sz]; for (size_t i = 0; i < sz; i++) arr[i] = other.arr[i]; } virtual ~Array (){ delete [] arr; } void print() const { for(size_t i = 0; i < sz; ++i) cout << setw(6) << setiosflags(ios::showpoint) << setprecision(3) << arr[i]; cout << endl; } double& operator[](size_t i) { if(i < 0 || i >= sz) throw out_of_range("Exit for size"); else return arr[i]; } size_t resize(size_t newsz) { if(newsz < sz) return sz; else { double *newarr = new double [newsz]; for(size_t i = 0; i < sz; ++i) newarr[i] = arr[i]; for(size_t i = sz; i < newsz; ++i) newarr[i] = 0; sz = newsz; delete [] arr; arr = newarr; return newsz; } } size_t size() const { return sz; } virtual Array& add (Array&) const = 0; virtual void foreach () = 0; }; #endif // ARRAY //SortArray методы SortArray& add (const Array&other){ Array *temp; for(size_t i = 0; i < sz; i++) temp [i] = arr[i]; for(size_t j = sz; j < other.sz; j++) temp [j] = other.arr[j - sz]; return temp; } void foreach(){ double temp; for (size_t i = 0; i < sz - 1; i++) { for (size_t j = 0; j < sz - i - 1; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } // XorArray методы XorArray& add (const Array &x){ for (size_t i = 0; i < arr; i++) x.arr[i] = arr[i] ^ arr[i]; } void foreach(){ double *newarr = new double[sz]; for (size_t i = 0; i < sz; i++) { newarr[i] = arr[i]; } for (size_t i = 0; i < sz; i++) { arr[i] = sqrt(newarr[i]); } }
operator[]variant, it would be nice to addconstoverload, otherwise how to get numbers from constant arrays? - HolyBlackCat 2:42 pm