-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp_tests.cpp
120 lines (97 loc) · 2.65 KB
/
cpp_tests.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <cstring>
#include <memory>
#include <algorithm>
#include <iostream>
using namespace std;
class mystring
{
private:
char *data;
public:
mystring(){}
mystring(const char *p)
{
size_t size = strlen(p)+1;
data = new char[size];
memcpy(data, p, size);
}
~mystring()
{
cout<<"\ndata: "<<&data<<"\n";
cout<<"destructor called for object: "<<data<<"\n";
delete[] data;
}
mystring(const mystring& source)
{
size_t size = strlen(source.data)+1;
data = new char[size];
memcpy(data, source.data, size);
}
// mystring(mystring &&source)
// {
// data = source.data;
// source.data = nullptr;
// }
char *getData()
{
cout<<"\ndata: "<<&data<<"\n";
return data;
}
mystring* operator+(const mystring *a) const
{
mystring *newdata = new mystring("");
strcat(newdata->data, this->data);
strcat(newdata->data, a->data);
return newdata;
}
mystring operator+(mystring a)
{
mystring newdata;
size_t this_size = strlen(this->data);
size_t source_size = strlen(a.data);
newdata.data = new char[strlen("12345678")];
strcpy(newdata.data,"12345678");
memcpy(newdata.data, this->data, this_size);
memcpy(newdata.data+this_size, a.data, source_size);
newdata.data[this_size + source_size + 1] = '\0';
return newdata;
}
};
class abc {
public:
static int x;
int i;
abc() {
i = ++x;
}
};
int abc::x;
void just_a_test()
{
abc m, n, p;
cout<<m.x<<" "<<m.i<<endl;
}
int main(void)
{
char *x = (char*)"Some text";
char *y = (char*)"string data";
mystring *a = new mystring(x);
mystring *b = new mystring(y);
mystring a1("1234");
mystring b1("5678");
mystring *c = new mystring("Some other string");
mystring *d = new mystring(*a);
mystring *e = new mystring("");
mystring f;
e = a->operator+(b);
// e = a + b;
f = a1.operator+(b1);
f = a1 + b1;
cout<<"a: "<<a->getData()<<"\n";
cout<<"b: "<<b->getData()<<"\n";
cout<<"c: "<<c->getData()<<"\n";
cout<<"d: "<<d->getData()<<"\n";
cout<<"e: "<<e->getData()<<"\n";
cout<<"f: "<<f.getData()<<"\n";
return 1;
}