T
Tony Johansson
Hello!!
I have done some operator overloading but my main testprogram doesn't work
well.
Have you any idea which of my methods are wrong?
#include <iostream>
#include <string>
using namespace std;
class String
{
friend ostream& operator<<(ostream& os, const String& s);
public:
/*explicit*/ String(const char* s);
String(const String& s);
~String() { if(buffer) delete[] buffer; buffer = 0; }
String& operator=(const String& s);
int operator==(const String& s) const; // bool
int operator!=(const String& s) const; // bool
String operator+(const String& s);
String& operator+=(const String& s);
private:
char* buffer;
int size;
};
ostream& operator<<(ostream& os, const String& s)
{
os << s.buffer;
return os;
}
String::String(const char* s)
{
size = strlen(s);
buffer = new char[size+1];
strcpy(buffer, s);
}
String::String(const String& s)
{
size = s.size;
buffer = new char[size+1];
strcpy(buffer, s.buffer);
}
String& String:
perator=(const String& s)
{
if (buffer)
delete [] buffer;
buffer = new char[strlen(s.buffer)+1];
strcpy(buffer, s.buffer);
return *this;
}
int String:
perator==(const String& s) const
{
if (strcmp(buffer, s.buffer) == 0)
return 1;
else
return 0;
}
int String:
perator!=(const String& s) const
{
if (strcmp(buffer, s.buffer))
return 1;
else
return 0;
}
String String:
perator+(const String& s)
{
char* str = new char[strlen(buffer) + strlen(s.buffer) + 2];
String local(str);
strcpy(local.buffer, "");
strcat(local.buffer, buffer);
strcat(local.buffer, s.buffer);
return local;
}
String& String:
perator+=(const String& s)
{
char* str = new char[strlen(buffer) + strlen(s.buffer) + 1];
String local(str);
strcpy(local.buffer, "");
strcat(local.buffer, buffer);
strcat(local.buffer, s.buffer);
delete [] buffer;
buffer = local.buffer;
size = local.size;
return *this;
}
int main()
{
String s1("Hej");
String s2(" du glade");
String s3(" ta en spade");
String s4(s1 + s2 + s3);
String s5("Test ");
String s6(" av program");
s5 += s6;
cout << s1 << endl << s2 << endl << s3 << endl
<< s4 << endl << s5 << endl << s6 << endl;
return 0;
}
Many thanks
//Tony
I have done some operator overloading but my main testprogram doesn't work
well.
Have you any idea which of my methods are wrong?
#include <iostream>
#include <string>
using namespace std;
class String
{
friend ostream& operator<<(ostream& os, const String& s);
public:
/*explicit*/ String(const char* s);
String(const String& s);
~String() { if(buffer) delete[] buffer; buffer = 0; }
String& operator=(const String& s);
int operator==(const String& s) const; // bool
int operator!=(const String& s) const; // bool
String operator+(const String& s);
String& operator+=(const String& s);
private:
char* buffer;
int size;
};
ostream& operator<<(ostream& os, const String& s)
{
os << s.buffer;
return os;
}
String::String(const char* s)
{
size = strlen(s);
buffer = new char[size+1];
strcpy(buffer, s);
}
String::String(const String& s)
{
size = s.size;
buffer = new char[size+1];
strcpy(buffer, s.buffer);
}
String& String:
{
if (buffer)
delete [] buffer;
buffer = new char[strlen(s.buffer)+1];
strcpy(buffer, s.buffer);
return *this;
}
int String:
{
if (strcmp(buffer, s.buffer) == 0)
return 1;
else
return 0;
}
int String:
{
if (strcmp(buffer, s.buffer))
return 1;
else
return 0;
}
String String:
{
char* str = new char[strlen(buffer) + strlen(s.buffer) + 2];
String local(str);
strcpy(local.buffer, "");
strcat(local.buffer, buffer);
strcat(local.buffer, s.buffer);
return local;
}
String& String:
{
char* str = new char[strlen(buffer) + strlen(s.buffer) + 1];
String local(str);
strcpy(local.buffer, "");
strcat(local.buffer, buffer);
strcat(local.buffer, s.buffer);
delete [] buffer;
buffer = local.buffer;
size = local.size;
return *this;
}
int main()
{
String s1("Hej");
String s2(" du glade");
String s3(" ta en spade");
String s4(s1 + s2 + s3);
String s5("Test ");
String s6(" av program");
s5 += s6;
cout << s1 << endl << s2 << endl << s3 << endl
<< s4 << endl << s5 << endl << s6 << endl;
return 0;
}
Many thanks
//Tony