Skip to content

Commit

Permalink
Примери за работа със статични компоненти
Browse files Browse the repository at this point in the history
  • Loading branch information
triffon committed Apr 20, 2022
1 parent 4ef1e64 commit 23b4e28
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lectures/1/player/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "player.hpp"

void anonymousPrint(Player p) {
std::clog << "anonymousPrint: В момента имаме " << Player::getPlayerCount() << " играчи" << std::endl;
p.print();
p.setName("<анонимен>");
p.print();
Expand Down Expand Up @@ -32,6 +33,8 @@ void testCopyPlayer() {

anonymousPrint("Проба");
createPlayer().print();

std::clog << "В момента имаме " << Player::getPlayerCount() << " играчи" << std::endl;
}

void testArrayPlayer() {
Expand Down
9 changes: 9 additions & 0 deletions lectures/1/player/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
#include <iostream>
#include "player.hpp"

unsigned Player::playerCount = 0;

Player::Player(char const* _name, unsigned _score)
: name(nullptr), score(_score) {
playerCount++;
setName(_name);
}

Player::Player(Player const& other) : name(nullptr), score(other.score) {
playerCount++;
setName(other.name);
}

Player::~Player() {
playerCount--;
std::clog << "~Player()\n";
delete[] name;
}
Expand All @@ -34,4 +39,8 @@ Player& Player::operator=(Player const& other) {
score = other.score;
}
return *this;
}

unsigned Player::getPlayerCount() {
return playerCount;
}
5 changes: 5 additions & 0 deletions lectures/1/player/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class Player {
//char name[MAXNAME];
char* name;
unsigned score;

static unsigned playerCount;

public:

Player(const char* _name = "<анонимен>", unsigned _score = 0);
Expand All @@ -20,6 +23,8 @@ class Player {
void setName(char const* _name);

void print() const;

static unsigned getPlayerCount();// { return playerCount; }
};

#endif

0 comments on commit 23b4e28

Please sign in to comment.