File tree Expand file tree Collapse file tree 1 file changed +27
-3
lines changed Expand file tree Collapse file tree 1 file changed +27
-3
lines changed Original file line number Diff line number Diff line change 1
1
#include < iostream>
2
+ #include < string>
2
3
3
- class Entity
4
+ class Printable
5
+ {
6
+ public:
7
+ virtual std::string GetClassName () = 0;
8
+ };
9
+
10
+ class Entity : public Printable
4
11
{
5
12
public:
6
13
virtual std::string GetName () { return " Entity" ; }
14
+ std::string GetClassName () override { return " Entity" ; }
7
15
};
8
16
9
17
class Player : public Entity
@@ -14,20 +22,36 @@ class Player : public Entity
14
22
Player (const std::string& name) : m_Name(name) {}
15
23
16
24
std::string GetName () override { return m_Name; }
25
+ std::string GetClassName () override { return " Player" ; }
17
26
};
18
27
19
28
void PrintName (Entity* entity)
20
29
{
21
30
std::cout << entity->GetName () << std::endl;
22
31
}
23
32
33
+ class A : public Printable
34
+ {
35
+ public:
36
+ std::string GetClassName () override { return " A" ; }
37
+ };
38
+
39
+ void Print (Printable* obj)
40
+ {
41
+ std::cout << obj->GetClassName () << std::endl;
42
+ }
43
+
24
44
int main ()
25
45
{
26
46
Entity* e = new Entity ();
27
- PrintName (e);
47
+ // PrintName(e);
28
48
29
49
Player* p = new Player (" Cherno" );
30
- PrintName (p);
50
+ // PrintName(p);
51
+
52
+ Print (e);
53
+ Print (p);
54
+ Print (new A ());
31
55
32
56
std::cin.get ();
33
57
}
You can’t perform that action at this time.
0 commit comments