-
Notifications
You must be signed in to change notification settings - Fork 3
/
1043_seminar13.cpp
121 lines (105 loc) · 2.38 KB
/
1043_seminar13.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
#include<iostream>
#include<string>
using namespace std;
//int adunare(int a, int b) {
// return a + b;
//}
//float adunare(float a, float b) {
// return a + b;
//}
class Timbru {
int an;
string tara;
float pret;
public:
Timbru() :an(0), tara("Romania"), pret(0) {};
Timbru(int an, string tara, float pret) {
this->an = an;
this->tara = tara;
this->pret = pret;
}
float getPret() {
return pret;
}
operator float() {
return this->pret;
}
Timbru operator+(Timbru t) {
Timbru timbru = *this;
timbru.pret = this->pret + t.pret;
return timbru;
}
};
template <class cl>
class Colectionar
{
cl* colectie;
int nr_elemente;
string nume;
public:
Colectionar(cl* colectie, int nr_elemente, string nume)
{
this->nr_elemente = nr_elemente;
this->colectie = new cl[nr_elemente];
for (int i = 0; i < nr_elemente; i++)
{
this->colectie[i] = colectie[i];
}
this->nume = nume;
}
Colectionar()
{
this->nume = "";
this->nr_elemente = 0;
this->colectie = NULL;
}
~Colectionar()
{
if(this->colectie != NULL)
delete[] this->colectie;
}
Colectionar(const Colectionar &col)
{
this->nr_elemente = col.nr_elemente;
this->nume = col.nume;
this->colectie = new cl[col.nr_elemente];
for (int i = 0; i < col.nr_elemente; i++)
{
this->colectie[i] = col.colectie[i];
}
}
Colectionar operator=(const Colectionar &c) {
this->nume = c.nume;
this->nr_elemente = c.nr_elemente;
if (this->colectie != null) delete[]this->colectie;
this->colectie = new cl[this->nr_elemente];
for (int i = 0; i < this->nr_elemente; i++) {
this->colectie[i] = c.colectie[i];
}
return *this;
}
float calculareValoare() {
float sum = 0;
for (int i = 0; i < this->nr_elemente; i++) {
sum =sum + this->colectie[i];
}
return sum;
}
};
template <class T>
T adunare(T a, T b) {
return a + b;
}
void main() {
std::cout << adunare(1, 2) << std::endl;
std::cout << adunare(1.0f, 2.678f) << endl;
Timbru t1(2019, "Romania", 100);
Timbru t2(2018, "Romania", 20);
Timbru t3 = adunare(t1, t2);
cout << t3.getPret()<<endl;
Colectionar<int> c1;
Colectionar<float> c2(new float[3]{ 4.6f,5.8f,2.7f }, 3, "Popescu");
cout << c2.calculareValoare()<<endl;
Colectionar<Timbru> c3(new Timbru[2]{ t1,t2 }, 2, "Vasilescu");
cout << c3.calculareValoare();
}