Add the function max as an abstract function to the class arrayListType to return the largest element of the list.

QUESTION

Add the function max as an abstract function to the class arrayListType to return the largest element of the list. Also, write the definition of the function max in the class unorderedArrayListType and write a program to test this function.

 

SUMMARY

An abstract class is implemented having a pure virtual function or an abstract method and this abstract method’s definition is written in another class which is inherited. The functionality is then tested.

 

EXPLANATION

The class named arrayListType is implemented having the function max() as an abstract method. Another array as another member of the class is used to find the maximum element of it. A class named unorderedArrayListType is implemented which inherits the arrayListType class. In this class, the max() function is defined and a constructor overrides the base class constructor. The max function of this class returns the maximum element of the array of arrayListType class.

 

CODE

#include <iostream>
using namespace std;
class ArrayListType 
{
    public:
    int arr[5];
    arrayListType(int a[])
    {
        for(int i=0;i<5;i++)
        arr[i]=a[i];
    }
    virtual int max() = 0;
};
class unorderedArrayListType : public arrayListType 
{
    public:
    unorderedArrayListType(int a[]): arrayListType(a){}
    int max()
    {
        int large=0;
        for(int j=0;j<5;j++){
            if(large<arr[j])
            large=arr[j];
        }
        return large;
    }
};
int main()
{
    int arr[5]={10,20,54,76,1};
    unorderedArrayListType al(arr);
    cout<<al.max()<<endl;
}

 

OUTPUT

 

 

Also Read: Design and implement (i.e write a program) an algorithm that adds two polynomials such as.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *