Как создать собственный класс String на C ++ с базовыми функциями
В этой статье мы создадим наш настраиваемый строковый класс, который будет иметь те же функции, что и существующий строковый класс.
Класс String имеет следующие основные функции:
- Конструктор без аргументов: он выделяет память для строкового объекта в куче и присваивает значение как символ NULL.
- Конструктор только с одним аргументом: он принимает указатель на символ, или мы можем сказать, если мы передаем массив символов, принимает указатель на первый символ в массиве, тогда конструктор класса String выделяет хранилище в куче памяти того же размера, что и переданный массив, и копирует содержимое массива в выделенную память в куче. Он копирует содержимое с помощью функции strcpy (), объявленной в библиотеке cstring.
Перед выполнением вышеуказанной операции он проверяет, что если переданный аргумент является указателем NULL, то он ведет себя как конструктор без аргументов. - Конструктор копирования: он вызывается, когда любой объект того же типа, созданный из уже созданного объекта, выполняет глубокую копию . Он выделяет новое пространство в куче для объекта, который должен быть создан, и копирует содержимое переданного объекта (который передается как ссылка).
- Конструктор перемещения: обычно вызывается, когда объект инициализируется (путем прямой инициализации или инициализации копированием) из rvalue того же типа. Он принимает ссылку на rvalue объекта типа настраиваемого строкового класса.
Below is the implementation of the above methods using custom string class Mystring:
// C++ program to illustrate the // above discussed functionality #include <cstring> #include <iostream> using namespace std; // Custom string class class Mystring { // Initialise the char array char * str; public : // No arguments Constructor Mystring(); // Constructor with 1 arguments Mystring( char * val); // Copy Constructor Mystring( const Mystring& source); // Move Constructor Mystring(Mystring&& source); // Destructor ~Mystring() { delete str; } }; // Function to illustrate Constructor // with no arguments Mystring::Mystring() : str{ nullptr } { str = new char [1]; str[0] = " " ; } // Function to illustrate Constructor // with one arguments Mystring::Mystring( char * val) { if (val == nullptr) { str = new char [1]; str[0] = " " ; } else { str = new char [ strlen (val) + 1]; // Copy character of val[] // using strcpy strcpy (str, val); cout << "The string passed is: " << str << endl; } } // Function to illustrate // Copy Constructor Mystring::Mystring( const Mystring& source) { str = new char [ strlen (source.str) + 1]; strcpy (str, source.str); } // Function to illustrate // Move Constructor Mystring::Mystring(Mystring&& source) { str = source.str; source.str = nullptr; } // Driver Code int main() { // Constructor with no arguments Mystring a; // Convert string literal to // char array char temp[] = "Hello" ; // Constructor with one argument Mystring b{ temp }; // Copy constructor Mystring c{ a }; char temp1[] = "World" ; // One arg constructor called, // then the move constructor Mystring d{ Mystring{ temp } }; return 0; } |
The string passed is: Hello The string passed is: Hello
Еще несколько функций строкового класса:
- pop_back (): удаляет последний элемент из объекта Mystring.
- push_back (char ch): принимает символ в качестве аргумента и добавляет его в конец объекта Mystring.
- length (): возвращает длину строки mystring.
- copy (): копирует объект mystring в массив символов из заданной позиции (pos) и определенной длины (len).
- swap (): он меняет местами два объекта Mystring.
- Объединение двух строк с помощью перегрузки оператора "+": позволяет объединить две строки.
Below is the program to illustrate the above-discussed functionality:
// C++ program to illustrate the // above functionalities #include <cstring> #include <iostream> using namespace std; // Class Mystring class Mystring { // Prototype for stream insertion friend ostream& operator<<( ostream& os, const Mystring& obj); // Prototype for stream extraction friend istream& operator>>( istream& is, Mystring& obj); // Prototype for "+" // operator overloading friend Mystring operator+( const Mystring& lhs, const Mystring& rhs); char * str; public : // No arguments constructor Mystring(); // pop_back() function void pop_bk(); // push_back() function void push_bk( char a); // To get the length int get_length(); // Function to copy the string // of length len from position pos void cpy( char s[], int len, int pos); // Swap strings function void swp(Mystring& rhs); // Constructor with 1 arguments Mystring( char * val); // Copy Constructor Mystring( const Mystring& source); // Move Constructor Mystring(Mystring&& source); // Overloading the assignment // operator Mystring& operator=( const Mystring& rhs); // Destructor ~Mystring() { delete str; } }; // Overloading the assignment operator Mystring& Mystring::operator=( const Mystring& rhs) { if ( this == &rhs) return * this ; delete [] str; str = new char [ strlen (rhs.str) + 1]; strcpy (str, rhs.str); return * this ; } // Overloading the plus operator Mystring operator+( const Mystring& lhs, const Mystring& rhs) { int length = strlen (lhs.str) + strlen (rhs.str); char * buff = new char [length + 1]; // Copy the strings to buff[] strcpy (buff, lhs.str); strcat (buff, rhs.str); // String temp Mystring temp{ buff }; // delete the buff[] delete [] buff; // Return the concatenated string return temp; } // Overloading the stream // extraction operator istream& operator>>(istream& is, Mystring& obj) { char * buff = new char [1000]; is >> buff; obj = Mystring{ buff }; delete [] buff; return is; } // Overloading the stream // insertion operator ostream& operator<<(ostream& os, const Mystring& obj) { os << obj.str; return os; } // Function for swapping string void Mystring::swp(Mystring& rhs) { Mystring temp{ rhs }; rhs = * this ; * this = temp; } // Function to copy the string void Mystring::cpy( char s[], int len, int pos) { for ( int i = 0; i < len; i++) { s[i] = str[pos + i]; } s[len] = " " ; } // Function to implement push_bk void Mystring::push_bk( char a) { // Find length of string int length = strlen (str); char * buff = new char [length + 2]; // Copy character from str // to buff[] for ( int i = 0; i < length; i++) { buff[i] = str[i]; } buff[length] = a; buff[length + 1] = " " ; // Assign the new string with // char a to string str * this = Mystring{ buff }; // Delete the temp buff[] delete [] buff; } // Function to implement pop_bk void Mystring::pop_bk() { int length = strlen (str); char * buff = new char [length]; // Copy character from str // to buff[] for ( int i = 0; i < length - 1; i++) buff[i] = str[i]; buff[length] = " " ; // Assign the new string with // char a to string str * this = Mystring{ buff }; // delete the buff[] delete [] buff; } // Function to implement get_length int Mystring::get_length() { return strlen (str); } // Function to illustrate Constructor // with no arguments Mystring::Mystring() : str{ nullptr } { str = new char [1]; str[0] = " " ; } // Function to illustrate Constructor // with one arguments Mystring::Mystring( char * val) { if (val == nullptr) { str = new char [1]; str[0] = " " ; } else { str = new char [ strlen (val) + 1]; // Copy character of val[] // using strcpy strcpy (str, val); } } // Function to illustrate // Copy Constructor Mystring::Mystring( const Mystring& source) { str = new char [ strlen (source.str) + 1]; strcpy (str, source.str); } // Function to illustrate // Move Constructor Mystring::Mystring(Mystring&& source) { str = source.str; source.str = nullptr; } // Driver Code int main() { // Constructor with no arguments Mystring a; // Convert string literal to // char array char temp[] = "Hello" ; // Constructor with one argument Mystring b{ temp }; // Copy constructor Mystring c{ a }; char temp1[] = "World" ; // One arg constructor called, // then the move constructor Mystring d{ Mystring{ temp } }; // Remove last character from // Mystring b b.pop_bk(); // Print string b cout << "Mystring b: " << b << endl; // Append last character from // Mystring b b.push_bk( "o" ); // Print string b cout << "Mystring b: " << b << endl; // Print length of string b cout << "Length of Mystring b: " << b << endl; char arr[80]; // Copy string b chars from // length 0 to 3 b.cpy(arr, 3, 0); // Print string arr cout << "arr is: " << arr << endl; // Swap d and b d.swp(b); // Print d and b cout << d << " " << b << endl; // Concatenate b and b with // overloading "+" operator d = b + b; // Print string d cout << "string d: " << d << endl; return 0; } |
Mystring b: Hell Mystring b: Hello Length of Mystring b: Hello arr is: Hel Hello Hello string d: HelloHello
Вниманию читателя! Не прекращайте учиться сейчас. Освойте все важные концепции DSA с помощью самостоятельного курса DSA по приемлемой для студентов цене и будьте готовы к работе в отрасли. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .
Если вы хотите посещать живые занятия с отраслевыми экспертами, пожалуйста, обращайтесь к Geeks Classes Live и Geeks Classes Live USA.