Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <String>
- using namespace std;
- class String
- {
- struct string_info
- {
- char* str;
- int length;
- } *str_ptr;
- public:
- String(const char *);
- String();
- String(const String &);
- String& operator=(const char *);
- String& operator=(const String &);
- ~String();
- friend ostream& operator<<(ostream&, String&);
- friend istream& operator>>(istream&, String&);
- friend int operator==(String& x, char* str)
- {
- return strcmp(x.str_ptr->str, str) == 0;
- }
- friend int operator==(String& x, String& y)
- {
- return strcmp(x.str_ptr->str, y.str_ptr->str) == 0;
- }
- };
- String::String()
- {
- str_ptr = new string_info;
- str_ptr->str = 0;
- str_ptr->length = 1;
- }
- String::String(const char* str)
- {
- str_ptr = new string_info;
- str_ptr->str = new char[strlen(str) + 1];
- strcpy(str_ptr->str, str);
- str_ptr->length = 1;
- }
- String::String(const String& x)
- {
- x.str_ptr->length++;
- str_ptr = x.str_ptr;
- }
- String::~String()
- {
- if (--str_ptr->length == 0) {
- delete str_ptr->str;
- delete str_ptr;
- }
- }
- String& String::operator=(const char* str)
- {
- if (str_ptr->length > 1) { // разъединить себя
- str_ptr->length--;
- str_ptr = new string_info;
- }
- else if (str_ptr->length == 1)
- delete str_ptr->str;
- str_ptr->str = new char[strlen(str) + 1];
- strcpy(str_ptr->str, str);
- str_ptr->length = 1;
- return *this;
- }
- String& String::operator=(const String& x)
- {
- x.str_ptr->length++;
- if (--str_ptr->length == 0) {
- delete str_ptr->str;
- delete str_ptr;
- }
- str_ptr = x.str_ptr;
- return *this;
- }
- ostream& operator<<(ostream& str, String& x)
- {
- return str << x.str_ptr->str << "\length";
- }
- istream& operator>>(istream& str, String& x)
- {
- char buf[256];
- str >> buf;
- x = buf;
- cout << x << "\length";
- return str;
- }
- int main()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement