Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <cstring>
- using namespace std;
- class String {
- private:
- char* st;
- int size;
- public:
- String();
- String(const char* s);
- String(const String&);
- ~String();
- void dislpay() const;
- void get();
- int lenght();
- String& operator=(const String&);
- friend String operator+(const String&, const String&);
- String& operator+=(const String&);
- bool operator==(const String&) const;
- bool operator!=(const String&) const;
- char& operator[](int);
- void operator()(int, int) const;
- friend istream& operator>>(istream&, String&);
- friend ostream& operator<<(ostream&, String&);
- };
- String::String() {
- this->size = 1;
- st = new char[this->size];
- st[0] = '\0';
- cout << "Был вызван конструктор: " << this << endl;
- }
- String::String(const char* s) {
- this->size = strlen(s) + 1;
- st = new char[this->size];
- strcpy(st, s);
- cout << "Был вызван конструктор: " << this << endl;
- }
- String::String(const String& other) {
- this->size = other.size + 1;
- this->st = new char[this->size];
- strcpy(this->st, other.st);
- cout << "Был вызван конструктор копирования: " << this << endl;
- }
- String::~String() {
- delete[] st;
- cout << "Был вызван деструктор: " << this << endl;
- }
- void String::dislpay() const {
- cout << "Строка: " << st << endl;
- }
- void String::get() {
- st = new char[80];
- cout << "Напишите строку: ";
- cin.getline(st, 80);
- int new_size = strlen(st) + 1;
- char* new_st = new char[new_size];
- strcpy(new_st, st);
- delete[] st;
- st = new_st;
- size = new_size;
- }
- int String::lenght() {
- return strlen(st);
- }
- String operator+(const String& s1, const String& s2) {
- String temp = s1;
- int new_size = s1.size + s2.size - 1;
- char* new_st = new char[new_size];
- strcpy(new_st, temp.st);
- delete[] temp.st;
- temp.st = new_st;
- strcat(temp.st, s2.st);
- temp.size = new_size;
- return temp;
- }
- String& String::operator=(const String& s) {
- delete[] st;
- st = new char[s.size];
- strcpy(st, s.st);
- size = s.size;
- return *this;
- }
- String& String::operator+=(const String& s) {
- int new_size = size + s.size;
- if (new_size >= size) {
- char* new_st = new char[new_size - 1];
- strcpy(new_st, st);
- delete[] st;
- st = new_st;
- }
- strcat(st, s.st);
- size = new_size - 1;
- return *this;
- }
- bool String::operator==(const String& s) const {
- if (!strcmp(st, s.st))
- return true;
- else return false;
- }
- bool String::operator!=(const String& s) const {
- if(!strcmp(st, s.st))
- return false;
- return true;
- }
- char& String::operator[](int index) {
- if (index >= size) {
- cout << "Ошибка!" << endl;
- exit(1);
- }
- return *(st + index);
- }
- void String::operator()(int start, int end) const {
- if (start < 0 || end >= size) {
- cout << "Ошибка!" << endl;
- exit(1);
- }
- cout << "Символы: ";
- for (int i = start; i <= end; i++)
- cout << *(st + i);
- cout << endl;
- }
- istream& operator>>(istream& in, String& s) {//################DON'T WORKED#######################
- return in >> s.st;
- }
- ostream& operator<<(ostream& out, String& s) {
- return out << s.st;
- }
- int main() {
- setlocale(LC_ALL, "Russian");
- String st1 = "Hello, ";
- String st2 = "World!";
- String st3;
- //st3 = "Hello, " + st2;
- //cout << st3 << endl;
- st3 = st1 + st2;
- cout << st3 << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement