-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.cpp
More file actions
195 lines (113 loc) · 3.57 KB
/
Copy pathclass.cpp
File metadata and controls
195 lines (113 loc) · 3.57 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
#include <iostream>
#include <vector>
using namespace std;
//modificatori di visibilità
// private gli elelemnti non sonon ne visibili all'esterno, ne possono essere ereditati
// public contrario di private ( per convenzione si inizia sempre con questo in C++)
// prtected non sono visibili all'esterno m posssono essere ereditati
class book{ // diviso in 3 aprti
// private:(in genere tutti gli attributi di classe sono privati)
private:
string title;
string author; // classe definita da soli 3 stati privati
int id;
// cosa ce ne facciamo? li dobbimao utilizzare
// in generale si utilizza il costruttore di classe, che permette di inizializzare la classe
// deve essere pubblico.
public:
book(){
this->title = "";
this->author = ""; // costruttore vuoto
this->id = -1;
}
book(string _title, string _author, int _id){
title = _title;
author = _author;
id = _id;
// this->title = title; this restitutisce il riferimenti all'oggetto
}
book(string title, string author, int id) // member initianization list
: title(title) ,author(author), id(id){ // pre costruttore, mi puo servire se ho qualcosa di const
// this->char....
}
// copy csotructor
book(book& b){
cout << " copio" << endl;
}
// move costructor
book(book&& b){
}
// distruttore
~book(){
// nel caso di un char
// delete this->char...
cout << "oggetto distrutto" << endl;
}
// accedere agli attributi
string get_title(){ // nessun argometno perche vogglio solo la ccopia
return title;
}
string get_author(){ // nessun argometno perche vogglio solo la ccopia
return author;
}
int get_id(){ // nessun argometno perche vogglio solo la ccopia
return id;
}
// se voglio modificare
// uso void , non voglio modificare
void set_title(string _title){
title = _title;
}
void set_author(string _author){
author = _author;
}
void set_id(int _id){
id = _id;
}
void print_attributes(){
cout << title << " " << author << " " << id << endl;
}
};
class library{
private:
int id;
int count_book;
int size_book;
book* arr;
public:
library(){
this->id = -1;
this->arr = NULL;
this->count_book = 0;
this->size_book = 0;
}
//library() = delete impediace creazione di librerie vuote
library(int id, size_t size ){
this->id = id;
arr = new book[size];
this->count_book = 0;
this->size_book = size;
}
bool add_book(const book& book){ // passo riferimento non copia
// inserisco libro nell array in modo sicuro
if(this->count_book >= this->size_book)
return false;
this->arr[count_book] = book;
count_book++;
return true;
}
};
int main(){
book b("prog_2", "DMI", 345);
// se voglio llocare nello heap
// bool *b = new book("prog_2", "DMI", 345)
book b2; // defoult constructor
//book b2 ();
cout << b.get_title() << " " << b.get_author() << " " << b.get_id() << endl; // utilizzo di ge_title;
b.set_author("universita di ct");
cout << b.get_author() << endl;
b.print_attributes();
move(b2);
// delete b distruggi v liberandone gli attributi
return 0;
}