|
-
March 31st, 2004, 02:42 PM
#1
Some operators in Class String
Please tell me what's wrong with that code.
I have problems with "operator+=" and because of it with "operator+" also.
Besides, please take a look at "operator<<" and "operator>>" and correct me if i have wrote in wrong.
I also have a problem with "operator[]" when i use that operator in the left side. Why is that?
Thank you for your very usefull help.
Header file:
Code:
#ifndef STRING_H
#define STRING_H
#include <iostream>
using namespace std;
class String
{
char* value;
private:
virtual void show(ostream&)const;
void CopyValue(const String& copied);
public:
String(void);
String(const char*);
String(const String&);
~String(void);
String& operator= (const String&);
size_t length() const;
//String& trim (void);
char& operator[] (int);
char operator[] (int) const;
//String& operator+= (const String&);
//friend String operator+ (const String&) const;
friend istream& operator>> (istream&, const String&);
friend ostream& operator<< (ostream&, const String&);
friend bool operator== (const String&, const String&);
friend bool operator!= (const String&, const String&);
};
#endif
Cpp file:
Code:
#include "String.h"
#include <iostream>
#include <cstring>
using namespace std;
String::String(void)
{
value = new char [1];
value[0] = '\0';
}
String::String(const char* data): value(NULL)
{
if (data)
{
value = new char [strlen(data) + 1];
strcpy(value,data);
}
else
{
value = new char[1];
value[0] = '\0';
}
}
String::~String(void)
{
delete [] value;
}
String::String(const String& copied): value(NULL)
{
//value = new char [strlen(copied.value) + 1];
//strcpy(value, copied.value);
this -> CopyValue(copied);
}
String& String:: operator= (const String& copied)
{
//delete [] value;
//value = new char [strlen(copied.value) + 1];
//strcpy(value, copied.value);
if (&copied != this)
this -> CopyValue(copied);
return *this;
}
void String:: CopyValue(const String& copied)
{
delete [] value;
value = NULL;
if (copied.value)
{
value = new char [strlen(copied.value) + 1];
strcpy(value, copied.value);
}
}
void String:: show(ostream& out)const
{
if (value)
out << value << endl;
}
bool operator== (const String& first, const String& second )
{
if (strcmp(first.value, second.value) == 0)
return true;
return false;
}
bool operator!= (const String& first, const String& second)
{
if (first == second)
return false;
return true;
}
ostream& operator<< (ostream& out, const String& obj)
{
obj.show(out);
return out;
}
istream& operator>> (istream& in, const String& obj)
{
in >> obj.value;
return in;
}
size_t String:: length() const
{
return strlen(value) + (size_t)1;
}
/*String& String:: operator+= (const String& rhs)
{
if (this != &rhs)
strcat(value, rhs.value);
return *this;
}
String operator+ (const String& rhs) const
{
return String(*this) += rhs;
}*/
char& String:: operator[] (int place)
{
return value [place];
}
char String:: operator[] (int place) const
{
return value [place];
}
main:
Code:
#include "String.h"
#include <iostream>
using namespace std;
int main (void)
{
String a;
const String b("LINDA IS MY DOG");
String c(b);
cout << b;
//b.show(); BEFORE I HAVE WROTE COUT
cout << c;
//c.show(); BEFORE I HAVE WROTE COUT
cout << a;
//a.show(); BEFORE I HAVE WROTE COUT
if (a == b)
cout << "A and B are the same" << endl;
if (a != b)
cout << "A and B are absolutly not the same" << endl;
a = b;
cout << a;
//a.show(); BEFORE I HAVE WROTE COUT
if (a == b)
cout << "A and B are the same" << endl;
if (a != b)
cout << "A and B are absolutly not the same" << endl;
cout << b.length() << endl;
//b [6] = 'k'; ERROR!!
//b += c; ERROR!!
cout << b;
cout << b [6] << endl;
String d("Meyers is my friend");
cin >> d;
cout << d;
return 0;
}
Last edited by Begins; March 31st, 2004 at 02:49 PM.
-
March 31st, 2004, 03:24 PM
#2
Well...just out of curiosity...why are you implementing yet another string class?
-
March 31st, 2004, 03:36 PM
#3
operator +=
----------------
think why you can't use 'strcat' the way you've
implemented operator +=
...
use the condition:
if(*this != rhs)
and not:
if(this != &rhs)
------------------------
change the prototype of the const version operator[] to :
const char& String:: operator[] (int place) const
**** **** **** **** **/**
-
March 31st, 2004, 04:10 PM
#4
This looks like a school assignment, otherwise std::string already does this work for you.
Another thing implement != in terms of ==.
Code:
bool operator== (const String& first, const String& second )
{
if (strcmp(first.value, second.value) == 0)
return true;
return false;
}
bool operator!= (const String& first, const String& second)
{
return !(first == second);
}
There is no need to write more code for !=.
Regards,
Paul McKenzie
-
March 31st, 2004, 10:28 PM
#5
Re: Some operators in Class String
Originally posted by Begins
/*String& String:: operator+= (const String& rhs)
{
if (this != &rhs)
strcat(value, rhs.value);
return *this;
}
[/CODE]
You're not allocating enough space to concatenate the string, so you're writing pass the array boundary.
Last edited by cma; March 31st, 2004 at 10:30 PM.
-
April 1st, 2004, 03:18 AM
#6
The >> operator cannot take a const argument, you are modifiying it.
All the buzzt
CornedBee
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|