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 11#include < iostream>
2+ #include < string>
23
3- class Entity
4+ class Printable
5+ {
6+ public:
7+ virtual std::string GetClassName () = 0;
8+ };
9+
10+ class Entity : public Printable
411{
512public:
613 virtual std::string GetName () { return " Entity" ; }
14+ std::string GetClassName () override { return " Entity" ; }
715};
816
917class Player : public Entity
@@ -14,20 +22,36 @@ class Player : public Entity
1422 Player (const std::string& name) : m_Name(name) {}
1523
1624 std::string GetName () override { return m_Name; }
25+ std::string GetClassName () override { return " Player" ; }
1726};
1827
1928void PrintName (Entity* entity)
2029{
2130 std::cout << entity->GetName () << std::endl;
2231}
2332
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+
2444int main ()
2545{
2646 Entity* e = new Entity ();
27- PrintName (e);
47+ // PrintName(e);
2848
2949 Player* p = new Player (" Cherno" );
30- PrintName (p);
50+ // PrintName(p);
51+
52+ Print (e);
53+ Print (p);
54+ Print (new A ());
3155
3256 std::cin.get ();
3357}
You can’t perform that action at this time.
0 commit comments