-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathdestructors_and_inheritance.cpp
85 lines (70 loc) · 1.99 KB
/
destructors_and_inheritance.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
/*******************************************************************************
*
* Program: Destructors And Inheritance Example
*
* Description: Example of how destructors work with inheritance in C++ when we
* have a base class and a derived class.
*
* YouTube Lesson: https://www.youtube.com/watch?v=jOnVin6eHV8
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
using namespace std;
// A simple base class
class BaseClass
{
public:
// base class constructor
BaseClass()
{
cout << "BaseClass constructor executing" << endl;
}
// base class destructor
~BaseClass()
{
cout << "BaseClass destructor executing" << endl;
}
};
// a simple derived class that inherits from the above base class
class DerivedClass : public BaseClass
{
public:
// derived class constructor
DerivedClass()
{
cout << "DerivedClass constructor executing" << endl;
}
//derived class destructor
~DerivedClass()
{
cout << "DerivedClass destructor executing" << endl;
}
};
void func()
{
// The derived class will be constructed at this point when we declare example
//
// In the case of constructors in a derived class, first the base class
// constructor will run and THEN the derived class constructor will run.
//
DerivedClass example;
// output and endline to separate the constructor/destructor output
cout << endl;
// When the function returns the example object will be destroyed.
//
// In the case of destructors, first the derived class destructor will run
// and THEN the base class destructor will run... the opposite order of
// the constructors!
}
int main()
{
// call the function func so we can create and destroy a DerivedClass object
// instance to explore how destructors work
func();
// output an endline to separate any output from the above function with any
// end of program output
cout << endl;
return 0;
}