Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

* File: check07b.cpp ************************************************************************/ #include using namespace std; #include voxel.h #include world.h /************************************************************** * Function: main * Purpose: The driver for the program. Creates a

image text in transcribedimage text in transcribedimage text in transcribed

* File: check07b.cpp
************************************************************************/
#include
using namespace std;
#include "voxel.h"
#include "world.h"
/**************************************************************
* Function: main
* Purpose: The driver for the program. Creates a world, sets
* some Voxels and displays them.
**************************************************************/
int main()
{
cout
// 1. Add code here to declare a World
// 2. Add code here to call the world's display function
/* You CANNOT just do this:
World w;
w.display();
*/
// First, we create a voxel for air
Voxel air(180, 200, 255, 'A');
// 3. Call setBox on your world to put the air at spot (50, 60, 70)
// Next, we create a voxel for grass
Voxel grass(25, 255, 50, 'G');
// 4. Call setBox on your world to put the grass at spot (45, 20, 10)
// 5. Call world's displayBox twice, once for each of these spots
// 6. Finally, make sure to free up your memory by deleting the world
cout
return 0;

}

/Makefile/--------
# Program:
# Checkpoint 07b, Pointers and Minecraft
#
# Author:
# Summary:
# Summaries are not necessary for checkpoint assignments.
###############################################################
a.out : voxel.o world.o check07b.o
g++ voxel.o world.o check07b.o
voxel.o : voxel.h voxel.cpp
g++ -c voxel.cpp
world.o : world.h voxel.h world.cpp
g++ -c world.cpp
check07b.o : voxel.h world.h check07b.cpp
g++ -c check07b.cpp
clean :
rm *.o

rm a.out

FILE: Voxel.cpp
#include "voxel.h"
#include
using namespace std;
/******************************************************
* Function: Display
* Description: Displays the value of a Voxel.
******************************************************/
void Voxel :: display() const
{
cout
cout
}

File: voxel.h

#ifndef VOXEL_H
#define VOXEL_H
/*********************************************************
* Class: Voxel
* Description: Holds an RGB color value as well as a char
* for a type.
*********************************************************/
class Voxel
{
private:
// stores an integer for Red, Blue, and Green
// The astute coder will observe the great ineffiency here
int red;
int green;
int blue;
// Stores a character code for a particular type
char typeCode;
public:
Voxel() : red(0), green(0), blue(0), typeCode('_') { }
Voxel(int red, int green, int blue, char type)
: red(red), green(green), blue(blue), typeCode(type) { }
/******************************************************
* Function: Display
* Description: Displays the value of a Voxel.
******************************************************/
void display() const;
};

#endif

* File: world.cpp
***********************************************************************/
#include "voxel.h"
#include "world.h"
#include
using namespace std;
World::World()
{
cout
// we are not really connecting to a server, just pretending
}
World::~World()
{
cout
// Shh... we're not actually doing anything :-)
}
/*****************************************************
* getBox / setBox
* Description: Get or set a specific voxel.
*****************************************************/
Voxel World::getBox(int x, int y, int z) const
{
return boxes[x][y][z];
}
void World::setBox(int x, int y, int z, const Voxel & box)
{
boxes[x][y][z] = box;
}
/*****************************************************
* Function: Display
* Description: Puts a message on the screen
*****************************************************/
void World::display()
{
cout
}
/*****************************************************
* Function: DisplayBox
* Description: Outputs the value of the Voxel at the
* location specified.
*****************************************************/
void World::displayBox(int x, int y, int z)
{
cout
boxes[x][y][z].display();
cout

}

* File: world.h
***********************************************************************/
#ifndef WORLD_H
#define WORLD_H
#include "voxel.h"
/***********************************************************
* Class: World
* Description: Holds a 3D array of Voxels for the world.
***********************************************************/
class World
{
private:
Voxel boxes[100][100][100];
public:
World();
~World();
Voxel getBox(int x, int y, int z) const;
void setBox(int x, int y, int z, const Voxel & box);
void display();
void displayBox(int x, int y, int z);
};
#endif
BACKGROUND The memory of your program is divided into two areas the "stack" and the "heap". The stack is where information about your function calls and their local variables are stored, whereas the heap is where data is stored when it is dynamically allocated (think pointers) Among other differences, the stack does not typically have as much space and you can cause a stack overflow or segmentation fault by either calling too many functions or by storing too many large values in local stack variables. Thus, a large array or something similar should be dynamically allocated so that it is stored on the heap SCENARIO Assume that we are writing a game like Minecraft. We will have a World class that stores a 3-dimensional array of "Voxels". A voxel is a simple element of volume (think Pixel, but 3D). Your task for this assignment is to create a world, set some Voxels and display them You are provided with a simple class for a Voxel that stores a RGB color value, as well as a character code for the type of element. You are also provided with a simple class for a World that stores a 3-Dimensional array, named boxes, that stores, 100 x 100 x 100 different Voxel objects. Because this large array is stored directly in the World class, you should not create a World object as a normal local stack variable. Depending on your environment this would likely even cause a segmentation fault. Instead you should use a Pointer to a world object, and dynamically allocate it using the "new" command As a side note, for all the reasons mentioned, a better design for the world class would be to have it contain a dynamically allocated array, and only store the pointer internally. But this is a little harder for us to practice... Instructions To help you focus solely on the dynamic allocation and pointer use, a simplistic Voxel and World class have been created for you Start with the provided code by copying the files to your current directory cp /home/cs165new/check07b/* . Follow the comments in main (in check07b.cpp) to do the following: 1. Declare a new World object. Make sure to use pointers and dynamic allocation. 2. Call the "display" method on your World object 3. Code is in place to create a Voxel for air. Call the setBox method on your World object to set it at position: (50, 60, 70) 4. Code is in place to create a Voxel for grass. Call the setBox method on your World object to set it at position: (45, 20, 10) 5. Call the displayBox method (on your World object) twice to diplay the two Voxels you just set. 6. Finally, make sure to free up the memory taken by your World object by deleting it BACKGROUND The memory of your program is divided into two areas the "stack" and the "heap". The stack is where information about your function calls and their local variables are stored, whereas the heap is where data is stored when it is dynamically allocated (think pointers) Among other differences, the stack does not typically have as much space and you can cause a stack overflow or segmentation fault by either calling too many functions or by storing too many large values in local stack variables. Thus, a large array or something similar should be dynamically allocated so that it is stored on the heap SCENARIO Assume that we are writing a game like Minecraft. We will have a World class that stores a 3-dimensional array of "Voxels". A voxel is a simple element of volume (think Pixel, but 3D). Your task for this assignment is to create a world, set some Voxels and display them You are provided with a simple class for a Voxel that stores a RGB color value, as well as a character code for the type of element. You are also provided with a simple class for a World that stores a 3-Dimensional array, named boxes, that stores, 100 x 100 x 100 different Voxel objects. Because this large array is stored directly in the World class, you should not create a World object as a normal local stack variable. Depending on your environment this would likely even cause a segmentation fault. Instead you should use a Pointer to a world object, and dynamically allocate it using the "new" command As a side note, for all the reasons mentioned, a better design for the world class would be to have it contain a dynamically allocated array, and only store the pointer internally. But this is a little harder for us to practice... Instructions To help you focus solely on the dynamic allocation and pointer use, a simplistic Voxel and World class have been created for you Start with the provided code by copying the files to your current directory cp /home/cs165new/check07b/* . Follow the comments in main (in check07b.cpp) to do the following: 1. Declare a new World object. Make sure to use pointers and dynamic allocation. 2. Call the "display" method on your World object 3. Code is in place to create a Voxel for air. Call the setBox method on your World object to set it at position: (50, 60, 70) 4. Code is in place to create a Voxel for grass. Call the setBox method on your World object to set it at position: (45, 20, 10) 5. Call the displayBox method (on your World object) twice to diplay the two Voxels you just set. 6. Finally, make sure to free up the memory taken by your World object by deleting it

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

13.5 Multicollinearity

Answered: 1 week ago

Question

Describe the appropriate use of supplementary parts of a letter.

Answered: 1 week ago