Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 10.03.2021, 14:35   #1
-d1mon-
 
Регистрация: 07.03.2021
Сообщений: 8
По умолчанию C++ создание класса string

Добрый день! Подскажите пожалуйста, как написать перегрузку для операторов cout << и cin >>?

#include <iostream>
using namespace std;
class Mystr
{
char* strq;
int length;
public:
Mystr()
{
strq = nullptr;
length = 0;
};
Mystr(const char* str)
{
length = strlen(str);
strq = new char[length + 1];
for (int i = 0; i < length; i++)
{
strq[i] = str[i];
}
strq[length] = '\0';
}
Mystr(const Mystr& other)
{
length = strlen(other.strq);
strq = new char[length + 1];
for (int i = 0; i < length; i++)
{
strq[i] = other.strq[i];
}
strq[length] = '\0';
}
Mystr& operator =(const Mystr& other)
{

length = strlen(other.strq);
strq = new char[length + 1];
for (int i = 0; i < length; i++)
{
this->strq[i] = other.strq[i];
}
strq[length] = '\0';
return *this;
}

Mystr operator + (const Mystr & other)
{
Mystr tmp;
int b = strlen(other.strq);
int a = strlen(this->strq);
tmp.length = b + a;
tmp.strq = new char[a + b + 1];
int i = 0;
for (; i < a; i++)
{
tmp.strq[i] = this->strq[i];
}
for (int j = 0; j < i; j++, i++)
{
tmp.strq[i] = other.strq[j];
}
tmp.strq[a + b] = '\0';
return tmp;
};
~Mystr()
{
delete[]strq;
}
int size()
{
return length;
}
bool operator ==(const Mystr& other)
{
if (this->length != other.length)
{
return false;
}
for (int i = 0; i < length; i++)
{
if (this->strq[i] != other.strq[i])
{
return false;
}
}
return true;
}
bool operator !=(const Mystr& other)
{
return !(this->operator!=(other));
}
Mystr(Mystr&& other)
{
this->strq = other.strq;
this->length = other.length;
other.strq = nullptr;

}
char & operator[](int value)
{
return this->strq[value];
}
};
int main()
{
Mystr a = "hello";
}
-d1mon- вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
C++ создание класса string -d1mon- Помощь студентам 1 07.03.2021 19:47
Переопределение операторов, создание собственного класса String БалаШагаЛ Общие вопросы C/C++ 3 04.04.2013 11:00
Задача с элементом класса System.String FullhDi C# (си шарп) 3 03.01.2013 00:28
Исходники класса String KaneKRY Visual C++ 2 08.04.2011 08:37
Runtime error - array of string как атрибут класса ElSnake Помощь студентам 1 08.05.2010 19:51