Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Comparable{
  6. public:
  7. virtual int compare(Comparable &other) = 0;
  8. };
  9.  
  10. class HighScoreElement : public Comparable{
  11. private:
  12. string name;
  13. int score;
  14. public:
  15. HighScoreElement();
  16. HighScoreElement(string name, int score);
  17. //HighScoreElement(Comparable &other);
  18. void Show();
  19. int compare(Comparable &other);
  20. };
  21.  
  22. HighScoreElement::HighScoreElement() : name("Default"), score(0){}
  23. HighScoreElement::HighScoreElement(string name, int score) : name(name), score(score){}
  24. /*
  25. HighScoreElement::HighScoreElement(Comparable &other){
  26. HighScoreElement el;
  27.  
  28. }
  29. */
  30. void HighScoreElement::Show(){
  31. cout << "Name: " << name << ", Score: " << score << endl;
  32. }
  33. int HighScoreElement::compare(Comparable& other) {
  34. HighScoreElement *other_el = (HighScoreElement *)& other;
  35. if (score == other_el->score)
  36. return 0;
  37. else if (score > other_el->score)
  38. return 1;
  39. else
  40. return -1;
  41. }
  42.  
  43. int main()
  44. {
  45. cout << "Hello world!" << endl;
  46.  
  47. HighScoreElement *he1 = new HighScoreElement("el1", 1);
  48. he1->Show();
  49. HighScoreElement *he2 = new HighScoreElement("el2", 2);
  50. he2->Show();
  51.  
  52. cout << he1->compare(*he2);
  53.  
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement