-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyString.cpp
More file actions
130 lines (103 loc) · 2.11 KB
/
myString.cpp
File metadata and controls
130 lines (103 loc) · 2.11 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
#include "myString.h"
#include <cstring>
#include <iostream>
myString::myString()
{
str = nullptr;
length = 0;
}
myString::myString(const char* str)
{
length = strlen(str);
this->str = new char[length + 1];
for (int i = 0; i < length; i++)
{
this->str[i] = str[i];
}
this->str[length] = '\0';
}
myString::~myString()
{
delete[] this->str;
}
myString::myString(const myString& other) //Конструктор копирования
{
length = strlen(other.str);
this->str = new char[length + 1];
for (int i = 0; i < length; i++)
{
this->str[i] = other.str[i];
}
this->str[length] = '\0';
}
myString::myString(myString&& other) //Конструктор перемещения
{
this->length = other.length;
this->str = other.str;
other.str = nullptr;
}
myString& myString::operator=(const myString& other) //Перегрузка =
{
if (this->str != nullptr)
{
delete[] str;
}
length = strlen(other.str);
this->str = new char[length + 1];
for (int i = 0; i < length; i++)
{
this->str[i] = other.str[i];
}
this->str[length] = '\0';
return *this;
}
myString myString::operator+(const myString& other) //Перегрузка +
{
myString newStr;
int thisLenght = strlen(this->str);
int otherLength = strlen(other.str);
newStr.length = thisLenght + otherLength;
newStr.str = new char[thisLenght + otherLength + 1];
int i = 0;
for (; i < thisLenght; i++)
{
newStr.str[i] = this->str[i];
}
for (int j = 0; j < otherLength; j++, i++)
{
newStr.str[i] = other.str[j];
}
newStr.str[thisLenght + otherLength] = '\0';
return newStr;
}
bool myString::operator==(const myString& other) //Перегрузка ==
{
if (this->length != other.length)
{
return false;
}
for (int i = 0; i < this->length; i++)
{
if (this->str[i] != other.str[i])
{
return false;
}
}
return true;
}
bool myString::operator!=(const myString& other) //Перегрузка !=
{
return !(this->operator==(other));
}
char& myString::operator[](int index)
{
return this->str[index];
}
int myString::Length()
{
return length;
}
void myString::Print()
{
std::cout << str << std::endl;
}