-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_task.cpp
More file actions
125 lines (105 loc) · 2.92 KB
/
Copy pathstring_task.cpp
File metadata and controls
125 lines (105 loc) · 2.92 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
#include <cstring>
#include <fmt/format.h>
#include <iostream>
#include <sstream>
#include <string>
using std::cin;
using std::cout;
using std::string;
class Student {
int id;
char *name;
char *edu_background;
void copy_name(const char *new_name, std::size_t length) {
if (name != nullptr) {
delete[] name;
}
name = new char[length + 1];
strncpy(name, new_name, length);
name[length] = '\0';
}
void copy_edu_background(const char *new_edu_background, std::size_t length) {
if (edu_background != nullptr) {
delete[] edu_background;
}
edu_background = new char[length + 1];
strncpy(edu_background, new_edu_background, length);
edu_background[length] = '\0';
}
public:
Student() : id(0), name(new char[]{"\0"}), edu_background(new char[]{"\0"}){};
Student(const Student &other_student) {
id = other_student.id;
name = nullptr;
edu_background = nullptr;
copy_name(other_student.name, strlen(other_student.name));
copy_edu_background(other_student.edu_background,
strlen(other_student.edu_background));
}
Student(Student &&) = delete;
~Student() {
if (name != nullptr) {
delete[] name;
}
if (edu_background != nullptr) {
delete[] edu_background;
}
}
friend std::istream &operator>>(std::istream &is, Student &student) {
std::string inputstring{};
std::getline(is, inputstring);
std::istringstream linestream{inputstring};
int input_id{};
linestream >> input_id;
student.id = input_id;
if (!linestream.eof()) {
string input_name{};
linestream >> input_name;
student.copy_name(input_name.c_str(), input_name.length());
}
if (!linestream.eof()) {
string input_edu_background{};
linestream >> input_edu_background;
student.copy_edu_background(input_edu_background.c_str(),
input_edu_background.length());
}
return is;
}
friend std::ostream &operator<<(std::ostream &os, const Student &student) {
return os << fmt::format("{} {} {}\n", student.id, student.name,
student.edu_background);
}
Student &operator=(const Student &other_student) {
id = other_student.id;
if (other_student.name != name) {
copy_name(other_student.name, strlen(other_student.name));
}
if (edu_background != other_student.edu_background) {
copy_edu_background(other_student.edu_background,
strlen(other_student.edu_background));
}
return *this;
}
void SetId(int newid) { id = newid; }
void SetName(const string &newname) {
copy_name(newname.c_str(), newname.length());
}
};
int main() {
Student s1;
cin >> s1;
Student s2(s1);
int id1;
string name1;
cin >> id1 >> name1;
s2.SetId(id1);
s2.SetName(name1);
Student s3;
s3 = s1;
cin >> id1;
s3.SetId(id1);
cout << s1;
cout << s2;
cout << s3;
return 0;
}