-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmystring.cpp
More file actions
49 lines (42 loc) · 1.23 KB
/
Copy pathmystring.cpp
File metadata and controls
49 lines (42 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "mystring.h"
#include <iostream>
#include "cstring"
int Mystring::strings = 0;
Mystring::Mystring() {
this->str = new char[10];
strcpy(this->str, "<default>");
strings++;
std::cout << "default string created, " << strings << "strings exit." << std::endl;
}
Mystring::Mystring(const char* c) {
length = strlen(c);
this->str = new char[length+1];
strcpy(this->str, c);
strings++;
std::cout << "string \""<< this->str << "\" created by constructor, " << strings << "strings exit." << std::endl;
}
Mystring::~Mystring() {
std::cout << this->str << " --> delete. ";
strings--;
std::cout << strings << " strings left. " << std::endl;
delete [] str;
}
Mystring::Mystring(const Mystring &s) {
strings++;
this->length = s.length;
this->str = new char[this->length + 1];
strcpy(this->str, s.str);
std::cout << "string \""<< this->str << "\" created by copy, " << strings << "strings exit." << std::endl;
}
Mystring & Mystring::operator=(const Mystring &s) {
if (&s == this) return *this;
delete [] this->str;
this->length = s.length;
this->str = new char[this->length + 1];
strcpy(this->str, s.str);
return *this;
}
std::ostream & operator<<(std::ostream &os, const Mystring &str) {
os << str.str << std::endl;
return os;
}