Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class CustomString {
  6. private:
  7.     char* _content;
  8.  
  9.     void createFromChar(const char* content) {
  10.         length = strlen(content);
  11.  
  12.         _content = new char[length + 1];
  13.  
  14.         strcpy_s(_content, length + 1, content);
  15.     }
  16.  
  17. public:
  18.     int length;
  19.  
  20.     CustomString(const char* content) {
  21.         createFromChar(content);
  22.     }
  23.  
  24.     CustomString(const CustomString& content) {
  25.         copy(content);
  26.     }
  27.  
  28.     ~CustomString() {
  29.         delete[] _content;
  30.     }
  31.  
  32.     void print() {
  33.         cout << *this << endl;
  34.     }
  35.  
  36.     void copy(const CustomString& content) {
  37.         length = content.length;
  38.         _content = new char[length + 1];
  39.  
  40.         strcpy_s(_content, length + 1, content._content);
  41.     }
  42.  
  43.     CustomString& operator =(const CustomString& content) {
  44.         copy(content);
  45.         return *this;
  46.     }
  47.  
  48.     CustomString& operator =(const char* content) {
  49.         createFromChar(content);
  50.         return *this;
  51.     }
  52.  
  53.     int operator -(const CustomString& content) {
  54.         return this->length - content.length;
  55.     }
  56.  
  57.     friend ostream& operator<<(ostream& os, const CustomString& content) {
  58.         return os << content._content;
  59.     }
  60. };
  61.  
  62. int main()
  63. {
  64.     CustomString st1("para1");
  65.     CustomString st2("para23");
  66.     CustomString st3(CustomString("new para"));
  67.  
  68.     cout << st1 << endl << endl;
  69.  
  70.     st1.print();
  71.     st2.print();
  72.     st3.print();
  73.  
  74.     cout << endl;
  75.  
  76.     cout << st2 - st1 << endl;
  77.  
  78.     system("pause");
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement