Hello,
I wrote a solution to the following practice problem.
Implement a sort function that takes a vector of pointers to an interface class, Comparable, that defines a method, compare(Comparable& other), and returns 0 if the objects are the same, 1 if the object is greater than other, and -1 if the object is less than other. Create a class that implements this interface, create several instances, and sort them. If you're looking for some inspiration for what to create—try a HighScoreElement class that has a name and a score, and sorts so that the top scores are first, but if two scores are the same, they are sorted next by name.
My code is below
Code:
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
class Comparable
{
public:
virtual int compare(Comparable& other)=0;
};
class HighScoreElement : public Comparable
{
public:
virtual int compare(HighScoreElement& other);
void set_members(string user_name, int user_high_score)
{
name=user_name;
high_score = user_high_score;
}
void print_members()
{
cout<<"Name:\t"<<name<<"\tHigh Score:\t"<<high_score<<endl;
}
string get_name()
{
return name;
}
int get_high_score()
{
return high_score;
}
private:
string name;
int high_score;
};
int HighScoreElement :: compare(HighScoreElement& other)
{
if(this->high_score>other.high_score) return (1);
if(this->high_score < other.high_score) return(-1);
if(this->name>other.name) return(1);
if(this->name<other.name) return(-1);else return(0);
}
void sort(vector<Comparable*> comparables)
{
cout<<"\nEntering the Sort Function";
cout<<"\nThis is the state of the vector at the beginning of the sort function using comparables parameter\n";
for(int i=0;i<comparables.size();i++)
{
((HighScoreElement*)comparables[i])->print_members();
}
Comparable *p_temp_Comparable;
for(int i=0;i<comparables.size()-i;i++)
{
for(int j=i+1; j<comparables.size();j++)
{
if(comparables[i]->compare(*comparables[j])<0)
{
p_temp_Comparable = comparables[i];
comparables[i]=comparables[j];
comparables[j] = p_temp_Comparable;
}
}
}
}
int main()
{
cout << "Hello world!" << endl;
HighScoreElement *p_HighScoreElement;
vector<Comparable*> comparables;
vector<HighScoreElement*> highscoreelements;
for(int i =0;i<10;i++)
{
p_HighScoreElement = new HighScoreElement;
p_HighScoreElement->set_members("Ateeque Mohammad", rand()%100);
comparables.push_back(p_HighScoreElement);
highscoreelements.push_back(p_HighScoreElement);
}
for(int i=0;i<10;i++)
{
p_HighScoreElement = new HighScoreElement();
p_HighScoreElement->set_members("Abdullah Mohammad", highscoreelements[i]->get_high_score());
comparables.push_back(p_HighScoreElement);
highscoreelements.push_back(p_HighScoreElement);
}
for(int i=0;i<10;i+=3)
{
p_HighScoreElement = new HighScoreElement();
p_HighScoreElement->set_members("Abdurrahman Mohammad", rand()%100);
comparables.push_back(p_HighScoreElement);
highscoreelements.push_back(p_HighScoreElement);
}
cout<<"\nThis is the state of the vector after the auto fill was run\n";
/*for ( vector<HighScoreElement*>::iterator itr = highscoreelements.begin(), end = highscoreelements.end();
itr != end; ++itr )
(*itr)->print_members();
*/
for(int i=0;i<highscoreelements.size();i++)
{
highscoreelements[i]->print_members();
}
cout<<"\nThis is the state of the vector after the auto fill was run using comparables\n";
for(int i=0;i<comparables.size();i++)
{
((HighScoreElement*)comparables[i])->print_members();
}
sort(comparables);
cout<<"\nThis is the state of the vector after the sorting of the vector\n";
for(int i=0;i<highscoreelements.size();i++)
{
highscoreelements[i]->print_members();
}
//auto_fill_vector();
return 0;
}
When I build the code I get the below errors.
\Sort_Inheritance\main.cpp|87|error: invalid new-expression of abstract class type 'HighScoreElement'|
C:\Users\atmoha\OneDrive\CPLUSPLUS\Chapter 27\Sort_Inheritance\main.cpp|16|note: because the following virtual functions are pure within 'HighScoreElement':|
C:\Users\atmoha\OneDrive\CPLUSPLUS\Chapter 27\Sort_Inheritance\main.cpp|11|note: virtual int Comparable::compare(Comparable&)|
C:\Users\atmoha\OneDrive\CPLUSPLUS\Chapter 27\Sort_Inheritance\main.cpp|96|error: invalid new-expression of abstract class type 'HighScoreElement'|
C:\Users\atmoha\OneDrive\CPLUSPLUS\Chapter 27\Sort_Inheritance\main.cpp|105|error: invalid new-expression of abstract class type 'HighScoreElement'|
Looks like the Compare method in the class HighScoreElement is not overriding the Compare method of the Comparable class when I did declare it pure virtual in Comparable class and then made the declaration of the method as a virtual in HighScoreElement class and then defined it as well.
Can someone point out what am I doing wrong?