forked from wadehuber/codeexamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtualfunctions.cpp
More file actions
75 lines (60 loc) · 2.05 KB
/
Copy pathvirtualfunctions.cpp
File metadata and controls
75 lines (60 loc) · 2.05 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<string>
using namespace std;
class Super {
public:
Super() { cout << "SUPER: constructor" << endl; }
// Non-virtual methods can be overridden by the child class
// The type of the pointer determines the method that gets called
void nonVirtual1() { cout << "SUPER: nonVirtual1" << endl; }
void nonVirtual2() { cout << "SUPER: nonVirtual2" << endl; }
// Virtual methods can be overridden by the child class
// The type of the object determines the method that gets called
virtual void Virtual1() { cout << "SUPER: Virtual1" << endl; }
virtual void Virtual2() { cout << "SUPER: Virtual2" << endl; }
// Pure virtual methods must be overridden by any non-abstract
// derived class
virtual void pureVirtual() = 0;
};
class Sub : public Super {
public:
Sub() { cout << "SUB: constructor" << endl; }
// void nonVirtual2() override { cout << "SUPER: nonVirtual2" << endl; }
void nonVirtual2() { cout << "SUB: nonVirtual2" << endl; }
virtual void Virtual2() override { cout << "SUB: Virtual2" << endl; }
void pureVirtual() override { cout << "SUB: pureVirtual" << endl; }
};
int main() {
/*
Super superObject;
*/
Sub subObject;
Super * superPtr = &subObject;
Sub * subPtr = &subObject;
/*
cout << endl << "Calling Super Object methods" << endl;
superObject.nonVirtual1();
superObject.nonVirtual2();
superObject.Virtual1();
superObject.Virtual2();
*/
cout << endl << "Calling Sub Object methods with Sub variable" << endl;
subObject.nonVirtual1();
subObject.nonVirtual2();
subObject.Virtual1();
subObject.Virtual2();
subObject.pureVirtual();
cout << endl << "Calling Sub Object methods with Super pointer" << endl;
superPtr->nonVirtual1();
superPtr->nonVirtual2();
superPtr->Virtual1();
superPtr->Virtual2();
superPtr->pureVirtual();
cout << endl << "Calling Sub Object methods with Sub pointer" << endl;
subPtr->nonVirtual1();
subPtr->nonVirtual2();
subPtr->Virtual1();
subPtr->Virtual2();
subPtr->pureVirtual();
return 0;
}