Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- class Position {
- public:
- Position(int x, int y) : x(x), y(y) {}
- int x;
- int y;
- };
- class Player {
- private:
- Position pos;
- public:
- Player() : pos(0, 0) {}
- // rückgabetyp const da position nicht nachträglich veränderbar
- // heißt wenn du getPosition().x = 13; aufrufst gibt es einen fehler
- const Position &getPosition() const { // funktion const da sie das objekt nicht verändert
- return this->pos;
- }
- void move(const Position &pos) { // funktion nicht const da sie das objekt verändert
- this->pos.x += pos.x;
- this->pos.y += pos.y;
- }
- };
- int main()
- {
- Player pl;
- std::cout << pl.getPosition().y << std::endl;
- pl.move(Position(0, 3));
- std::cout << pl.getPosition().y << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement