Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / 07-Practice-05.04.2022 / GameCase.cpp
blob: e0337a94bbabf32d9034730de3b38e1b35e22bfe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "GameCase.h"

/* Private */

void GameCase::copyFrom(const GameCase& other) {
	this->command = other.command;
	this->description = new char[strlen(other.description) + 1];
	strcpy(this->description, other.description);
	this->func = other.func;
}

void GameCase::free() {
	delete[] description;
}

/* Public */

GameCase::GameCase() : GameCase('\0', nullptr, nullptr)
{}

GameCase::GameCase(char command, const char* description, bool func(double&)) {
	this->command = command;
	this->func = func;

	if (description == nullptr)
		return;
	
	int descLen = strlen(description);
	this->description = new char[descLen + 1];
	strcpy(this->description, description);
}

GameCase::GameCase(const GameCase& other) {
	this->copyFrom(other);
}

GameCase& GameCase::operator=(const GameCase& other) {
	if (this != &other) {
		this->free();
		this->copyFrom(other);
	}
	return *this;
}

GameCase::~GameCase() {
	this->free();
}

char GameCase::get_comm() const {
	return this->command;
}

void GameCase::print() const {
	std::cout << this->command << " - " << this->description << std::endl;
}

bool GameCase::exec(double& bet) const {
	return this->func(bet);
}