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 above.
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);
};
Sort
function without trying to change the signature and use it as a sort of entrypoint, inside which you call a real function (maybe private) that performs sorting, for example, in a recursive way or something else – Wuhsien