aboutsummaryrefslogtreecommitdiff
path: root/week05/Exercise3.cpp
blob: 5373540c32f171723d8024ad61f64a2f27c033e3 (plain) (blame)
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
#include <cstring>

struct Email {
private:
	char address[128];
	char* contents;

	void free() {
		delete[] contents;
	}
	void copyFrom(const Email& other) {
		strcpy(this->address, other.address);

		int contSize = strlen(other.contents);
		this->contents = new char[contSize+1];
		strcpy(this->contents, other.contents);
	}

public:
	Email(char address[128], const char* contents) {
		strcpy(this->address, address);

		int contSize = strlen(contents);
		this->contents = new char[contSize + 1];
		strcpy(this->contents, contents);
	}

	Email() {
		address[0] = '\0';
		contents = nullptr;
	}
	~Email() {
		free();
	}
	Email(const Email& other) {
		copyFrom(other);
	}
	Email& operator=(const Email& other) {
		if (this != &other) {
			free();
			copyFrom(other);
		}
		return *this;
	}
};