Mira2706

oop xml

May 4th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. class String{
  8. private:
  9.     char* str;
  10. public:
  11.     String();
  12.     String(const char *s)
  13.     {
  14.         str = new char[strlen(s)+1];
  15.         strcpy(str, s);
  16.     }
  17.     void print()
  18.     {
  19.         cout << str << endl;
  20.     }
  21.    
  22.     String operator+(String other)
  23.     {
  24.         String result;
  25.         result.str = new char[strlen(str)+strlen(other.str)+1];
  26.         strcpy(result.str, str);
  27.         strcat(result.str, other.str);
  28.         return result;
  29.     }
  30.     String& operator=(const String& other)
  31.     {
  32.         delete[] str;
  33.         str = new char(strlen(other.str)+1);
  34.         strcpy(str, other.str);
  35.         return *this;
  36.     }
  37.     bool operator==(String other)
  38.     {
  39.         return strcmp(str, other.str);
  40.     }
  41.     ~String()
  42.     {
  43.         delete[] str;
  44.     }
  45. };
  46.  
  47. //class File
  48. int main()
  49. {
  50.     String str1;
  51.     String str2;
  52.     str1="hello ";
  53.     str2="world";
  54.     str1 = str2;
  55.     str1.print();
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment