-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathItem7.cpp
More file actions
57 lines (45 loc) · 1.24 KB
/
Item7.cpp
File metadata and controls
57 lines (45 loc) · 1.24 KB
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
//Item 7: Declare destructors virtual in polymorphic base classes
#include <iostream>
#include <string>
using namespace std;
class Animal
{
public:
// Destructor declared virtual
virtual ~Animal() = 0;
virtual void makeSound()=0;
private:
int age;
};
// See Notes at the end
Animal::~Animal() {}
class Dog : public Animal
{
public:
Dog():str("Wow Wow") {}
void makeSound() { cout << str << endl;}
private:
string str;
};
int main()
{
// Base class pointer to a derived class object
Animal *a1 = new Dog();
a1->makeSound();
// See Notes below
delete a1;
a1 = NULL;
}
/* Notes:
1). 'delete a1' would call the derived Dog's destructor only because Animal's destructor is virtual
2). Dog's destructor would destroy the Dog part of a1 and then call Animal's destructor.
3). That's why you need a definition for Animal's destructor even though its pure virtual.
*/
/* Notes:
1). Polymorphic base classes should declare virtual destructors.
If a class has any virtual functions, it should have a virtual destructor.
Like Animal class above
2). Classes not designed to be base classes or not designed to be used polymorphically
should not declare virtual destructors.
Like boost::noncopyable in Item 6.
*/