forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pure_virtual.cpp
75 lines (57 loc) · 1.76 KB
/
pure_virtual.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
/*
# Pure virtual
Virtual function without implementation. Syntax:
virtual f() = 0
Makes it impossible instantiate the class.
It is only possible to instantiate derived classes that override it.
If a class has a pure virtual method is called as an *abstract class* or *interface*.
In Java there is a language difference between those two terms,
and it might be a good idea to differentiate them when speaking about C++:
- interface: no data
- abstract: data
*/
#include "common.hpp"
int main() {
// Basic example.
{
// This is an abstract type because it has a pure virtual method.
class Base {
public:
virtual int f() = 0;
};
class Derived : public Base {
public:
virtual int f() { return 1; }
};
// ERROR: abstract type.
//Base b;
Derived d;
assert(d.f() == 1);
// We can still have abstract pointers and references to abstract classes.
Base *bp = &d;
Base &br = d;
assert(bp->f() == 1);
assert(br.f() == 1);
}
/*
It is not possible to implement pure virtual methods on another base class
with multiple-inheritance: they must be implemented on the Derived class.
In some languages where this is possible, this pattern is called a mixin:
http://stackoverflow.com/a/20022860/895245
*/
{
class Base {
public:
virtual int f() = 0;
};
class Implementor {
public:
virtual int f() {
return 1;
}
};
class Derived : public Base, public Implementor {};
// ERROR: abstract.
//Derived d;
}
}