File tree 1 file changed +14
-17
lines changed
1 file changed +14
-17
lines changed Original file line number Diff line number Diff line change 3
3
class Entity
4
4
{
5
5
public:
6
- float X, Y;
7
-
8
- void Move (float xa, float ya)
9
- {
10
- X += xa;
11
- Y += ya;
12
- }
6
+ virtual std::string GetName () { return " Entity" ; }
13
7
};
14
8
15
9
class Player : public Entity
16
10
{
11
+ private:
12
+ std::string m_Name;
17
13
public:
18
- const char * Name;
14
+ Player ( const std::string& name) : m_Name(name) {}
19
15
20
- void PrintName ()
21
- {
22
- std::cout << Name << std::endl;
23
- }
16
+ std::string GetName () override { return m_Name; }
24
17
};
25
18
19
+ void PrintName (Entity* entity)
20
+ {
21
+ std::cout << entity->GetName () << std::endl;
22
+ }
23
+
26
24
int main ()
27
25
{
28
- std::cout << sizeof (Entity) << std::endl ;
29
- std::cout << sizeof (Player) << std::endl ;
26
+ Entity* e = new Entity () ;
27
+ PrintName (e) ;
30
28
31
- Player player;
32
- player.Move (5 , 5 );
33
- player.X = 2 ;
29
+ Player* p = new Player (" Cherno" );
30
+ PrintName (p);
34
31
35
32
std::cin.get ();
36
33
}
You can’t perform that action at this time.
0 commit comments