I have a base class in which I have a pure virtual function and with this function, I want to override it in other derived classes (in some of those with a different number of parameters if possible).
So in the MergeSort subclass, I have MSort method which will need a different number of parameters as it is done recursively.
So having this function at the moment with those parameters I'm getting this error 'MergeSort:' cannot instantiate abstract class. BUT if I override the Sort method from the base class works fine, but I don't need one parameter.
I also tried to declare another virtual function with a different number of parameters and define it in MergeSort class and I get the same thing.
I'd also like to clarify that I have other subclasses for different algorithms (bubble sort, insertion sort etc) which are implemented similarly to MergeSort (a constructor and a sort function) but the sort function has the same no of parameters(just one used for a graphical interface) like in the base class from below.
So is it possible to have an overridden method with a different number of parameters? Or any other solution to what I've said above?
// BASE CLASS
// Forward declaration
class Interface;
/**
* Base class from which the sorting algorithms classes will inherit (Polymorphic class)
* The base class will allow us to create a sequence with n elements
*/
class SortingAlgorithms
{
protected:
std::vector<sf::RectangleShape> sequence; // vector which will contain a randomized sequence
std::vector<sf::RectangleShape> sequenceCpy; // a copy of sequence used for interaction features
sf::RenderWindow& window; // initializes the window
int minimum, maximum; // the range in which the elements will be randomized
int elements; // the number of elements which will be initialized
public:
SortingAlgorithms();
/** SortingAlgorithms() - class constructor which initializes the sequence
* @param min - the minimum value for randomizing
* @param max - the maximum value for randomizing
* @param els - the number of elements to generate
* @param win - since the window will be initialized only once (singleton pattern);
* it will be needed to pass on this object to almost every function that has
graphics features
*/
SortingAlgorithms(int min, int max, int els, sf::RenderWindow& win);
// A pure virtual function for overriding and param init which is what I described about win param from SortingAlgorithms constructor
virtual void Sort(std::unique_ptr<Interface>& init) = 0;
};
class MergeSort : public SortingAlgorithms
{
public:
MergeSort(int min, int max, int els, sf::RenderWindow& win);
void Merge(std::unique_ptr<Interface>& init, int first, int mid, int last);
void MSort(std::unique_ptr<Interface>& init, int first, int last);
};
As stated in the comments, you have to use the same signature for all your overrides. In doing so, you could use the following approach: use overridden function as a sort of entrypoint, inside which you call a real function (maybe private) that performs sorting. An example to illustrate the approach:
class SortingAlgo
{
public:
virtual void sort(int arr[], int n) = 0;
};
class BubbleSort: public SortingAlgo
{
public:
void sort(int arr[], int n){
this->bubble_sort(arr, n);
}
private:
void bubble_sort(int arr[], int n){
//implemetation
}
};
class MergeSort: public SortingAlgo
{
public:
void sort(int arr[], int n){
this->mergeSort(arr, 0, n - 1);
}
private:
void mergeSort(int arr[], int l, int r){
//recursive implemetation
}
void merge(int arr[], int l, int m, int r){
//implemenation
}
};