Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. class String
  9. {
  10.   friend ostream& operator << (ostream &out, String str);
  11.   friend String operator + (String str1, string str2);
  12.   friend string classToString(String str);
  13.   friend String stringToClass(string str);
  14.   friend bool operator == (String str1, string str2);
  15.   friend bool operator != (String str1, string str2);
  16. public:
  17.     String(string defValue)
  18.     {
  19.         value = defValue;
  20.     }
  21.     String ()
  22.     {
  23.  
  24.     }
  25.     void setValue()
  26.     {
  27.         cout << "enter new value: " << endl;
  28.         string newVal;
  29.         cin >> newVal;
  30.         cin.clear();
  31.         cin.ignore(1000, '\n');
  32.         this -> value = newVal;
  33.     }
  34.     void display()
  35.     {
  36.         cout << value << endl;
  37.     }
  38.     String operator = (String str2)
  39.     {
  40.       this -> value = str2.value;
  41.       return *this;
  42.     }
  43.     char operator [](int i)
  44.     {
  45.       return (this -> value)[i];
  46.     }
  47.     String operator () (string newVal)
  48.     {
  49.       return this -> value = newVal;
  50.     }
  51.     String operator + (String str)
  52.     {
  53.       return this -> value + str.value;
  54.     }
  55.     bool operator == (String str)
  56.     {
  57.       return this -> value == str.value;
  58.     }
  59.  
  60.     bool operator != (String str)
  61.     {
  62.       return this -> value != str.value;
  63.     }
  64.  
  65. private:
  66.     string value;
  67.  
  68. };
  69.  
  70. ostream& operator << (ostream &out, String str)
  71. {
  72.     out << str.value;
  73.     return out;
  74. }
  75.  
  76. String operator + (String str1, string str2)
  77. {
  78.     return str1.value + str2;
  79. }
  80.  
  81. bool operator == (String str1, string str2)
  82. {
  83.   return str1.value == str2;
  84. }
  85.  
  86. bool operator != (String str1, string str2)
  87. {
  88.   return str1.value != str2;
  89. }
  90.  
  91. string classToString(String str)
  92. {
  93.     string res;
  94.     res = str.value;
  95.     return res;
  96. }
  97.  
  98. String stringToClass(string str)
  99. {
  100.     String res("0");
  101.     res.value = str;
  102.     return res;
  103. }
  104.  
  105. void compare(String str1, String str2)
  106. {
  107.     str1.setValue();
  108.     str2.setValue();
  109.     bool x = str1 == str2;
  110.     if(x) cout << "strings are equal" << endl;
  111.     else cout << "strings are different" << endl;
  112. }
  113.  
  114. void compare1(String str1)
  115. {
  116.     str1.setValue();
  117.     string temp;
  118.     cout << "enter string to compare: " << endl;
  119.     cin >> temp;
  120.     cin.clear();
  121.     cin.ignore(1000, '\n');
  122.     bool x = str1 == temp;
  123.     if(x) cout << "strings are equal" << endl;
  124.     else cout << "strings are different" << endl;
  125. }
  126.  
  127. int main()
  128. {
  129.     String str1;
  130.     String str2;
  131.     str1.setValue();
  132.     str2.setValue();
  133.     str1 = str2;
  134.     cout << "the result of operation str1 = str2 is: " << endl;
  135.     str1.display();
  136.     int id;
  137.     do
  138.     {
  139.       cout << "enter index: " << endl;
  140.       cin >> id;
  141.       cin.clear();
  142.       cin.ignore(1000, '\n');
  143.       if(id >= 0) break;
  144.       else cout << "the index must be above 0" << endl;
  145.     }while(true);
  146.     cout << "the " << id << " simbol of a str1 is " << str1[id] << endl;
  147.     string temp;
  148.     cout << "enter new string: " << endl;
  149.     cin >> temp;
  150.     cin.clear();
  151.     cin.ignore(1000, '\n');
  152.     str2(temp);
  153.     cout << "the result of operation () is: str2 = " << endl;
  154.     str2.display();
  155.     cout << "the result of operation str1 + str2 is " << str1 + str2 << endl;
  156.     compare(str1, str2);
  157.     cout << "enter string for addition: " << endl;
  158.     cin >> temp;
  159.     cin.clear();
  160.     cin.ignore(1000, '\n');
  161.     cout << "the result of addition string + String is ";
  162.     (str1 + temp).display();
  163.     compare1(str1);
  164.     cout << "the result of << for str2 is: ";
  165.     cout << str2 << endl;
  166.     cout << "enter string for convertion to String: " << endl;
  167.     cin >> temp;
  168.     cin.clear();
  169.     cin.ignore(1000, '\n');
  170.     str1 = stringToClass(temp);
  171.     str1.display();
  172.     cout << "enter String for convertion to string: " << endl;
  173.     str1.setValue();
  174.     temp = classToString(str1);
  175.     cout << temp;
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement