Меню для проверки всех методов Память для поля класса выделяется динам

  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
//1)Меню для проверки всех методов+
//2)Память для поля класса выделяется динамически+
//3)Освобождать память в деструкторе+
//4)Конструктор по умолчанию+
//5)Конструктор копирования+
//6)Перегрузка присваивания+
//7)Перегрузка оператора ввода и вывода+
//8)Метода не меняющие полей сделать константными+
//9)Неизменяемые аргументы сделать константными+
//std::cout << "It's time for destructor!" <<std::endl;
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <vector>
#include <math.h>
class student {
private :
std::string name;
std::string second_name;
std::string last_name;
int age;
int idStudent;
double average;
public :
student(std::string name, std::string second_name, std::string last_name, int age, int idStudent, double average) {
this->name = name;
this->second_name = second_name;
this->last_name = last_name;
this->age = age;
this->idStudent = idStudent;
this->average = average;
}
student() {
name = "";
second_name = "";
last_name = "";
age = 0;
idStudent = 0;
average = 0.;
}
std::string getName(){
return name;
}
std::string getLastName(){
return last_name;
}
std::string getSecondName(){
return second_name;
}
int getAge(){
return age;
}
int getIdStudent(){
return idStudent;
}
double getAverage(){
return average;
}
void setName(std::string n){
name = n;
}
void setSecondName(std::string s){
second_name = s;
}
void setLastName(std::string l){
last_name = l;
}
void setAge(int ag){
age =ag;
}
void setAverage(double aver){
average = aver;
}
void setId(int id){
idStudent = id;
}
void toString(){
std::cout << "Second name:" << second_name << "\nName:" << name << "\nLast name:"
<< last_name << "\nAge:" << age << "\nNumber of record book:" <<idStudent << "\nAverage: "
<< average << "\n" << std::endl;
}
friend std::istream & operator >> (std::istream & os, student& ob){
std::cout <<"Enter name:\n";
os >> ob.name;
ob.setName(ob.name);
std::cout <<"Enter second name:\n";
os >> ob.second_name;
ob.setSecondName(ob.second_name);
std::cout <<"Enter last name:\n";
os >> ob.last_name;
ob.setLastName(ob.last_name);
std::cout <<"Enter age:\n";
os >> ob.age;
ob.setAge(abs(ob.age));
std::cout <<"Enter number of record book:\n";
os >> ob.idStudent;
ob.setId(abs(ob.idStudent));
std::cout <<"Enter average:\n";
os >> ob.average;
ob.setAverage(abs(ob.average));
return os;
}
friend std::ostream & operator << (std::ostream & os, student& ob){
os << "Name:";
os << ob.name;
os << "\nSecond name:";
os << ob.second_name;
os << "\nLast name:";
os << ob.last_name;
os << "\nAge:";
os << ob.age;
os << "\nNumber of record book:";
os << ob.idStudent;
os << "\nAverage:";
os << ob.average;
os << std::endl;
os << std::endl;
return os;
}
student& operator = (student &a){
name = a.name;
second_name = a.second_name;
last_name = a.last_name;
age = a.age;
idStudent = a.idStudent;
average = a.average;
return *this;
}
~student(){}
};
class Student_Group{
private:
std::vector <student> mn;
public:
Student_Group(std::vector <student > mn){
this->mn = mn;
}
void show(){
std::vector <student> :: iterator i;
if (mn.size() > 0)
for (i = mn.begin(); i < mn.end(); i++)
std::cout<<*i;
else std::cout << "Group is empty!" << std::endl;
}
void SearchBySecondName(std::string secondName){
int k = 0;
std::cout << "Result for searching by second name:" << std::endl;
std::vector <student> :: iterator i;
for (i = mn.begin(); i < mn.end(); i++){
if ((*i).getSecondName() == secondName){
std::cout << *i << std::endl;
k = 1;
}
else k = 0;
}
if ( k == 0)
std::cout << "There is no such element!" << std::endl;
std::cout << std::endl;
}
int Size(){
return mn.size();
}
void SearchById(int id){
int k = 0;
std::cout << "Result for searching by number of record book:" << std::endl;
std::vector <student> :: iterator i;
for (i = mn.begin(); i < mn.end(); i++){
if ((*i).getIdStudent() == id){
std::cout << *i << std::endl;
k = 1;
}
else k = 0;
}
if ( k == 0 )
std::cout << "There is no such element!" << std::endl;
std::cout << std::endl;
}
Student_Group (const Student_Group &a){
int i = 0;
this->mn = a.mn;
std::cout << "Constructor copied!" << std::endl;
}
Student_Group & operator = (Student_Group &a){
mn = a.mn;
return *this;
}
void AddElement(){
student element;
std::cin >> element;
mn.push_back(element);
}
void DeleteElement(){
if (mn.size() > 1){
int in = 1;
std::vector <student> :: iterator i;
std::cout << "Choose id of student: " << std::endl;
for (i = mn.begin(); i < mn.end(); i++){
std::cout << "Id of student number " << in << ": " << (*i).getIdStudent() << std::endl;
in++;
}
int id;
std::cout << "Enter id of student" << std::endl;
std::cin >> id;
id = abs(id);
for (i = mn.begin(); i < mn.end();){
if ((*i).getIdStudent() == id)
i = mn.erase(i);
else
++i;
}
}
else mn.clear();
}
void Clear(){
mn.clear();
}
~Student_Group(){ mn.clear();}
};
int main(){
student *ms[3];
ms[0] = new student("Artem","Antonov","Aleekseevich", 20, 12223, 4.2);
ms[1] = new student("Anton","Sergeyev","Antonovich", 20, 12332, 4.2);
ms[2] = new student("Vladimir","Orlov","Vlasov", 18, 12102, 4.3);
std::vector <student > mn;
for ( int i = 0; i < 3; i++)
mn.push_back(*ms[i]);
Student_Group ob(mn);
std::string name;
int id = 0;
int menu;
std::cout << "Chose an option:" << std::endl;
std::cout << "(1) " << " Show all group" << std::endl;
std::cout << "(2) " << " Add student" << std::endl;
std::cout << "(3) " << " Delete student" << std::endl;
std::cout << "(4) " << " Search student by second name" << std::endl;
std::cout << "(5) " << " Search student by number of record book" << std::endl;
std::cout << "(6) " << " Delete all group" << std::endl;
std::cout << "(0) " << " Finish the job" << std::endl;
do{
std::cout << "Enter comand:" << std::endl;
std::cin >> menu;
menu = abs(menu);
if (menu == 1) ob.show();
else
if (menu == 2) {
ob.AddElement();
}
else
if (menu == 3) ob.DeleteElement();
else if( menu == 4){
std::cout << "Enter second name for search:" << std::endl;
std::cin >> name;
ob.SearchBySecondName(name);
}
else if( menu == 5) {
std::cout << "Enter number of record book of student for search:" << std::endl;
std::cin >> id;
id = abs(id);
ob.SearchById(id);
}
else if( menu == 6) ob.Clear();
}while (menu != 0);
std::cout << "Good bye!" << std::endl;
}