-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpens.cpp
More file actions
31 lines (26 loc) · 886 Bytes
/
pens.cpp
File metadata and controls
31 lines (26 loc) · 886 Bytes
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
#include <iostream>
#include "pens.hpp"
using namespace std;
int main() {
// dynamically created array that contains pen pointers
pen** assorted_pens = new pen*[3];
// the array can contain pointers to pens or any of its derived classes due to
// polymorphism
assorted_pens[0] = new pen;
assorted_pens[1] = new thick_pen;
assorted_pens[2] = new marker;
// all pens and its derived classes will have an implementation of the write()
// function so they can all easily be called using a loop
for (int i = 0; i < 3; i++) {
assorted_pens[i]->write();
}
// all addresses of the objects stored in the array need to be deleted first
for (int i = 0; i < 3; i++) {
delete assorted_pens[i];
assorted_pens[i] = nullptr;
}
// after deleting each element then the array can be deleted
delete[] assorted_pens;
assorted_pens = nullptr;
return 0;
}