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(int);
- String(const char* s, int);
- String(String&, int);
- ~String();
- void dislpay() const;
- void get();
- String operator=(String);
- String operator+(String) const;
- void operator+=(String);
- bool operator==(String) const;
- bool operator!=(String) const;
- char& operator[](int);
- void operator()(int, int) const;
- friend istream& operator>>(istream&, String&);
- friend ostream& operator<<(ostream&, String&);
- };
- String::String(int size = 80) {
- this->size = size;
- st = new char[this->size];
- st[0] = '\0';
- //cout << "Был вызван конструктор: " << this << endl;
- }
- String::String(const char* s, int size = 80) {
- this->size = size;
- st = new char[this->size];
- strcpy(st, s);
- cout << "Был вызван конструктор: " << this << endl;
- }
- String::String(String& other, int size = 80) {
- this->size = other.size;
- 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() {
- cout << "Напишите строку: ";
- cin >> st;
- }
- String String::operator+(String s) const {
- String temp;
- strcpy(temp.st, st);
- strcat(temp.st, s.st);
- return temp;
- }
- String String::operator=(String s) {
- return String(strcpy(st, s.st));
- }
- void String::operator+=(String s) {
- strcat(st, s.st);
- }
- bool String::operator==(String s) const {
- if (!strcmp(st, s.st))
- return true;
- else return false;
- }
- bool String::operator!=(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) {
- return in >> s.st;
- }
- ostream& operator<<(ostream& out, String& s) {
- return out << s.st;
- }
- int main() {
- setlocale(LC_ALL, "Russian");
- String st1 = "test1 ";
- String st2 = "test2 ";
- String st9;
- st9 = st1 + st2;
- cout << st9;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement