-
Notifications
You must be signed in to change notification settings - Fork 0
/
n-ton-2.cpp
241 lines (200 loc) · 3.35 KB
/
n-ton-2.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <bits/stdc++.h>
using namespace std;
class NTon
{
public:
static void set_N(int n)
{
N = n;
}
static int get_curr_num_of_objects()
{
return curr_num_of_objects;
}
protected: // ensures access in derived classes but not in client
static int N;
static int curr_num_of_objects;
static void disp()
{
cout<<"\n\n_________________________________________________\n";
cout<<"N : "<<N<<'\n';
cout<<"Current Number of Objects : "<<curr_num_of_objects<<'\n';
cout<<"Remaining Number of Objects : " << N - curr_num_of_objects <<'\n';
cout<<"_________________________________________________\n\n\n";
}
NTon() { } //ensures its not instantiable in any of the derived classes and in the client
};
class A : public NTon
{
public:
static A* get_A()
{
if(curr_num_of_objects < N)
{
count_A++;
curr_num_of_objects++;
disp();
return new A();
}
else
{
disp();
throw std::runtime_error("too many objects");
}
}
static int get_count_A()
{
return count_A;
}
virtual void del()
{
count_A--;
curr_num_of_objects--;
disp();
delete(this);
}
virtual ~A()
{
cout<<"A dtor\n";
}
private:
void operator delete( void * ) {}
static int count_A;
};
class B: public A
{
public:
static B* get_B()
{
if(curr_num_of_objects < N)
{
count_B++;
curr_num_of_objects++;
disp();
return new B();
}
else
{
disp();
throw std::runtime_error("too many objects");
}
}
static int get_count_B()
{
return count_B;
}
virtual void del()
{
count_B--;
curr_num_of_objects--;
disp();
delete(this);
}
virtual ~B()
{
cout<<"B dtor\n";
}
private:
void operator delete( void * ) {}
static int count_B;
};
class C: public A
{
public:
static C* get_C()
{
if(curr_num_of_objects < N)
{
count_C++;
curr_num_of_objects++;
disp();
return new C();
}
else
{
disp();
throw std::runtime_error("too many objects");
}
}
static int get_count_C()
{
return count_C;
}
virtual void del()
{
count_C--;
curr_num_of_objects--;
disp();
delete(this);
}
virtual ~C()
{
cout<<"C dtor\n";
}
private:
void operator delete( void * ) {}
static int count_C;
};
class D: public B
{
public:
static D* get_D()
{
if(curr_num_of_objects < N)
{
count_D++;
curr_num_of_objects++;
disp();
return new D();
}
else
{
disp();
throw std::runtime_error("too many objects");
}
}
static int get_count_D()
{
return count_D;
}
virtual void del()
{
count_D--;
curr_num_of_objects--;
disp();
delete(this);
}
~D()
{
cout<<"D dtor\n";
}
private:
void operator delete( void * ) {}
static int count_D;
};
int NTon::N = 0;
int NTon::curr_num_of_objects = 0;
int A::count_A = 0;
int B::count_B = 0;
int C::count_C = 0;
int D::count_D = 0;
int main()
{
NTon::set_N(4);
A* a1 = A::get_A();
A* a2 = A::get_A();
a2->del();
a2 = A::get_A();
a2->del();
A* a3 = B::get_B();
a3->del();
a1->del();
cout<<NTon::get_curr_num_of_objects()<<'\n';
vector<A*> v { A::get_A(), B::get_B(), C::get_C(), D::get_D()};
cout<<NTon::get_curr_num_of_objects()<<'\n';
for_each(begin(v), end(v), [](A* e)
{
e->del();
cout<<"\n\n";
});
}