-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimal.cpp
More file actions
115 lines (111 loc) · 2.6 KB
/
Animal.cpp
File metadata and controls
115 lines (111 loc) · 2.6 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "Animal.h"
#define SIZE 50
Animal::Animal()
{
m_color = NULL;
m_childCount = 0;
m_avgLifetime = 0;
}
Animal::Animal(const char* color, int childs, float avgLifetime) : m_childCount(childs), m_avgLifetime(avgLifetime)
{
m_color = new char[strlen(color) + 1];
strcpy(m_color, color);
}
Animal::Animal(ifstream& in_file)
{
int n;
in_file.read((char*)&n, sizeof(n));
m_color = new char[n + 1];
in_file.read(m_color, n);
m_color[n] = '\0';
in_file.read((char*)&m_childCount, sizeof(m_childCount));
in_file.read((char*)&m_avgLifetime, sizeof(m_avgLifetime));
}
void Animal::Save(ofstream& ofs) const
{
ofs << m_color << endl;
ofs << m_childCount << endl;
ofs << m_avgLifetime << endl;
}
void Animal::Load(ifstream& ifs)
{
char buffer[SIZE];
ifs.getline(buffer, SIZE);
m_color = new char[strlen(buffer)];
strcpy(m_color, buffer);
ifs >> m_childCount;
ifs >> m_avgLifetime;
}
void Animal::SaveBin(ofstream& ofs) const
{
int n = strlen(m_color);
ofs.write((char*)&n, sizeof(n));
ofs.write(m_color, n);
ofs.write((char*)&m_childCount, sizeof(m_childCount));
ofs.write((char*)&m_avgLifetime, sizeof(m_avgLifetime));
}
void Animal::LoadBin(ifstream& ifs) const
{
int n;
ifs.read((char*)&n, sizeof(n));
m_color = new char[n + 1];
ifs.read(m_color, n);
m_color[n] = '\0';
ifs.read((char*)&m_childCount, sizeof(m_childCount));
ifs.read((char*)&m_avgLifetime, sizeof(m_avgLifetime));
}
Animal::~Animal()
{
delete[] m_color;
}
const char* Animal::GetColor() const
{
return m_color;
}
int Animal::GetChildCount() const
{
return m_childCount;
}
float Animal::GetLifetime() const
{
return m_avgLifetime;
}
void Animal::Save(ofstream& ofs) const
{
ofs << m_color << endl;
ofs << m_childCount << endl;
ofs << m_avgLifetime << endl;
}
void Animal::Load(ifstream& ifs)
{
char buffer[SIZE];
ifs.getline(buffer, SIZE);
m_color = new char[strlen(buffer)];
strcpy(m_color, buffer);
ifs >> m_childCount;
ifs >> m_avgLifetime;
}
void Animal::LoadBin(ifstream& ifs) const
{
int n;
ifs.read((char*)&n, sizeof(n));
m_color = new char[n + 1];
ifs.read(m_color, n);
m_color[n] = '\0';
ifs.read((char*)&m_childCount, sizeof(m_childCount));
ifs.read((char*)&m_avgLifetime, sizeof(m_avgLifetime));
}
void Animal::SaveTypeBin(ofstream& out_file) const
{
int n = strlen(GetTypeName());
out_file.write((char*)&n, sizeof(n));
out_file.write(GetTypeName(), n);
}
void Animal::SaveType(ofstream& out_file) const
{
out_file << GetTypeName();
}
char* Animal::GetTypeName() const
{
return (char*)typeid(*this).name() + 6;
}