-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.cpp
More file actions
88 lines (72 loc) · 1.57 KB
/
Copy pathstring.cpp
File metadata and controls
88 lines (72 loc) · 1.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
#include "string.h"
#include <cstring>
int String::num_strings = 0;
String::String() {
this->len = 1;
this->str = new char[1];
(this->str)[0] = 0;
num_strings++;
}
String::String(const char *s) {
this->len = strlen(s);
this->str = new char[this->len + 1];
strcpy(this->str, s);
num_strings++;
}
String::String(const String &s) {
this->len = s.len;
this->str = new char[this->len + 1];
strcpy(this->str, s.str);
num_strings++;
}
String::~String() {
num_strings--;
delete [] this->str;
}
String & String::operator=(const String &s) {
if(this == &s)
return *this;
delete [] this->str;
this->len = s.len;
this->str = new char[this->len + 1];
strcpy(this->str, s.str);
return *this;
}
String & String::operator=(const char *s) {
delete [] this->str;
this->len = strlen(s);
this->str = new char[this->len + 1];
strcpy(this->str, s);
return *this;
}
char & String::operator[](int i) {
return (this->str)[i];
}
const char & String::operator[](int i) const {
return (this->str)[i];
}
bool operator<(const String &s1, const String &s2) {
return strcmp(s1.str, s2.str) < 0;
}
bool operator>(const String &s1, const String &s2) {
return s2 < s1;
}
bool operator==(const String &s1, const String &s2) {
return strcmp(s1.str, s2.str) == 0;
}
ostream & operator<<(ostream &os, const String &s) {
os << s.str;
return os;
}
istream & operator>>(istream &is, String &s) {
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if(is)
s = temp; // use overload =
while(is && is.get() != '\n')
continue;
return is;
}
int String::HowMany() {
return num_strings;
}