GregLeck

Untitled

Feb 18th, 2022
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Object
  6. {
  7. public:
  8.     virtual void BeginPlay();
  9. };
  10.  
  11. class Actor : public Object
  12. {
  13. public:
  14.     virtual void BeginPlay() override;
  15. };
  16.  
  17. class Pawn : public Actor
  18. {
  19. public:
  20.     virtual void BeginPlay() override;
  21. };
  22.  
  23. int main()
  24. {
  25.     Object* ptr_to_object = new Object;
  26.     Actor* ptr_to_actor = new Actor;
  27.     Pawn* ptr_to_pawn = new Pawn;
  28.  
  29.     // Since Actor and Pawn are Objects, they can be included in
  30.     // an array of type Object
  31.     Object* ObjectArray[] = {ptr_to_object, ptr_to_actor, ptr_to_pawn};
  32.  
  33.     // Because of polymorphism, even though we are looping through an
  34.     // array of Objects, we are calling the specific override BeginPlay() function
  35.     // from the derived classes
  36.     for (int i = 0; i < 3; i++)
  37.     {
  38.         ObjectArray[i]->BeginPlay();
  39.     }
  40.  
  41.     delete ptr_to_object;
  42.     delete ptr_to_actor;
  43.     delete ptr_to_pawn;
  44.     system("pause");
  45. }
  46.  
  47. void Object::BeginPlay()
  48. {
  49.     cout << "Object BeginPlay() called \n\n";
  50. }
  51.  
  52. void Actor::BeginPlay()
  53. {
  54.     cout << "Actor BeginPlay() called \n\n";
  55. }
  56.  
  57. void Pawn::BeginPlay()
  58. {
  59.     cout << "Pawn BeginPlay() called \n\n";
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment