-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual_function.cpp
More file actions
62 lines (43 loc) · 1.36 KB
/
Copy pathvirtual_function.cpp
File metadata and controls
62 lines (43 loc) · 1.36 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
#include<iostream>
#include<string>
// You want be able to create a instance of the class since it has a pure virtual function
// PureVirtualExample
//
class Entity {
public:
// cannot override
std::string GetName() { return "Entity"; }
// this can be overriden, but not required
virtual std::string GetName1() { return "Entity"; }
// pure virtual function, this makes the call abstract,
// meaning that we cannot create a instance of the class
virtual std::string PureVirtualExample() = 0;
};
class Player : public Entity {
std::string m_Name;
public:
Player(const std::string& name)
: m_Name(name) {}
std::string GetName() {
return m_Name;
}
std::string GetName1 () override {
return m_Name;
}
std::string PureVirtualExample() {
return m_Name;
}
};
void PrintName(Entity* e) {
std::cout << "function from entity: " << e->GetName() << std::endl;
std::cout << "Virtual function: " << e->GetName1() << std::endl;
std::cout << "pure virtual function: " << e->PureVirtualExample() << std::endl;
}
int main() {
Player* p = new Player("Joan");
PrintName(p);
// output:
// function from entity: Entity
// Virtual function: Joan
// pure virtual function: Joan
}