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]); } } 
  • The copy constructor was done, and the assignment operator was forgotten. This is not an error, but in a real program such a class cannot normally be used ... Plus, in addition to the current operator[] variant, it would be nice to add const overload, otherwise how to get numbers from constant arrays? - HolyBlackCat 2:42 pm

2 answers 2

First, there is no need to make the base class abstract, declaring two pure virtual functions in it. You must define them so that the base class performs in its own way and has the ability to create objects of the base class.

 virtual Array& Array::add (Array&) const; virtual void Array::foreach (); 

Secondly, you define a function using a bit operation, but you are trying to perform a bit operation on double variables, which is impossible. True, I did not clearly understand the condition of the problem, but it can be done, for example, as follows:

 // XorArray методы // для начала сделайте члены класса защищенными, а не закрытыми Array& XorArray::add(const Array &x) override { for (size_t i = 0; i < sz; i++) { double d1 = arr[i], d2 = x.arr[i]; int x1 = d1, x2 = d2; arr[i] = (x1 ^ x2) + (d1 - x1) + (d2 - x2); } return *this; } 

And you also define the copy constructor incorrectly: It is necessary to destroy the old array, if the size of the copied object is different, and then create a new one, otherwise, simply assignment:

 Array (const Array& other){ if ( sz != other.sz) { delete arr; sz = other.sz; arr = new double [sz]; } for (size_t i = 0; i < sz; i++) arr[i] = other.arr[i]; } 

Did you see any other errors? .. But you also need to define an assignment operator

  • Advice: do not use foreach identifier, think up a different name - AR Hovsepyan
  • I need to call Foreach according to my instructions) and there are at least the errors that I have indicated to allocate in add'ah of both classes. I need to write them well, and I am not very familiar with the syntax. Another question, without a double I won’t; need a square root in XorArray. - Gregory Volzhin
  • And I did not advise not to use double. Just need not try to perform bit operations on them. x.arr [i] = arr [i] ^ arr [i]; that's why Just take sum up - AR Hovsepyan
  • Again, the assignment states that you need to add arrays as an Exclusive OR, and what I wrote is a sketch for thinking about how to implement it correctly - Gregory Volzhin
  • Grigory Volzhin, read the updated answer - AR Hovsepyan

You have incorrectly declared the heirs methods. The signatures of the virtual methods must match the signatures of the base class, i.e. heir declaration should be something like this:

 class SortArray: public Array{ ... Array& add (Array&) const override; void foreach() override; ... }; 

In order to prevent such errors, use override in successors — in this case, in case of an error, the compiler will directly say that the base class does not have a virtual method with such a signature.

  • You can ask you to throw off a link to a good source, where everything will be listed on the shelves. I do not understand how the heirs should take values ​​from the base class and can’t figure it out in any way, I have already reread a couple of textbooks, but with their examples ... - Grigory Volzhin