Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am currently programming an assignment for a C++ class and was wondering how t

ID: 3868260 • Letter: I

Question

I am currently programming an assignment for a C++ class and was wondering how to make the text below happen. I need to link rooms together with pointers, but i dont understand how to if the adjacent rooms have not been initialized yet? Would i simply assign the pointers after i have declared the objects that hold the adjacent rooms? Any code would be of great help.

The game requires a Space class, which represents the space the player can be in. The Space class must be an abstract class that will have pure virtual functions.

Inside the Space class, there must be at least 4 Space pointers: top, right, left, and bottom.

Use the class to create a game with structure of linked space. (You are free to add more Space pointers to the Space class, but must have at least 4 Space pointers)

Note:

Even if your structure is linear, such as a train, you still have at least 4 pointers in the Space class

Any unused pointers need to point to NULL.

Explanation / Answer

#include<iostream>

using namespace std;

class Space {
    public:
      int data;
      Space *top, *left,*right,*bottom;
      virtual void f() = 0;
};

class Room : public Space {
   public:
     void f(){
           cout << "Implemented";
      }
};

int main() {   //Creating a linear structure consisting of 5 rooms

   Space *ptr1, *ptr2, *head;

   head = new Room();
   head->top = NULL;
   head->bottom = NULL;
   head->left = NULL;
   head->right = new Room();
   ptr1 = head;
  
   for (int i = 0; i<5; i++){
      ptr1->data = i;
      ptr2 = new Room();
      ptr2->left = ptr1;
      ptr2->top = NULL;
      ptr2->bottom = NULL;
      ptr1->right = ptr2;
      ptr1 = ptr1->right;
     
   }
   ptr1 = head;
   for (int i = 0; i<5; i++){
     cout << ptr1->data << " ";
     ptr1 = ptr1->right;     
   }
   cout << endl;
}