-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18.virtual_functions.cpp
46 lines (34 loc) · 1.31 KB
/
18.virtual_functions.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
#include <iostream>
// virtual functions allow us to override methods in subclasses
// we got two classes A & B, B is derived from A, meaning B is sub class of A
// if we create a a method in A class and mark it as virtual we have the option of overriding that method in B class
class Entity
{
public:
virtual std::string GetName(){ return "Entity";} // *
};
class Player : public Entity
{
private:
std::string m_name;
public:
Player(const std::string& name)
: m_name(name) {}
std::string GetName() override {return m_name;} // *
};
int main()
{
Entity* e = new Entity();
std::cout << e->GetName() << std::endl;
Player* p = new Player("fati") ;
std::cout << p->GetName() << std::endl;
// virtual functions introduce sth called dynamic dispatch which compiler implement via v table
// v tbale is a table which contains mapping of all the virtual functions inside our base class
// so we can actually map them to the correct overwritten function at runtime
Entity* entity = p;
std::cout << entity->GetName() << std::endl;
// so all we need to know is that if you want to overwrite a function you have to mark the base function
// in the base class as virtual
// virtual functions do cost a little more memmory and performance
std::cin.get();
}