C++ Object-Oriented Programming 1 #ifndef GRID_HPP 2 #define GRID_HPP 3 4 #inclu
ID: 3917349 • Letter: C
Question
C++ Object-Oriented Programming1 #ifndef GRID_HPP 2 #define GRID_HPP 3 4 #include <memory> 5 #include <vector> 6 7 // /begin globals.hpp 8 enum class Direction {LEFT, RIGHT, UP, DOWN}; 9 enum class Bug {ANT, DOODLE}; 10 11 class NoBug { }; 12 class NoMove { }; 13 // /end globals.hpp 14 15 class Critter; 16 17 class Grid { 18 public: 19 Grid(const Grid &g) = delete; 20 void operator =(const Grid &g) = delete; 21 22 /* 23 * LOOK MA, NO DESTRUCTOR NEEDED 24 */ 25 26 static Grid* instance() 27 { 28 if (!singleGrid) 29 singleGrid = new Grid; 30 31 return singleGrid; 32 } 33 void spawnGrid(int rows, int cols) 34 { 35 36 if (grid.size() == 0) { 37 grid.resize(rows); 38 for (auto &i : grid) { 39 i.resize(cols); 40 } 41 ROWS = rows; 42 COLS = cols; } 43 } 44 45 class iterator; 46 // *** CLASS DECLARATION REMOVED *** 47 48 iterator begin(); 49 iterator end(); 50 51 // Grid stuff like moving Critters and deleting and all that 52 private: 53 static Grid *singleGrid; 54 Grid(); 55 std::vector< std::vector< std::unique_ptr<Critter> > > grid; 56 int ROWS; 57 int COLS; 58 59 // Critter tracking variables 60 }; 61 62 #endif (a) (2 points) How do I declare a Grid object? (b) (2 points) How do I access the Grid object? (c) (4 points) Is it possible to declare multiple Grid objects? Why or why not (be specific, using the code to explain)? (d) (4points)ThisclassstoresdynamicallyallocatedCritters.Whyisnodestructorneeded? (e) (2 points) For this class, what would an iterator represent? (f) (2 points) For this class, what would the iterator returned by the begin() function represent (be specific)? (g) (2 points) For this class, what would the iterator returned by the end() function rep- resent (be specific)? C++ Object-Oriented Programming
1 #ifndef GRID_HPP 2 #define GRID_HPP 3 4 #include <memory> 5 #include <vector> 6 7 // /begin globals.hpp 8 enum class Direction {LEFT, RIGHT, UP, DOWN}; 9 enum class Bug {ANT, DOODLE}; 10 11 class NoBug { }; 12 class NoMove { }; 13 // /end globals.hpp 14 15 class Critter; 16 17 class Grid { 18 public: 19 Grid(const Grid &g) = delete; 20 void operator =(const Grid &g) = delete; 21 22 /* 23 * LOOK MA, NO DESTRUCTOR NEEDED 24 */ 25 26 static Grid* instance() 27 { 28 if (!singleGrid) 29 singleGrid = new Grid; 30 31 return singleGrid; 32 } 33 void spawnGrid(int rows, int cols) 34 { 35 36 if (grid.size() == 0) { 37 grid.resize(rows); 38 for (auto &i : grid) { 39 i.resize(cols); 40 } 41 ROWS = rows; 42 COLS = cols; } 43 } 44 45 class iterator; 46 // *** CLASS DECLARATION REMOVED *** 47 48 iterator begin(); 49 iterator end(); 50 51 // Grid stuff like moving Critters and deleting and all that 52 private: 53 static Grid *singleGrid; 54 Grid(); 55 std::vector< std::vector< std::unique_ptr<Critter> > > grid; 56 int ROWS; 57 int COLS; 58 59 // Critter tracking variables 60 }; 61 62 #endif (a) (2 points) How do I declare a Grid object? (b) (2 points) How do I access the Grid object? (c) (4 points) Is it possible to declare multiple Grid objects? Why or why not (be specific, using the code to explain)? (d) (4points)ThisclassstoresdynamicallyallocatedCritters.Whyisnodestructorneeded? (e) (2 points) For this class, what would an iterator represent? (f) (2 points) For this class, what would the iterator returned by the begin() function represent (be specific)? (g) (2 points) For this class, what would the iterator returned by the end() function rep- resent (be specific)? C++ Object-Oriented Programming
1 #ifndef GRID_HPP 2 #define GRID_HPP 3 4 #include <memory> 5 #include <vector> 6 7 // /begin globals.hpp 8 enum class Direction {LEFT, RIGHT, UP, DOWN}; 9 enum class Bug {ANT, DOODLE}; 10 11 class NoBug { }; 12 class NoMove { }; 13 // /end globals.hpp 14 15 class Critter; 16 17 class Grid { 18 public: 19 Grid(const Grid &g) = delete; 20 void operator =(const Grid &g) = delete; 21 22 /* 23 * LOOK MA, NO DESTRUCTOR NEEDED 24 */ 25 26 static Grid* instance() 27 { 28 if (!singleGrid) 29 singleGrid = new Grid; 30 31 return singleGrid; 32 } 33 void spawnGrid(int rows, int cols) 34 { 35 36 if (grid.size() == 0) { 37 grid.resize(rows); 38 for (auto &i : grid) { 39 i.resize(cols); 40 } 41 ROWS = rows; 42 COLS = cols; } 43 } 44 45 class iterator; 46 // *** CLASS DECLARATION REMOVED *** 47 48 iterator begin(); 49 iterator end(); 50 51 // Grid stuff like moving Critters and deleting and all that 52 private: 53 static Grid *singleGrid; 54 Grid(); 55 std::vector< std::vector< std::unique_ptr<Critter> > > grid; 56 int ROWS; 57 int COLS; 58 59 // Critter tracking variables 60 }; 61 62 #endif (a) (2 points) How do I declare a Grid object? (b) (2 points) How do I access the Grid object? (c) (4 points) Is it possible to declare multiple Grid objects? Why or why not (be specific, using the code to explain)? (d) (4points)ThisclassstoresdynamicallyallocatedCritters.Whyisnodestructorneeded? (e) (2 points) For this class, what would an iterator represent? (f) (2 points) For this class, what would the iterator returned by the begin() function represent (be specific)? (g) (2 points) For this class, what would the iterator returned by the end() function rep- resent (be specific)?
1 #ifndef GRID_HPP 2 #define GRID_HPP 3 4 #include <memory> 5 #include <vector> 6 7 // /begin globals.hpp 8 enum class Direction {LEFT, RIGHT, UP, DOWN}; 9 enum class Bug {ANT, DOODLE}; 10 11 class NoBug { }; 12 class NoMove { }; 13 // /end globals.hpp 14 15 class Critter; 16 17 class Grid { 18 public: 19 Grid(const Grid &g) = delete; 20 void operator =(const Grid &g) = delete; 21 22 /* 23 * LOOK MA, NO DESTRUCTOR NEEDED 24 */ 25 26 static Grid* instance() 27 { 28 if (!singleGrid) 29 singleGrid = new Grid; 30 31 return singleGrid; 32 } 33 void spawnGrid(int rows, int cols) 34 { 35 36 if (grid.size() == 0) { 37 grid.resize(rows); 38 for (auto &i : grid) { 39 i.resize(cols); 40 } 41 ROWS = rows; 42 COLS = cols; } 1 #ifndef GRID_HPP 2 #define GRID_HPP 3 4 #include <memory> 5 #include <vector> 6 7 // /begin globals.hpp 8 enum class Direction {LEFT, RIGHT, UP, DOWN}; 9 enum class Bug {ANT, DOODLE}; 10 11 class NoBug { }; 12 class NoMove { }; 13 // /end globals.hpp 14 15 class Critter; 16 17 class Grid { 18 public: 19 Grid(const Grid &g) = delete; 20 void operator =(const Grid &g) = delete; 21 22 /* 23 * LOOK MA, NO DESTRUCTOR NEEDED 24 */ 25 26 static Grid* instance() 27 { 28 if (!singleGrid) 29 singleGrid = new Grid; 30 31 return singleGrid; 32 } 33 void spawnGrid(int rows, int cols) 34 { 35 36 if (grid.size() == 0) { 37 grid.resize(rows); 38 for (auto &i : grid) { 39 i.resize(cols); 40 } 41 ROWS = rows; 42 COLS = cols; } 43 } 44 45 class iterator; 46 // *** CLASS DECLARATION REMOVED *** 47 48 iterator begin(); 49 iterator end(); 50 51 // Grid stuff like moving Critters and deleting and all that 52 private: 53 static Grid *singleGrid; 54 Grid(); 55 std::vector< std::vector< std::unique_ptr<Critter> > > grid; 56 int ROWS; 57 int COLS; 58 59 // Critter tracking variables 60 }; 61 62 #endif (a) (2 points) How do I declare a Grid object? (b) (2 points) How do I access the Grid object? (c) (4 points) Is it possible to declare multiple Grid objects? Why or why not (be specific, using the code to explain)? (d) (4points)ThisclassstoresdynamicallyallocatedCritters.Whyisnodestructorneeded? (e) (2 points) For this class, what would an iterator represent? (f) (2 points) For this class, what would the iterator returned by the begin() function represent (be specific)? (g) (2 points) For this class, what would the iterator returned by the end() function rep- resent (be specific)?
Explanation / Answer
(a) answer:
The following code illustrates how to create a Gird Object creates an identity matrix of size n, in which the elements are 1.0 along the main diagonal and 0.0 everywhere else:
(b) answer:
The following code illustrates how to access a Gird Object in Main function by just using the arrow operator(->) after ObjectName and function or variables you want to access from grid class:
(c) answer:
The following code creates multiple object of gird class :
In above code two object of Grid class is created one of which is character datatype and another is double datatype. this code shows that by using simple syntax of Grid object multiple object can be created.
(d) answer:
Class Critters does not required any destructor because due to it is dynamically allocated and the class iterator is doing the the work of destructor by using the function end().this function will deallocate all the memory allocate for class Critters.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.