-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.cpp
56 lines (49 loc) · 1.55 KB
/
notes.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
// clang-format off
/*****************************************************************//**
* \file notes.cpp
* \brief
* A destructor is invoked automatically when the object goes out of scope
* or is explicitly destroyed by a call to delete.
* A destructor has the same name as the class, preceded by a tilde (~).
* For example, the destructor for class String is declared: ~String().
*
* You only need to define a custom destructor when the class stores handles
* to system resources that need to be released, or pointers that own the
* memory they point to.
*
* Do not accept arguments.
* Do not return a value (or void).
* Cannot be declared as const, volatile, or static.
*
* \author Xuhua Huang
* \date November 2020
*********************************************************************/
// clang-format on
#include <string>
class String {
public:
String(const char* const ch); // Declare constructor
~String(); // and destructor.
private:
char* _text;
size_t sizeOfText;
};
// Define the constructor.
String::String(const char* const ch) {
sizeOfText = strlen(ch) + 1;
// Dynamically allocate the correct amount of memory.
_text = new char[sizeOfText];
// If the allocation succeeds, copy the initialization string.
if (_text)
strcpy_s(_text, sizeOfText, ch);
}
// Define the destructor.
String::~String() {
// Deallocate the memory that was previously reserved
// for this string.
delete[] _text;
}
int main(void) {
String str("Hello, world!");
return 0;
}