-
Notifications
You must be signed in to change notification settings - Fork 3
/
1043_seminar08.cpp
277 lines (233 loc) · 6.2 KB
/
1043_seminar08.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include<iostream>
#include<string>
using namespace std;
class Apartament {
private:
static double tva;
const int id;
const int etaj;
int nrCamere;
float *suprafataCamera;
string adresa;
static int contor;
static int nrApartamenteExistente;
public:
Apartament() :id(contor++), etaj(3) {
nrCamere = 3;
suprafataCamera = new float[nrCamere];
for (int i = 0; i < nrCamere; i++) {
suprafataCamera[i] = 10;
}
adresa = "piata Romana";
nrApartamenteExistente++;
}
Apartament(string adresa, int nrCamere, float* suprafataCamera, int etajPrimit) :id(contor++), etaj(etajPrimit) {
this->nrCamere = nrCamere;
this->suprafataCamera = new float[nrCamere];
for (int i = 0; i < nrCamere; i++) {
this->suprafataCamera[i] = suprafataCamera[i];
}
this->adresa = adresa;
nrApartamenteExistente++;
}
~Apartament() {
delete[]suprafataCamera;
nrApartamenteExistente--;
}
void afisare() {
cout << id << ". In " << adresa << " se afla un apartament cu " << nrCamere << " la etajul "
<< etaj << " cu suprafetele ";
for (int i = 0; i < nrCamere; i++) {
cout << suprafataCamera[i] << ", ";
}
cout << endl;
}
void setAdresa(string nouaAdresa) {
if (nouaAdresa.length()>2)
this->adresa = nouaAdresa;
}
string getAdresa() {
return adresa;
}
void setNrCamere(int nrCamere, float* suprafete) {
this->nrCamere = nrCamere;
delete[]this->suprafataCamera;
this->suprafataCamera = new float[nrCamere];
for (int i = 0; i < nrCamere; i++) {
this->suprafataCamera[i] = suprafete[i];
}
}
int getNrCamere() {
return nrCamere;
}
float* getSuprafeteCamere() {
return this->suprafataCamera;
}
float getSuprafataCameraData(int index) {
if (index < nrCamere && index >= 0) {
return this->suprafataCamera[index];
}
else {
throw "index incorect";
}
}
static void setTVA(double tvaNou) {
tva = tvaNou;
}
static double getTVA() {
return tva;
}
Apartament(const Apartament &a) :id(contor++), etaj(a.etaj) {
this->nrCamere = a.nrCamere;
this->suprafataCamera = new float[nrCamere];
for (int i = 0; i < nrCamere; i++) {
this->suprafataCamera[i] = a.suprafataCamera[i];
}
this->adresa = a.adresa;
nrApartamenteExistente++;
}
Apartament operator=(const Apartament &a) {
this->nrCamere = a.nrCamere;
if (this->suprafataCamera != NULL) {
delete[]this->suprafataCamera;//evit aparitia de memory leaks;
}
this->suprafataCamera = new float[nrCamere];
for (int i = 0; i < nrCamere; i++) {
this->suprafataCamera[i] = a.suprafataCamera[i];
}
this->adresa = a.adresa;
return *this;
}
Apartament operator+(const Apartament &a) {
Apartament b;
b.adresa = this->adresa;
b.nrCamere = this->nrCamere + a.nrCamere;
b.suprafataCamera = new float[b.nrCamere];
for (int i = 0; i < this->nrCamere; i++) {
b.suprafataCamera[i] = this->suprafataCamera[i];
}
for (int i = this->nrCamere; i < b.nrCamere; i++) {
b.suprafataCamera[i] = a.suprafataCamera[i - this->nrCamere];
}
return b;
}
Apartament operator+(int nrCamere) {
Apartament t = *this;
t.nrCamere = this->nrCamere + nrCamere;
delete[]t.suprafataCamera;
t.suprafataCamera = new float[t.nrCamere];
for (int i = 0; i < this->nrCamere; i++) {
t.suprafataCamera[i] = this->suprafataCamera[i];
}
for (int i = this->nrCamere; i < t.nrCamere; i++) {
t.suprafataCamera[i] = 10;
}
return t;
}
float& operator[](int index) {
//return this->getSuprafataCameraData(index);
if (index < nrCamere && index >= 0) {
return this->suprafataCamera[index];
}
else {
throw "index incorect";
}
}
float calculareSUprafataTotala() {
float s = 0;
for (int i = 0; i < this->nrCamere; i++) {
//s += this->suprafataCamera[i];
s += (*this)[i];
}
return s;
}
bool operator>(Apartament a) {
//return this->nrCamere > a.nrCamere;
//float s = 0;
//for (int i = 0; i < this->nrCamere; i++) {
// //s += this->suprafataCamera[i];
// s += (*this)[i];
//}
return this->calculareSUprafataTotala() > a.calculareSUprafataTotala();
}
//preincrementare
Apartament operator++() {
for (int i = 0; i < this->nrCamere; i++) {
this->suprafataCamera[i]++;
}
return *this;
}
//postincrementare
Apartament operator++(int) {
Apartament temporar = *this;
for (int i = 0; i < this->nrCamere; i++) {
this->suprafataCamera[i]++;
}
return temporar;
}
float operator()() {
return calculareSUprafataTotala();
}
float operator()(int index) {
//return (*this)[index];
return getSuprafataCameraData(index);
}
bool operator()(int c1, int c2) {
return this->getSuprafataCameraData(c1) < this->getSuprafataCameraData(c2);
}
explicit operator int() {
return this->nrCamere;
}
explicit operator float() {
return calculareSUprafataTotala();
}
friend ostream& operator<<(ostream& afisar,const Apartament& a);
friend istream& operator>>(istream& citir, Apartament& a) {
cout << "Adresa:";
citir >> a.adresa;
cout << "numarul de camere:";
citir >> a.nrCamere;
delete[]a.suprafataCamera;//evitare memory-leaks
a.suprafataCamera = new float[a.nrCamere];
for (int i = 0; i < a.nrCamere; i++) {
cout << "Suprafata" << i << ":";
citir >> a.suprafataCamera[i];
}
return citir;
}
};
int Apartament::contor = 1;
double Apartament::tva = 19;
int Apartament::nrApartamenteExistente = 0;
void afisareAdresa(Apartament a) {
cout << a.getAdresa();
}
Apartament operator+(int nrCamere, Apartament a) {
Apartament t;
t = a + nrCamere;
return t;
//return a+nrCamere;
}
ostream& operator<<(ostream& afisar, const Apartament& a) {
afisar << a.id << ". In " << a.adresa << " se afla un apartament cu " << a.nrCamere << " la etajul "
<< a.etaj << " cu suprafetele ";
for (int i = 0; i < a.nrCamere; i++) {
afisar << a.suprafataCamera[i] << ", ";
}
afisar << endl;
return afisar;
}
void main() {
Apartament a;
Apartament b;
cout << a[0];
a[0] = 8;
float z = 8.7;
int y = z;
int x = (int)a;
float m = (float)a;
cin >> a;
cout << a << b;
afisareAdresa(a);
a.getAdresa();
}