Assignment - Continue the implementation of the String class. Add each of the following:
a. A constructor String (int n, char c) that initializes the string with n copies of the character c.
b. The + operator to perform concatenation of two String objects.
c. A member function compare(String) that returns -1, 0, or 1 depending upon whether the string is lexicographically less than, equal to, or greater than the argument. Then, using this member function, provide definitions for the comparison operators <, , and >=.
d. A function resize (int n, char c) that changes the size of the string to n, either truncating characters from the end, or inserting new copies of character c.
e. The function call operator, so that s(int start, int length) returns a substring starting at the given position of the given size.
Here is what I have so far (but my code seems to be broken):
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#include <iostream>
#include <string>
using namespace std;
//base class
class String{
public:
String(int n, char c);
void resize(int n, char c);
void s(int start, int length);
friend String operator+(String);
int length() const;
char operator[](int index) const;
char& operator[](int index);
int compare(String);
private:
char *buffer;
int len;
};
/*
gets the size of the string
@return the length of the string
*/
int String::length() const{
return len;
}
/*
define overloaded subscript operator[]
@return the index of the sting
*/
char String::operator[](int index) const{
return buffer[index];
}
/*
define overloaded subscript operator[]
@return the index of the sting
*/
char& String::operator[](int index){
return buffer[index];
}
/*
define overloaded stream output operator
*/
ostream& operator<<(ostream& out, const String& right){
int n = right.length();
for (int i = 0; i < n; i++)
out << right[i];
return out;
}
/*
gets the n copies of the char c
@pram n for number of copies
@pram c for character
@return the stirng with n copies of one char
*/
String::String(int n, char c){
len = n;
buffer = new char[len + 1];
for (int i = 0; i < len; i++){
buffer[i] = c;
}
buffer[len] = '\0';
for (int i = 0; i <= len; i++){
cout << buffer[i];
}
}
/*
define overloaded operator+
*/
String operator+(const String& one, const String& two){
int n, l, len;
int j;
char *buffer;
n = one.length();
l = two.length();
len = n + l + 1;
buffer = new char[len + 1];
for (int i = 0; i < n; i++){
buffer[i] = one[i];
}
j = 0;
for (int i = n; i < len; i++){
buffer[i] = two[j];
j++;
}
buffer[len] = '\0';
for (int i = 0; i <= len; i++){
cout << buffer[i];
}
}
/*
compare 2 strings
@return -1, 1 and 0 when string is less than, equal to
and greater than the argument
*/
int String::compare(String S){
char c, d;
char str[150];
int a;
cout << "enter a string less than 50: ";
cin >> str;
len = S.length();
a = strlen(str);
if (a < len)
cout << "string is smaller than the arguement";
if (a == len)
cout << "string is equal to the argument";
if (a != len)
cout << "string is not equal to the argument";
if (a > len)
cout << "string is greater than the argument";
if (a <= len)
cout << "string is greater or equal to the argument";
buffer = new char[len + 1];
for (int i = 0; i < len; i++){
buffer[i] = S[i];
}
if (strlen(str) < len)
return(-1);
if (strlen(str) > len)
return(1);
if (strlen(str)==len)
for (int i = 0; i < len; i++){
c = str[i];
d = buffer[i];
if (c < d)
return(-1);
if (c > d)
return(1);
}
return 0;
}
/*
resize string
@pram n for number of elements
@pram c for char
@return the new string
*/
void String::resize(int n, char c){
char str[150];
cout << "enter a string size not more than 50): ";
cin >> str;
if (n < strlen(str))
for (int i = n; i < strlen(str); i++){
str[i] = '\0';
}
if (n > strlen(str));
for (int i = n; i < strlen(str); i++){
str[i] = c;
}
puts(str);
}
void String::s(int start, int length){
char str[50];
char str1[150];
cout << "enter a string size not mroe than 50";
cin >> str;
int j = 0;
int k = start + length;
for (int i = start; i < k; i++){
str1[j] = str[i];
j++;
}
for (int i = j; i < 50; i++){
str1[j] = '\0';
j++;
}
puts(str1);
}
int main(){
int n = 0;
String a(6, 'c');
String b(6, 'd');
a + b;
n = a.compare(b);
cout << n;
a.resize(10, 'a');
a.s(2, 4);
return 0;
}
|
The error codes I am comming up with are:
warning C4018: '<' : signed/unsigned mismatch (lines 131, 159, 160, 164)
warning C4018: '>' : signed/unsigned mismatch (lines 133, 163)
error C2561: 'operator +' : function must return a value
warning C4390: ';' : empty controlled statement found; is this the intent?
The question:
1) The section about the operator+ is confusing, I just want to overload that function (ie define it)
2) what are thoese warnings? I admit I am rather new to programming but don't think I have seen them before.
Any help would be great!
toonhead85