Answer:
part 1: Adding min as an abstract function to the class arrayListType
We cannot add an abstract function to the class arrayListType directly because it is a concrete class. Instead, we can make the function virtual and assign it a default implementation. Here's how we can do that:
class arrayListType {
public:
virtual int min() const {
int min = list[0];
for (int i = 1; i < length; i++) {
if (list[i] < min) {
min = list[i];
}
}
return min;
}
// rest of the class definition
};
Here, we made the min function virtual, which means that it can be overridden by derived classes. We also provided a default implementation of the function, which finds the minimum element of the list by iterating over all the elements and comparing them with a variable called min. We start with the first element of the list and update min whenever we find an element that is smaller. Finally, we return min.
part 2: Definition of min in the class unorderedArrayListType
Since the class unorderedArrayListType is derived from the arrayListType class, it inherits the min function. However, we can also override the function in the derived class if we want to provide a different implementation. Here's one way to do that:
class unorderedArrayListType : public arrayListType {
public:
int min() const override {
if (length == 0) {
throw std::logic_error("Cannot find minimum of an empty list");
}
int min = list[0];
for (int i = 1; i < length; i++) {
if (list[i] < min) {
min = list[i];
}
}
return min;
}
// rest of the class definition
};
Here, we override the min function and provide a new implementation that is similar to the one in the base class, but with an additional check for the length of the list. If the list is empty, we throw an exception to indicate that we cannot find the minimum. Otherwise, we find the minimum in the same way as before.
part 3: A program to test the min function in the class unorderedArrayListType
Here's a sample program that tests the min function in the unorderedArrayListType class:
#include <iostream>
#include "unorderedArrayListType.h"
using namespace std;
int main() {
unorderedArrayListType list(5);
list.insertEnd(3);
list.insertEnd(1);
list.insertEnd(4);
list.insertEnd(1);
list.insertEnd(5);
cout << "List: ";
list.print();
cout << "Minimum: " << list.min() << endl;
return 0;
}
This program creates an instance of the unorderedArrayListType class with a maximum size of 5 and inserts some elements into the list. Then it prints the list, finds the minimum element using the min function, and prints the result. The output should be:
List: 3 1 4 1 5
Minimum: 1
Step-by-step explanation: