I'm trying to make a program that keeps track of how many times a sort does a comparison but I'm not sure how to pass a variable to and from the base class to the child class. Right now I get an error saying "error: passing 'const childsort' as 'this' argument of 'void AbstractSort::setConversions(int)' discards qualifiers [-fpermissive]|"
I have a few questions. Isn't the purpose of using a child class that it inherits the functions from the parent class? If so how do I get access to the conversion variable in the base class through the child class? If not, do I need a local variable and a local function in the child class to count the conversions?
#include <iostream>
using namespace std;
class AbstractSort
{
protected :
int conversions;
public :
AbstractSort(){};
void setConversions(int c)
{ conversions = c; }
int getConversions() const
{ return conversions; }
int addConversions()
{ return conversions++; }
virtual void sort(int arr[], int size) const = 0;
};
class childsort : public AbstractSort
{
public :
childsort() : AbstractSort(){}
virtual void sort(int arr[], int size) const
{
int x = 0, y = 0, c = 0;
for(x = 0; x < size - 1; ++x)
{
for(y = 0; y < size - x - 1; ++y)
{
if(arr[y] > arr[y + 1])
{
int temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
setConversions(c++);
}
}
}
};