Skip to content

Commit a3ac87d

Browse files
committed
Add Interfaces in C++
1 parent 728d314 commit a3ac87d

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

HelloWorld/src/Main.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
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
{
512
public:
613
virtual std::string GetName() { return "Entity"; }
14+
std::string GetClassName() override { return "Entity"; }
715
};
816

917
class 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

1928
void 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+
2444
int 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
}

0 commit comments

Comments
 (0)