Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Position {
  4. public:
  5.   Position(int x, int y) : x(x), y(y) {}
  6.   int x;
  7.   int y;
  8. };
  9.  
  10. class Player {
  11. private:
  12.   Position pos;
  13. public:
  14.   Player() : pos(0, 0) {}
  15.  
  16.   // rückgabetyp const da position nicht nachträglich veränderbar
  17.   // heißt wenn du getPosition().x = 13; aufrufst gibt es einen fehler
  18.   const Position &getPosition() const { // funktion const da sie das objekt nicht verändert
  19.     return this->pos;
  20.   }
  21.  
  22.   void move(const Position &pos) { // funktion nicht const da sie das objekt verändert
  23.     this->pos.x += pos.x;
  24.     this->pos.y += pos.y;
  25.   }
  26. };
  27.  
  28. int main()
  29. {
  30.     Player pl;
  31.     std::cout << pl.getPosition().y << std::endl;
  32.     pl.move(Position(0, 3));
  33.     std::cout << pl.getPosition().y << std::endl;
  34.  
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement