-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathdestructor_basics.cpp
88 lines (71 loc) · 2.61 KB
/
destructor_basics.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
/*******************************************************************************
*
* Program: Destructor Basics
*
* Description: Demonstrates the basics of using destructor functions in C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=abOMO8GY6io
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
using namespace std;
// a simple "Number" class
class Number
{
// the class has one private member variable, a pointer to a double
private:
double *number;
public:
// the constructor
Number(double num)
{
// allocate space for a double value, have the private member variable
// number pointo that space
number = (double *) malloc(sizeof(double));
// dereference the pointer, store the value num at the allocated space
*number = num;
// output statements to trace execution of when the constructor is called
cout << "Constructor executing!" << endl;
cout << "Number: " << *number << endl << endl;
}
// Destructor function runs when the object is destroyed, and is generally
// used to do clean up work such as making free dynamically allocated memory.
// We put a ~ symbol in front of the class name to define the destructor, and
// destructors cannot have parameters.
~Number()
{
// output statements to trace execution of when the destructor is called
cout << "Destructor executing!" << endl;
cout << "Number: " << *number << endl << endl;
// free the dynamically allocated memory to make it available again and
// prevent a memory leak
free(number);
}
};
// very basic function that creates a Number object locally
void test()
{
// Number object is created here, and destroyed when the function completes
// execution... at this point the destructor is called but it is implicit
// in the sense that we don't write any statement to explicitly invoke it
Number six(6);
}
int main()
{
// when we create an object on the heap with dynamically allocated memory
// using 'new', the constructor will be called
Number *five = new Number(5);
// when we use delete to destroy the object and free the memory, the
// destructor will then be called, and in this sense we are more explicitly
// "calling" the destructor
delete five;
// call the test function, which will cause an object to be created and
// destroyed when the function completes execution
test();
// we create an object that will be destroyed when the program terminates,
// calling the destructor at that time
Number seven(7);
return 0;
}