abstract class, pure virtual function prob

I'm having trouble understanding what they are asking for here. My class is pretty much over, but I'd still like to take a shot at it. I don't think the textbook author defined this problem very well. This is exact text from the book.

Design a class, AbstractSort that can be used to analyze the number of comparisons performed by a sorting algorithm. The class should have a member function, compare, that is capable of comparing two array elements, and a means of keeping track of the number of comparisons performed. The class should be an astract class with a pure virtual member function:
void sort(int arr[], int size)
which, when overridden, will sort the array by calling the compare function to determine the relative order of pairs of numbers. Create a subclass of AbstractSort that uses a simple sorting algorithm to implement the sort function. The class should have a member function that can be called after the sorting is done to retrieve the number of comparisons performed.

Is it just me, or is this problem statement kinda ambiguous? What does he mean by "sort the array by calling the compare function to determine the relative order of pairs of numbers."?

Shouldn't the type of comparison wanted be specified here?
The class should have a member function, compare, that is capable of comparing two array elements, and a means of keeping track of the number of comparisons performed.

I'm having trouble with the whole classes, inheritance, polymorphism stuff.

In answer to your question:

Shouldn't the type of comparison wanted be specified here?

I would guess that lower numbers go before higher numbers in the array, so by cycling through the array comparing first and second, second and third etc and swapping them around if needed, you'll end up with all of them in sequence.
The compare function will be called to perform each comparison.
Have a read of this
http://www.cplusplus.com/doc/tutorial/polymorphism.html
Last edited on
The idea, as far as I can tell, is that sort functions will all, at some point, need to compare two elements in the array to determine the order.
This can be set into a function such as
1
2
3
4
5
6
7
8
int compare(int elementa,int elementb)
{
    if (elementa<elementb)
        return 1;
    else if (elementb>elementa)
        return -1;
    else return 0;
}

You can then write a whole set of classes which each implement sort in a different way (bubble sort, selection sort, quicksort, etc) but all use compare to do the comparison.
The type of comparison is not stated explicitly, but by declaring void sort(int arr[], int size) you know the elements are int's, so yo uonlu have a choice of numeric ascending or descending, and it would be pretty harsh to downgrade you for picking the wrong one!
Last edited on
Thanks for the responses!
I have a feeling this forum is going to be a real plus.
Topic archived. No new replies allowed.