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:
Cpp 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
main: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]; }
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; }




Reply With Quote