Making a myString class
Nov 4, 2014 at 9:34pm UTC
Hi, I'm creating a myString class. I can't get the reverse, = operator, and + operator to work. I posted the .h and the .cpp file below.
Problem with reverse:
My reverse function is not assigning buf[i] to temp[j] in my for loop
Problem with = operator:
this works!
1 2 3 4 5
String b("I am b" );
String a = b;
cout << "***A: " << a << endl;
however this doesn't, abort gets called
1 2 3
String c;
c = b;
cout << c << endl;
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
#include<iostream>
using namespace std;
class String
{
public :
String(const char * s = "" );//
String(const String & s);//
String operator = (const String & s);//
char & operator [] (int index);//
int size();//
String reverse(); // NEEDS WORK
int indexOf(char c);//
int indexOf(String pattern); // do later
bool operator == (String & s);//
bool operator != (String & s);//
bool operator > (String & s);//
bool operator < (String & s);//
bool operator <= (String & s);//
bool operator >= (String & s);//
/// concatenates this and s to return result
String operator + (String & s);
/// concatenates s onto end of this
String operator += (String & s);
void print(ostream & out);//
void read(istream & in);//
~String();
private :
bool inBounds(int i)
{
return i >= 0 && i < len;
}
char * buf;
int len;
};
ostream & operator << (ostream & out, String & str);
istream & operator >> (istream & in, String & str);
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
#include"myString.h"
String::String(const char * s)
:len(strlen(s) + 1), buf(new char ){
strcpy_s(buf, len, s);
}
String::String(const String & s)
: len(s.len), buf(new char ){
strcpy_s(buf, len, s.buf);
}
String String::operator = (const String &s)
{
return String(s);
}
char & String::operator [](int index){
if (inBounds(index))
return buf[index];
else
return buf[len];
}
int String::size(){ return len-1; }
String String::reverse()
{
char *temp = new char [len];
int j = 0;
for (int i = len - 1; i >= 0; --i)
temp[j++] = buf[i];
temp[j] = '\0' ;
return String(temp);
}
int String::indexOf(char c){
for (int i = 0; i < len - 1; i++){
if (buf[i] == c)
return i;
}
return -1;
}
void String::print(ostream & out){
out << buf;
}
void String::read(istream & in){
in >> buf;
}
String::~String(){
cout << buf << " " ;
delete [] buf;
cout << "destructed" << endl;
}
ostream & operator << (ostream & out, String & str){
str.print(out);
return out;
}
istream & operator >> (istream & in, String &str){
str.read(in);
return in;
}
int main(){
String a("I am a" );
cout << a << endl;
cout << "Size is " << a.size() << endl;
cout << a.reverse() << endl;
String b(a);
cout << b << endl;
cout << a.indexOf('a' ) << endl;
String c = b;
cout << c << endl;
system("PAUSE" );
return 0;
}
Last edited on Nov 4, 2014 at 10:30pm UTC
Nov 5, 2014 at 12:31am UTC
Operator=() should replace the current value of the string with the new value. It should return *this. Your operator=() just returns a copy of the right hand side of the statement.
There are other problems:
String::String(const char * s)
and String::String(const String & s)
allocate only 1 byte. You need to allocate enough room in buf for the entire string.
The read()
method assumes that buf already has enough room and it doesn't update len
Topic archived. No new replies allowed.