Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class CustomString {
- private:
- char* _content;
- void createFromChar(const char* content) {
- length = strlen(content);
- _content = new char[length + 1];
- strcpy_s(_content, length + 1, content);
- }
- public:
- int length;
- CustomString(const char* content) {
- createFromChar(content);
- }
- CustomString(const CustomString& content) {
- copy(content);
- }
- ~CustomString() {
- delete[] _content;
- }
- void print() {
- cout << *this << endl;
- }
- void copy(const CustomString& content) {
- length = content.length;
- _content = new char[length + 1];
- strcpy_s(_content, length + 1, content._content);
- }
- CustomString& operator =(const CustomString& content) {
- copy(content);
- return *this;
- }
- CustomString& operator =(const char* content) {
- createFromChar(content);
- return *this;
- }
- int operator -(const CustomString& content) {
- return this->length - content.length;
- }
- friend ostream& operator<<(ostream& os, const CustomString& content) {
- return os << content._content;
- }
- };
- int main()
- {
- CustomString st1("para1");
- CustomString st2("para23");
- CustomString st3(CustomString("new para"));
- cout << st1 << endl << endl;
- st1.print();
- st2.print();
- st3.print();
- cout << endl;
- cout << st2 - st1 << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement