Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Comparable{
- public:
- virtual int compare(Comparable &other) = 0;
- };
- class HighScoreElement : public Comparable{
- private:
- string name;
- int score;
- public:
- HighScoreElement();
- HighScoreElement(string name, int score);
- //HighScoreElement(Comparable &other);
- void Show();
- int compare(Comparable &other);
- };
- HighScoreElement::HighScoreElement() : name("Default"), score(0){}
- HighScoreElement::HighScoreElement(string name, int score) : name(name), score(score){}
- /*
- HighScoreElement::HighScoreElement(Comparable &other){
- HighScoreElement el;
- }
- */
- void HighScoreElement::Show(){
- cout << "Name: " << name << ", Score: " << score << endl;
- }
- int HighScoreElement::compare(Comparable& other) {
- HighScoreElement *other_el = (HighScoreElement *)& other;
- if (score == other_el->score)
- return 0;
- else if (score > other_el->score)
- return 1;
- else
- return -1;
- }
- int main()
- {
- cout << "Hello world!" << endl;
- HighScoreElement *he1 = new HighScoreElement("el1", 1);
- he1->Show();
- HighScoreElement *he2 = new HighScoreElement("el2", 2);
- he2->Show();
- cout << he1->compare(*he2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement