-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.cpp
63 lines (53 loc) · 1.33 KB
/
State.cpp
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
60
61
62
#include "State.h"
#include "AutomatonException.h"
#include <iostream>
#include <string>
#include <sstream>
State::State(const char* name) : stateName(new char[strlen(name) + 1]) {
strcpy_s(stateName, strlen(name) + 1, name);
}
State::State(const State& rhs) : stateName(new char[strlen(rhs.stateName) + 1]) {
strcpy_s(stateName, strlen(rhs.stateName) + 1, rhs.stateName); // destination, number of el, source
}
State& State::operator=(const State& rhs) {
if (this != &rhs) {
if (stateName != nullptr) {
delete[] stateName;
}
stateName = new char[strlen(rhs.stateName) + 1];
strcpy_s(stateName, strlen(rhs.stateName) + 1, rhs.stateName);
}
return *this;
}
bool State::operator==(const State& rhs) {
if (strcmp(stateName, rhs.getStateName()) == 0)
return true;
return false;
}
int State::setStateName(char* name) {
if (stateName != nullptr) {
delete[] stateName;
}
stateName = new char[strlen(name) + 1];
strcpy_s(stateName, strlen(name) + 1, name);
return 0;
}
char* State::getStateName() const {
return stateName;
}
State::~State() {
if (stateName != nullptr) {
delete[] stateName;
}
}
std::ostream& operator<<(std::ostream& out, const State& rhs) {
out << rhs.getStateName();
return out;
}
std::istream& operator>>(std::istream& lhs, State& rhs)
{
char arr[50];
lhs >> arr;
rhs.setStateName(arr);
return lhs;
}