Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- #include <cstdlib>
- using namespace std;
- class String
- {
- friend ostream& operator << (ostream &out, String str);
- friend String operator + (String str1, string str2);
- friend string classToString(String str);
- friend String stringToClass(string str);
- friend bool operator == (String str1, string str2);
- friend bool operator != (String str1, string str2);
- public:
- String(string defValue)
- {
- value = defValue;
- }
- String ()
- {
- }
- void setValue()
- {
- cout << "enter new value: " << endl;
- string newVal;
- cin >> newVal;
- cin.clear();
- cin.ignore(1000, '\n');
- this -> value = newVal;
- }
- void display()
- {
- cout << value << endl;
- }
- String operator = (String str2)
- {
- this -> value = str2.value;
- return *this;
- }
- char operator [](int i)
- {
- return (this -> value)[i];
- }
- String operator () (string newVal)
- {
- return this -> value = newVal;
- }
- String operator + (String str)
- {
- return this -> value + str.value;
- }
- bool operator == (String str)
- {
- return this -> value == str.value;
- }
- bool operator != (String str)
- {
- return this -> value != str.value;
- }
- private:
- string value;
- };
- ostream& operator << (ostream &out, String str)
- {
- out << str.value;
- return out;
- }
- String operator + (String str1, string str2)
- {
- return str1.value + str2;
- }
- bool operator == (String str1, string str2)
- {
- return str1.value == str2;
- }
- bool operator != (String str1, string str2)
- {
- return str1.value != str2;
- }
- string classToString(String str)
- {
- string res;
- res = str.value;
- return res;
- }
- String stringToClass(string str)
- {
- String res("0");
- res.value = str;
- return res;
- }
- void compare(String str1, String str2)
- {
- str1.setValue();
- str2.setValue();
- bool x = str1 == str2;
- if(x) cout << "strings are equal" << endl;
- else cout << "strings are different" << endl;
- }
- void compare1(String str1)
- {
- str1.setValue();
- string temp;
- cout << "enter string to compare: " << endl;
- cin >> temp;
- cin.clear();
- cin.ignore(1000, '\n');
- bool x = str1 == temp;
- if(x) cout << "strings are equal" << endl;
- else cout << "strings are different" << endl;
- }
- int main()
- {
- String str1;
- String str2;
- str1.setValue();
- str2.setValue();
- str1 = str2;
- cout << "the result of operation str1 = str2 is: " << endl;
- str1.display();
- int id;
- do
- {
- cout << "enter index: " << endl;
- cin >> id;
- cin.clear();
- cin.ignore(1000, '\n');
- if(id >= 0) break;
- else cout << "the index must be above 0" << endl;
- }while(true);
- cout << "the " << id << " simbol of a str1 is " << str1[id] << endl;
- string temp;
- cout << "enter new string: " << endl;
- cin >> temp;
- cin.clear();
- cin.ignore(1000, '\n');
- str2(temp);
- cout << "the result of operation () is: str2 = " << endl;
- str2.display();
- cout << "the result of operation str1 + str2 is " << str1 + str2 << endl;
- compare(str1, str2);
- cout << "enter string for addition: " << endl;
- cin >> temp;
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "the result of addition string + String is ";
- (str1 + temp).display();
- compare1(str1);
- cout << "the result of << for str2 is: ";
- cout << str2 << endl;
- cout << "enter string for convertion to String: " << endl;
- cin >> temp;
- cin.clear();
- cin.ignore(1000, '\n');
- str1 = stringToClass(temp);
- str1.display();
- cout << "enter String for convertion to string: " << endl;
- str1.setValue();
- temp = classToString(str1);
- cout << temp;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement