-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_creation_skills_unit.pas
More file actions
204 lines (170 loc) · 4.05 KB
/
class_creation_skills_unit.pas
File metadata and controls
204 lines (170 loc) · 4.05 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
{
6 kyu
Train your skills in creation of classes
https://www.codewars.com/kata/5ab4f002379d20e82500008c
}
unit class_creation_skills_unit;
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
interface
type
{ ---- (1) Class Version ----- }
TX = class(TObject)
private
m_a: integer;
m_b: integer;
public
constructor Create(const a, b: integer); // normal create
constructor Create(const a: integer); // create with default b
constructor Create; overload; // create with defaults
constructor Create(const Other: TX); // create & assignment
function CreateClone: TX; // create copy of self
destructor Destroy; override; // destroy
procedure Assign(const a, b: integer); // assignment a,b to self
procedure Assign(const Other: TX); // assignment other to self
procedure Add(const Other: TX); // add other to self
procedure Subtract(const Other: TX); // subtract other from self
procedure Inc(const Count: integer = 1); // increment self by count
procedure Dec(const Count: integer = 1); // decrement self by count
function ToString(): string; override; // self as string
property GetA: integer read m_a; // get a
property GetB: integer read m_b; // get b
procedure SetA(const a: integer); // set a
procedure SetB(const b: integer); // set b
end;
type
{ ---- (2) Record Version ($modeswitch advancedrecords) ----- }
TY = record
private
m_a: integer;
m_b: integer;
public
constructor Create(a, b: integer); overload;
constructor Create(a: integer); overload;
class function Default: TY; static;
class operator +(const L, R: TY): TY;
class operator -(const L, R: TY): TY;
function ToString: string;
function GetA: integer;
function GetB: integer;
procedure SetA(a: integer);
procedure SetB(b: integer);
end;
implementation
{ ---- (1) Class Version ----- }
constructor TX.Create(const a, b: integer);
begin
inherited Create;
m_a := a;
m_b := b;
end;
constructor TX.Create(const a: integer);
begin
Self.Create(a, 2); // delegate
end;
constructor TX.Create;
begin
Self.Create(1, 2); // delegate
end;
constructor TX.Create(const Other: TX);
begin
inherited Create;
Assign(Other);
end;
function TX.CreateClone: TX;
begin
Result := TX.Create(Self);
end;
destructor TX.Destroy;
begin
Writeln('Destroy ', ToString);
inherited Destroy;
end;
procedure TX.Assign(const a, b: integer);
begin
m_a := a;
m_b := b;
end;
procedure TX.Assign(const Other: TX);
begin
m_a := Other.m_a;
m_b := Other.m_b;
end;
procedure TX.Add(const Other: TX);
begin
m_a := m_a + Other.m_a;
m_b := m_b + Other.m_b;
end;
procedure TX.Subtract(const Other: TX);
begin
m_a := m_a - Other.m_a;
m_b := m_b - Other.m_b;
end;
procedure TX.Inc(const Count: integer = 1);
begin
System.Inc(m_a, Count);
System.Inc(m_b, Count);
end;
procedure TX.Dec(const Count: integer = 1);
begin
System.Dec(m_a, Count);
System.Dec(m_b, Count);
end;
function TX.ToString: string;
begin
WriteStr(Result, '[', m_a, ',', m_b, ']');
end;
procedure TX.SetA(const a: integer);
begin
m_a := a;
end;
procedure TX.SetB(const b: integer);
begin
m_b := b;
end;
{ ---- (2) Record Version ($modeswitch advancedrecords) ----- }
constructor TY.Create(a, b: integer);
begin
m_a := a;
m_b := b;
end;
constructor TY.Create(a: integer);
begin
Self.Create(a, 2);
end;
class function TY.Default: TY;
begin
Result.m_a := 1;
Result.m_b := 2;
end;
class operator TY.+(const L, R: TY): TY;
begin
Result.m_a := L.m_a + R.m_a;
Result.m_b := L.m_b + R.m_b;
end;
class operator TY.-(const L, R: TY): TY;
begin
Result.m_a := L.m_a - R.m_a;
Result.m_b := L.m_b - R.m_b;
end;
function TY.ToString: string;
begin
WriteStr(Result, '[', m_a, ',', m_b, ']');
end;
function TY.GetA: integer;
begin
Result := m_a;
end;
function TY.GetB: integer;
begin
Result := m_b;
end;
procedure TY.SetA(a: integer);
begin
m_a := a;
end;
procedure TY.SetB(b: integer);
begin
m_b := b;
end;
end.