-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPureVirtualFunction.cpp
More file actions
45 lines (38 loc) · 1.02 KB
/
PureVirtualFunction.cpp
File metadata and controls
45 lines (38 loc) · 1.02 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
// Pure virtual function: a function in a base class with =0 and no function body, and must be overriden in derived classes
// Abstract class: a class with pure virtual function, can not be instantiated
#include <iostream>
using namespace std;
class Base
{
public:
// Pure virtual function
virtual void display() = 0;
// Pure virtual destructor: 1. declared with =0; 2. makes the class abstract;
// 3. ensures that when you delete a derived class object through a base class pointer, the derived desctructor runs first, followed by the base destructor
virtual ~Base() = 0;
};
// Definition of pure virtual destructor
Base::~Base()
{
cout << "Base destructor called" << endl;
}
class Derived : public Base
{
public:
void display() override
{
cout << "Derived class display" << endl;
}
~Derived()
{
cout << "Derived destructor called" << endl;
}
};
int main()
{
Base *basePtr;
Derived derivedObj;
basePtr = &derivedObj;
basePtr->display();
return 0;
}