-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.cpp
More file actions
106 lines (84 loc) · 2.47 KB
/
Copy pathstring.cpp
File metadata and controls
106 lines (84 loc) · 2.47 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
// string.cpp dinamikus sztringkezelő osztály (definíciók) - SAXHSH
#include "string.h"
#include "memtrace.h"
#include <iostream>
#include <cstddef>
#include <cstring>
/* Konstruktorok */
// default és karakter paraméteres a headerben
String::String(char const *str) :len(strlen(str)), pData(new char[len+1]) {
strcpy(pData, str);
}
String::String(const String& rhs) :len(rhs.len), pData(new char[len+1]) {
strcpy(pData, rhs.pData);
}
// Destruktor és getterek a headerben
/* Felüldefiniált operátorok */
String& String::operator=(const String& rhs) {
if (this != &rhs) {
delete[] pData;
len = rhs.len;
pData = new char[len+1];
strcpy(pData, rhs.pData);
}
return *this;
}
String String::operator+(const String& rhs) const {
String newStr;
newStr.len = len + rhs.len;
delete[] newStr.pData;
newStr.pData = new char[newStr.len + 1];
strcpy(newStr.pData, pData);
strcat(newStr.pData, rhs.pData);
return newStr;
}
String String::operator+(char c) const {
String newStr;
newStr.len = len + 1;
delete[] newStr.pData;
newStr.pData = new char[newStr.len + 1];
strcpy(newStr.pData, pData);
newStr.pData[len] = c;
newStr.pData[len + 1] = '\0';
return newStr;
}
String& String::operator+=(const String& rhs) {
return (*this = *this + rhs);
}
String& String::operator+=(char c) {
return (*this = *this + c);
}
char& String::operator[](int i) {
if (i < 0 || i >= static_cast<int>(len)) throw std::out_of_range ("Túlindexelted a sztringet!");
return pData[i];
}
char String::operator[](int i) const {
if (i < 0 || i >= static_cast<int>(len)) throw std::out_of_range ("Túlindexelted a sztringet!");
return pData[i];
}
/* Logikai operátorok */
bool String::operator>(const String& rhs) const {
return (strcmp(pData, rhs.pData) > 0);
}
bool String::operator<(const String& rhs) const {
return (strcmp(pData, rhs.pData) < 0);
}
bool String::operator==(const String& rhs) const {
return (strcmp(pData, rhs.pData) == 0);
}
bool String::operator!=(const String& rhs) const {
return (strcmp(pData, rhs.pData) != 0);
}
/* Stream operátorok */
std::ostream& operator<<(std::ostream& os, const String& rhs) {
os << rhs.c_str();
return os;
}
std::istream& operator>>(std::istream& is, String& rhs) {
char c; // szimpla karakter buffer
while(is.get(c)) {
if (c == '\n') break; // Enter esetén megáll
rhs += c;
}
return is;
}