In: Computer Science
Write a C++ PROGRAM:
Add the function min as an abstract function to the class arrayListType to return the smallest element of the list.
Also, write the definition of the function min in the class unorderedArrayListType and write a program to test this function.
#include <iostream> // To use cout and cin
using namespace std;
class ArrayListType {
// Abstract class: A class with atleast one pure virtual function or abstract function.
// arrayListType: An abstract class
// Member functions: min(): Pure virtual function or abstract function.
public:
virtual int min(int list[], int n) = 0; // Pure virtual or abstract function.
};
class UnorderedArrayListType: public ArrayListType {
// unorderedArrayListType: Class inheriting arrayListType abstract class.
public:
int min(int list[], int n) {
int min = INT8_MAX; // Taking min as max value of int data type.
for ( int i = 0; i < n; i++ ) {
if(list[i] < min)
min = list[i];
}
return min;
}
};
int main() {
// Your program start executing from here.
UnorderedArrayListType uoalt;
int n;
cin >> n;
int list[n];
for (int i = 0; i < n; i++) {
// Taking n elements
cin >> list[i];
}
cout << "Minimun List Value: " << uoalt.min(list, n) << endl;
return 0;
}
OUTPUT: