Advertisement
Week045

Lab 2, Task 2

Sep 27th, 2022
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. class String {
  8. private:
  9.     char* st;
  10. public:
  11.     String(int);
  12.     String(const char* s, int);
  13.     void dislpay()const;
  14.     void get();
  15.     void operator=(String);
  16.     String operator+(String) const;
  17.     String operator+=(String);
  18.     bool operator==(String);
  19.     bool operator!=(String);
  20.     String& operator[](int);
  21.     String operator()(int, int);
  22. };
  23.  
  24. String::String(int sz = 80) {
  25.     st = new char[sz];
  26.     st[0] = '\0';
  27. }
  28. String::String(const char* s, int sz = 80) {
  29.     st = new char[sz];
  30.     strcpy(st, s);
  31. }
  32. void String::dislpay()const {
  33.     cout << "String: " << st << endl;
  34. }
  35. void String::get() {
  36.     cout << "Напишите строку: ";
  37.     cin >> st;
  38. }
  39. bool String::operator==(String s) {
  40.     if (!strcmp(st, s.st))
  41.         return true;
  42.     else return false;
  43. }
  44. bool String::operator!=(String s)
  45. {
  46.     if(!strcmp(st, s.st))
  47.         return false;
  48.     return true;
  49. }
  50.  
  51. void String::operator=(String s) {
  52.     this->st = s.st;
  53. }
  54.  
  55. String String::operator+(String s) const
  56. {
  57.     String temp;
  58.     strcat(temp.st = this->st, s.st);
  59.     return temp;
  60. }
  61.  
  62. String String::operator+=(String s)
  63. {
  64.     return String(strcat(st, s.st));
  65. }
  66.  
  67.  
  68. int main() {
  69.     setlocale(LC_ALL, "Russian");
  70.     String st1 = "World!";
  71.     String st2 = "Hello, ";
  72.     st1.dislpay();
  73.     st2.dislpay();
  74.  
  75.    
  76.     String st3;
  77.     st3 = st2 + st1;
  78.  
  79.     st3.dislpay();
  80.  
  81.  
  82.     /*int a = 1, b = 2;
  83.     if (a == b)
  84.         cout << "lox";
  85.     else cout << "ne lox";
  86.  
  87.     if (st1 == st2)
  88.         cout << "Одинаковые" << endl;
  89.     else cout << "Разные" << endl;*/
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement