-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.h
More file actions
170 lines (128 loc) · 6.1 KB
/
string.h
File metadata and controls
170 lines (128 loc) · 6.1 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
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
#pragma once
/************************************************
DELETE THIS SECTION BEFORE FINAL SUBMISSION
This is a prototype class that should encapsulate
dynamic memory management of a character array.
Its functions should seek to take as much advantage
as possible of the <cstring> standard library.
strcpy, strcat, and strcmp will be especially useful.
The solutions for each of these functions are
about 3-5 lines. Remember, these functions may
be used by each other to greatly reduce the
amount of work required (1 line solutions for many!)
Hint: Try implementing the 'resize' first. If you
keep most all of your dynamic memory management
to a single function, you can reuse it without
worrying about memory!
Note: It will be necessary for this class to
implement the C++ "Rule of 5" AND overload
stream operators for functionality with cin/cout.
Don't expect to solve these without research
or help!
IMPORTANT:
You should modify this header and implement
whatever additional functionality you find useful.
Modify this project until it compiles without error.
When successful, copy the string.h and string.cpp to
your game project.
YOU MAY CHANGE THE NAMESPACE IF YOU WISH,
but fix the test-main.
Final Hint:
If you hover-click on a green squiggly and select
"show potential fixes," one of them says
"Create definition in <filename>.cpp". You may use this
to automatically generate the function definitions
in the source file.
**************************************************/
/*************************************************
<FILL OUT THIS PORTION AND LEAVE IT IN YOUR FILE>
NAME OF FILE (ie. string.h)
DESCRIPTION OF WHAT THIS FILE DOES,
WHAT'S IN IT, AND HOW IT SHOULD WORK!
DUPLICATE THIS FOR THE SOURCE FILE.
Esmeralda Salamone, esmes@aie.edu.au, 9/23/2015
Modified: STUDENT NAME, EMAIL, DATE OF LAST EDIT
All rights reserved.
***************************************************/
// This header forward declares ostream and istream
// iostream should still be included in the source file
#include <iosfwd>
/*
The following comments describe implementation details
for each function. These comments are hints to you-
THEY SHOULD BE REMOVED and each function should have
a short comment describing what the function does.
*/
namespace sfw
{
class string
{
private:
char *m_data; // Pointer to a dynamically allocated char array
size_t m_size; // How much data can our array currently store?
public:
string(void); // Should set m_data and m_size to meaningful defaults
string(size_t size); // create an empty string with a given amount of memory.
string(const char *a, size_t size);
// duplicate data in character array, and set our size to size.
~string(); // should free (delete) dynamic memory if any is still in use (rule of 5).
// Constructors, use strcpy
string(const char *a); // should duplicate the data of the character array
string(const string &a); // should duplicate the data of the other string (rule of 5).
string(string &&a); // should 'steal' the dynamic memory from the other string (rule of 5).
// Assignment operators, use strcpy
string &operator=(const string &a); // should duplicate the data of the other string (rule of 5).
string &operator=(string &&a); // should 'steal' the dynamic memory from the other string (rule of 5).
string &operator=(const char *a); // should duplicate the data of the character array
// Concatenation assignment (resize and use strcat())
string &operator+=(const string &a);
string &operator+=(const char *a);
string &operator+=(char a);
// Array indexing
char &operator[](size_t index); // write access to an indexed character
const char &operator[](size_t index) const; // read-only access to an indexed character
size_t length() const; // return the length of the string (strlen)
size_t size() const; // return the max number of chars we could hold (m_size-1, for term char)
void resize(size_t size); // create a new array of size, strcpy old data, then delete old data
void clear(); // set the first character to '\0'
bool empty() const; // returns true if the first character is '\0'
const char *cstring() const; // constant access to m_data
};
// Relational operator overloads- use strcmp()
// less-than is true if alphabetical-ascending
bool operator<(const string &a, const string &b);
bool operator<(const string &a, const char *b);
bool operator<(const char *a, const string &b);
bool operator<=(const string &a, const string &b);
bool operator<=(const string &a, const char *b);
bool operator<=(const char *a, const string &b);
// greater-than is true if alphabetical-descending
bool operator>(const string &a, const string &b);
bool operator>(const string &a, const char *b);
bool operator>(const char *a, const string &b);
bool operator>=(const string &a, const string &b);
bool operator>=(const string &a, const char *b);
bool operator>=(const char *a, const string &b);
bool operator!=(const string &a, const string &b);
bool operator!=(const string &a, const char *b);
bool operator!=(const char *a, const string &b);
bool operator==(const string &a, const string &b);
bool operator==(const string &a, const char *b);
bool operator==(const char *a, const string &b);
// Concatenation operations- so many possible combinations.
// should create a new string that is the concatenation of the two passed in.
// Should support single characters also.
string operator+(const string &a, const string &b);
string operator+(const string &a, const char *b);
string operator+(const char *a, const string &b);
string operator+(const string &a, char b);
string operator+(char a, const string &b);
// Literal operator overload, use given char data to create a string
// ie, "I AM A STRING"_sfw; should create a string /class/ literal!
inline namespace literals {
const string operator""_sfw(const char *a, size_t len); }
// Allows us to work with stream processors (cin/cout)
// they can be a little tricky! Don't be afraid to do research.
std::ostream &operator<<(std::ostream &os, const string& p); //output stream operator
std::istream &operator>>(std::istream &is, string& p); //input stream operator
}