-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path07OK.cpp
More file actions
168 lines (143 loc) · 3.15 KB
/
Copy path07OK.cpp
File metadata and controls
168 lines (143 loc) · 3.15 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Person
{
char* name;
public:
friend class Couple;
friend ostream& operator<<(ostream& str, const Person& os)
{
//str << "Person : " << os.name << endl;
str << os.name;
};
Person(const char* n)
{
//name = (char*) n;
/* Jak dluga jest tablica? */
int licznik, licznik1;
const char *glowicar;
char* glowicaw;
glowicar = n;
//cout << "n : " << n << endl;
//cout << "glowicar : " << glowicar << endl;
licznik = 0;
while( *glowicar != 0)
{
++glowicar;
++licznik;
}
//cout << "n : " << n << endl;
//cout << "glowicar : " << glowicar << endl;
/* Kopiowanie. */
glowicar = n;
//cout << "aa:" << (int)glowicar << endl;
name = (char*)malloc( (licznik + 1)*sizeof(char) );
glowicaw = name;
for(licznik1 = 0; licznik1 < licznik + 1; ++licznik1)
{
*glowicaw = (char)(*glowicar);
++glowicaw;
++glowicar;
}
/* Wydruk testowy. */
cout << "test : " << (int) name << endl ;
};
Person(const Person& os)
{
//name = os.name;
/* Jak dluga jest tablica? */
int licznik, licznik1;
const char* glowicar;
char* glowicaw;
glowicar = os.name;
licznik = 0;
while( *glowicar != 0)
{
++glowicar;
++licznik;
}
/* Kopiowanie. */
glowicar = os.name;
name = (char*)malloc( (licznik + 1)*sizeof(char) );
glowicaw = name;
for(licznik1 = 0; licznik1 < licznik + 1; ++licznik1)
{
*glowicaw = (char)(*glowicar);
++glowicaw;
++glowicar;
}
/* Wydruk testowy. */
cout << "test2 : " << (int) name << endl ;
};
Person& operator=(const Person& os)
{
//free(this->name);
this->name=os.name;
return *this;
};
~Person()
{
cout << "Desktruktor ~Person" << endl;
free(name);
};
};
class Couple
{
Person *husb, *wife;
public:
friend ostream& operator<<(ostream& str, const Couple& p)
{
str << "He: " << *p.husb << " , She: " << *p.wife << endl;
};
Couple(const char* m, const char* z)
{
husb = new Person(m);
wife = new Person(z);
};
Couple(const Couple& p)
{
husb = new Person( *p.husb );
wife = new Person( *p.wife );
};
Couple& operator=(const Couple& p)
{
//free(this->husb->name);
//free(this->wife->name);
//free(this->husb);
//free(this->wife);
this->husb = new Person(*p.husb);
this->wife = new Person(*p.wife);
return *this;
};
~Couple()
{
cout << "Desktruktor ~Couple" << endl;
free(husb->name);
free(wife->name);
free(husb);
free(wife);
};
};
int main(void)
{
Person person1("Mariusz");
Person person2(person1);
cout << person1 << endl;
cout << person2 << endl;
Person *person_c1 = new Person("John");
Person person_c2("Bert");
*person_c1 = person_c2;
Person person_c3(*person_c1);
delete person_c1;
cout << person_c3 << endl;
Couple *c1 = new Couple("John","Sue");
Couple c2("Bert","Elsa");
*c1 = c2;
Couple c3(*c1);
delete c1;
cout << c3 << endl; //He: Bert, She: Elsa
//system("Pause");
return 0;
}