Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <cstring>
- using namespace std;
- class String{
- private:
- char* str;
- public:
- String();
- String(const char *s)
- {
- str = new char[strlen(s)+1];
- strcpy(str, s);
- }
- void print()
- {
- cout << str << endl;
- }
- String operator+(String other)
- {
- String result;
- result.str = new char[strlen(str)+strlen(other.str)+1];
- strcpy(result.str, str);
- strcat(result.str, other.str);
- return result;
- }
- String& operator=(const String& other)
- {
- delete[] str;
- str = new char(strlen(other.str)+1);
- strcpy(str, other.str);
- return *this;
- }
- bool operator==(String other)
- {
- return strcmp(str, other.str);
- }
- ~String()
- {
- delete[] str;
- }
- };
- //class File
- int main()
- {
- String str1;
- String str2;
- str1="hello ";
- str2="world";
- str1 = str2;
- str1.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment